WP查询类别属性不起作用

时间:2019-10-10 作者:Pedro Ferreira

我正在尝试在我的index.php 页面以从我的blogue 类别

在我的其他页面中,此查询非常有效,但在index.php 返回最近的帖子3次。

他们询问:

$args = array(
    \'posts_per_page\'   => 3,
    \'offset\'           => 0,
    \'cat\'         => \'\',
    \'category_name\'    => \'blogue\',
    \'orderby\'          => \'date\',
    \'order\'            => \'DESC\',
    \'include\'          => \'\',
    \'exclude\'          => \'\',
    \'meta_key\'         => \'\',
    \'meta_value\'       => \'\',
    \'post_type\'        => \'post\',
    \'post_mime_type\'   => \'\',
    \'post_parent\'      => \'\',
    \'author\'       => \'\',
    \'author_name\'      => \'\',
    \'post_status\'      => \'publish\',
    \'suppress_filters\' => true,
    \'fields\'           => \'\',

);
$wqBlog = get_posts($args); 
html:

<section class="container"> 
<div class="cards display--flex ">
            <?php foreach ($wqBlog as $blogPost): setup_postdata( $blogPost ); ?>
            <div class="card">
                <?php if(has_post_thumbnail( ))  : ?>
                <div class="card__image display--flex">
                    <!--imagem -->
                    <?php the_post_thumbnail( ); ?>
                </div> <!-- imagem -->
                <?php else : ?>
                <div class="card__image__none">
                </div>
                <?php endif;?>
                <div class="card__container">
                    <p class="card__container__subtitle">
                        <?php echo get_the_date(); ?>
                    </p>

                    <h2 class="card__container__title">
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
                </div>
            </div>
            <?php endforeach; ?>         
        </div> 

</section>

1 个回复
最合适的回答,由SO网友:Fernando Claussen 整理而成

与其使用get\\u posts,不如使用WP\\u Query?

我删除了一些参数,因为它们是默认参数,可以省略。

$args = array(
    \'posts_per_page\'   => 3,
    \'category_name\'    => \'blogue\',
    \'suppress_filters\' => true,
);
$wqBlog = new WP_Query($args);
在HTML中

<?php if( $wpBlog->have_posts() ): ?>
<section class="container"> 
<div class="cards display--flex ">
            <?php while ( $wqBlog->have_posts() ): $wpBlog->the_post(); ?>
            <div class="card">
                <?php if(has_post_thumbnail( ))  : ?>
                <div class="card__image display--flex">
                    <!--imagem -->
                    <?php the_post_thumbnail( ); ?>
                </div> <!-- imagem -->
                <?php else : ?>
                <div class="card__image__none">
                </div>
                <?php endif;?>
                <div class="card__container">
                    <p class="card__container__subtitle">
                        <?php echo get_the_date(); ?>
                    </p>

                    <h2 class="card__container__title">
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
                </div>
            </div>
            <?php endwhile; wp_reset_postdata(); ?>         
        </div> 

</section>
<?php endif; ?>