php - Wordpress 查询仅显示置顶帖子

标签 php wordpress

以下是我的 wordpress 查询,我只想在其中显示置顶帖子,但查询未显示任何帖子。此外,我将两个帖子设置为置顶,以便检查该部分!!!!请告诉我如何修改此查询,以便它只显示置顶的帖子

<?php 
   $wp_query = null; 
  $wp_query =  new WP_Query(array(
 'posts_per_page' => 2,
 //'paged' => get_query_var('paged'),
 'post_type' => 'post',
'post__in'  =>  'sticky_posts',
 //'post__not_in' => array($lastpost),
 'post_status' => 'publish',
 'caller_get_posts'=> 0 ));

  while ($wp_query->have_posts()) : $wp_query->the_post(); $lastpost[] = get_the_ID();
?>

最佳答案

只显示置顶帖子的查询:

// get sticky posts from DB
$sticky = get_option('sticky_posts');
// check if there are any
if (!empty($sticky)) {
    // optional: sort the newest IDs first
    rsort($sticky);
    // override the query
    $args = array(
        'post__in' => $sticky
    );
    query_posts($args);
    // the loop
    while (have_posts()) {
         the_post();
         // your code
    }
}

关于php - Wordpress 查询仅显示置顶帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19814320/

相关文章:

css - 仅倾斜/倾斜 div 的底部

mysql - 针对 100 多个 wp 数据库和低 RAM 使用率优化 my.cnf

php - 使用 PHP 从 SQL 表中进行选择并从结果中删除特定前缀

wordpress - 如何按语言获取wordpress api数据?

javascript - JavaScript 和 PHP 中的动态变量

php - WP_REST_Response 与 WP_Error

php - 如何防止 PHP 中的 SQL 注入(inject)?

php - 如何将参数发送到 PHP 中的比较函数?

php - 如何修复PHP中的“ header 已发送”错误

php - 在 WooCommerce "My account"仪表板上显示最新订单