猿问

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

我正在为我的 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


    }


慕沐林林
浏览 139回答 1
1回答

神不在的星期二

$posts_args = array('post_type'&nbsp; &nbsp; &nbsp; => 'post','numberposts' => 1, //this show how many posts to query from DB'order'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'DESC','posts_per_page' => 1//this show how many posts display);post_per_page
随时随地看视频慕课网APP
我要回答