WordPress 分类页调用置顶文章时,如果没有置顶文章会自动调用普通文章的解决办法

wordpress教程 主机教程评论233字数 672阅读2分14秒阅读模式

最近发现 WordPress 分类页调用置顶文章时,如果没有置顶文章会自动调用普通文章,我调用置顶文章的代码如下:

WordPress文章源自国外主机测评-https://www.zjcp.org/3510.html

 $cat,
	'post__in' => $sticky_posts,
	'posts_per_page' => 2,
	); 
	$query = new WP_Query( $args ); 
	while ( $query->have_posts() ) : $query->the_post();
?> 
...

经过 echo 大法,发现当没有置顶文章时,post__in 这个条件就自动被忽略了,因此临时想了个解决办法,就是判断 $sticky_posts 为空时,给数组加上个 0 ,这样 post__in 就会生成一个查询 id in (0),也就查询不到了。文章源自国外主机测评-https://www.zjcp.org/3510.html

	if(empty($sticky_posts)){
		$sticky_posts = [0];
	}
	$args = array( 
	'cat'=> $cat,
	'post__in' => $sticky_posts,
	'posts_per_page' => 2,
	); 
	$query = new WP_Query( $args ); 
	while ( $query->have_posts() ) : $query->the_post();
?> 
...
文章源自国外主机测评-https://www.zjcp.org/3510.html文章源自国外主机测评-https://www.zjcp.org/3510.html
 
  • 本文由 主机测评 发表于 2022年10月25日23:02:00
  • 转载请务必保留本文链接:https://www.zjcp.org/3510.html

发表评论