[英]adding a div block to a wordpress loop when no posts are found in a category
[英]Adding an offset to a category loop in WordPress
在WordPress主题的category.php中,您有以下循环:
if ( have_posts() ) : while ( have_posts() ) : the_post();
// output posts
endwhile; endif;
你如何输出这个完全相同的循环,但有一个偏移量? 我发现你可以通过做一个改变循环
query_posts('offset=4');
但是这会重置整个循环并且偏移量会起作用,但会显示每个类别的所有帖子,所以我得到的结果是query_posts完全重置循环并仅使用您添加的过滤器来完成。 有没有办法告诉循环:
“做你正在做的事情,除了偏移让它4”
这可能吗?
谢谢!
首先,不使用query_posts()
看到这里改用WP_Query
试试这个:
//To retrieve current category id dynamically
$current_cat = get_the_category();
$cat_ID = $current_cat[0]->cat_ID;
$loop = new WP_Query(array(
'offset' => 4, //Set your offset
'cat' => $cat_ID, //The category id
));
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
// output posts
endwhile; endif;
是的Wordpress声明:
设置偏移参数会覆盖/忽略分页参数并打破分页(单击此处获取解决方法)
只需遵循分页解决方法说明,您就可以开始了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.