Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 首先使用WP\u查询和分类显示粘性帖子,然后_Wordpress - Fatal编程技术网

Wordpress 首先使用WP\u查询和分类显示粘性帖子,然后

Wordpress 首先使用WP\u查询和分类显示粘性帖子,然后,wordpress,Wordpress,尝试使用WP_查询和category_u和显示来自多个帖子类别的常见帖子。以下是查询: $query = array( 'category__and' => array( 'cat-1', 'cat-2'), 'posts_per_page' => 10, 'paged' => $paged ); $cat_query = new WP_Query($query); 现在,在上面的例子中,帖子被正确地获取,但是粘性帖子不会首先显示 下面的代码

尝试使用WP_查询和category_u和显示来自多个帖子类别的常见帖子。以下是查询:

$query = array(
    'category__and' => array( 'cat-1', 'cat-2'),
    'posts_per_page' => 10,
    'paged' => $paged
    );

$cat_query = new WP_Query($query);
现在,在上面的例子中,帖子被正确地获取,但是粘性帖子不会首先显示

下面的代码解决了这个问题,但它不执行类别文章的“和”

$query = array(
    'cat' => array( 'cat-1', 'cat-2'),
    'posts_per_page' => 10,
    'paged' => $paged
    );
上面的查询首先显示粘性文章,但不会对类别文章执行“和”


有什么方法可以满足多个类别的粘性帖子优先和普通帖子的条件吗?

您可以使用这些参数来显示粘性帖子-

$query_sticky = array(
  'category__and' => array( 'cat-1', 'cat-2'),
  'posts_per_page' => 10,
  'post__in' => get_option( 'sticky_posts' ),
  'ignore_sticky_posts' => 1,
  'paged' => $paged
);
$query_sticky = new WP_Query($query_sticky);
一旦你有了这些粘帖,你可以在主查询中合并它们-

$query->posts = array_merge($query->posts, $query_sticky->posts);
$query->post = reset($query->posts);
$query->post_count += $query_sticky->post_count;
$query->found_posts += $query_sticky->found_posts;
$query->max_num_pages = $query->found_posts / $query->get('posts_per_page');

要使wordpress循环正常工作,需要使用
post\u count
等变量。

如果对查询进行分页,则会在每页的分类帖子顶部添加粘性帖子。