我想按缩略图顺序显示帖子,即有缩略图的帖子会先显示,然后显示没有缩略图的帖子。
我在 Stackoverflow 上发现了同样的情况 here ,但这对我不起作用。目前我正在使用目录+主题,我想在其中实现它。
我尝试制作两个循环,一个循环将显示带有缩略图的帖子,第二个将显示没有缩略图的帖子。但这在分页的情况下不起作用,因为没有缩略图的帖子应该在最后一页
这是代码
{var $itemQuery = array(
'post_type' => 'ait-item',
'posts_per_page' => $filterCountsSelected,
//'meta_key'=> '_thumbnail_id',
'tax_query' => array(
array(
'taxonomy' => 'ait-items',
'field' => 'term_id',
'terms' => $currentCategory->term_id
)
),
'paged' => $pagination,
)}
{customQuery as $query, $itemQuery}
{* Loop for categories with featured Image*}
{customLoop from $query as $post}
{if $post->image}
//Display post with thumbnail
{/if} //end of if
{/customLoop} //end of loop
{* Loop for categories without featured Image*}
{customLoop from $query as $post}
{if !$post->image}
//Display post without thumbnail
{/if} //end of if
{/customLoop} //end of loop
{includePart parts/pagination, location => pagination-below, max => $query->max_num_pages} //includes pagination
编辑后的查询 1:
{var $itemQuery = array(
'post_type' => 'ait-item',
'posts_per_page' => $filterCountsSelected,
'tax_query' => array(
array(
'taxonomy' => 'ait-items',
'field' => 'term_id',
'terms' => $currentCategory->term_id
)
),
'paged' => $pagination,
)}
{var $itemQuery['orderby'] = 'meta_value_num'}
{var $itemQuery['meta_query'] = array(
array(
'relation' => 'OR',
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS'
),
array(
'key' => '_thumbnail_id',
'compare' => '!NOT EXISTS'
),
)
)}
最佳答案
如果帖子有缩略图,我会通过 foreach 分配数字变量对其进行排序,然后使用 array_multisort
试试这个,
function cmk_load_post_by_custom_order() {
// Query Argument
$args = array(
'post_type' => 'post',
'posts_per_page' => 6,
);
//Initization Wordpress Query
$query = new WP_Query( $args );
$posts = $query->get_posts();
$output = array();
$order = array();
//Ourput result as Custom Array
foreach( $posts as $post ) {
$has_thumb = has_post_thumbnail( $post->ID ) ? 1 : 0 ;
$output[] = array(
'title' => $post->post_title,
'slug' => $post->post_name,
'thumb' => $has_thumb,//get_the_post_thumbnail( $post->ID,'full'),
);
$order[] = $has_thumb;
}
array_multisort($order, SORT_DESC, $output);
echo '<pre>',print_r($output,1),'</pre>';
}
add_action('wp_head','cmk_load_post_by_custom_order');
编辑 1
如果您打算使用 wordpress 分页,因为分页依赖于查询对象,我上面的回答将不起作用,
我做了一些测试,我想到了在 orderby 上使用 meta_value_num 然后将 _thumbnail_id 设置为 meta_key并添加关系或如果 _thumbnail_id 是否为空,因为没有缩略图的帖子将有空 _thumbnail_id 键。
试试下面这个查询参数
$args = array(
'post_type' => 'post',
'posts_per_page' => 99,
'order' => 'DESC', // Display all post with thumbnail ID first
'orderby' => 'meta_value_num', // order by meta key value
'meta_query' => array( //query post based on meta key
array(
'relation' => 'OR', // add condition if meta key is exists or not
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS' // include post without _thumbnail_id key
),
array(
'key' => '_thumbnail_id',
'compare' => '!NOT EXISTS' // include post with _thumbnail_id key
)
)
)
);
我在本地测试它并得到以下结果,它查询有和没有缩略图的帖子,我在结果中包含了缩略图 ID,你可以看到帖子是根据缩略图值排序的
Array
(
[0] => Array
(
[title] => The Magnificent
[slug] => the-magnificent
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/happy-hour.png
[tuhmb_id] => 17
)
[1] => Array
(
[title] => The Awesome Company
[slug] => the-awesome-company
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/comedy.png
[tuhmb_id] => 16
)
[2] => Array
(
[title] => Selfieties
[slug] => selfieties
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/beverage-store.png
[tuhmb_id] => 13
)
[3] => Array
(
[title] => Drunkard
[slug] => drunkard
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/restaurant31.png
[tuhmb_id] => 9
)
[4] => Array
(
[title] => My First Company
[slug] => my-first-company
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/party.png
[tuhmb_id] => 7
)
[5] => Array
(
[title] => No Thumb Compnay
[slug] => no-thumb-compnay
[thumb] =>
[tuhmb_id] =>
)
)
编辑 2
您还可以尝试在 orderby 上使用数组值,并使用 meta_value_num 回退到 date,它显示所有带有缩略图 ID 的帖子,然后按以下方式排序日期然后所有没有缩略图 ID 的帖子
将argument参数改成这个,
'orderby' => 'date, meta_value_num', // order by meta key value
关于php - 如何在 wordpress 中按存在的缩略图显示帖子顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32456689/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择