为了账号安全,请及时绑定邮箱和手机立即绑定

如何在wordpress小部件中仅查询一篇文章

如何在wordpress小部件中仅查询一篇文章

PHP
慕沐林林 2021-11-13 10:38:42
我正在为我的 wordpress 主题制作一个 wordpress 小部件。在小部件中,我试图仅查询我正在使用的 front-page.php 上的一篇文章WP_query。但问题是它正在获取所有可用的帖子。我不知道如何解决这个问题。任何建议都会有所帮助。我的代码public function widget($args, $instance) {        $posts_args = array(            'post_type'     => 'post',            'post_per_page' => 1,            'order'         => 'DESC'        );        $posts_query = new WP_Query($posts_args);        echo $args['before_widget'];            if($posts_query -> have_posts()):                while($posts_query->have_posts()): $posts_query->the_post();                    echo '<div class="widget-single-content" style="background-image: url('.get_the_post_thumbnail_url().')">';                    echo '<div class="content-box">';                    echo '<h1><span>"</span>'.get_the_title().'<span>"</span></h1>';                    echo '<button class="readmore-btn text-captalize">                    <a href="'.get_the_permalink().'">Read More</a></button>';                    echo '</div>';                    echo '</div>';                endwhile;            endif;        echo $args['after_widget'];        //Query    }
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

$posts_args = array(

'post_type'      => 'post',

'numberposts' => 1, //this show how many posts to query from DB

'order'          => 'DESC',

'posts_per_page' => 1//this show how many posts display

);

post_per_page


查看完整回答
反对 回复 2021-11-13
  • 1 回答
  • 0 关注
  • 138 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信