繁体   English   中英

Wordpress我的查询帖子未显示带有类别的帖子

[英]Wordpress my query post doesn't display my post with category

我是wordpress的新手,我有一个帖子,其中提供了商品的category_name并且有一个内容,这是固定链接: http://localhost/jcjohn/2016/09/20/what-we-offer/

现在,我想在我的版面页面中显示帖子的内容。

这是本节中的代码:

<section id = "offer">
<?php    
    $args = array(
        'type' => 'post',
        'posts_per_page' => 3,
        'category_name' => 'offer',
    );
    $offerBlog = new WP_Query($args);

    if ($offerBlog->have_post()):
        while ($offerBlog->have_posts()):
            $offerBlog->the_post();
            get_template_part('content', get_post_format()); 
        endwhile;
    endif;
    wp_reset_postdata();    
?>
</section>

您尝试将此循环与创建类别的cat_id一起使用。

query_posts( array( 'cat' => '1', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'DESC' ) );

您可以尝试以下替换的代码。

<section id = "offer">
<?php    
    $args = array(
        'post_type' => 'post',
        'cat' => '1',
        'posts_per_page' => 5,
        'orderby' => 'title', 
        'order' => 'DESC' 
    );
    $offerBlog = new WP_Query($args);

    if ($offerBlog->have_posts()):
        while ($offerBlog->have_posts()):
            $offerBlog->the_post();
            get_template_part('content', get_post_format()); 
        endwhile;
    endif;
    wp_reset_postdata();    
?>
</section>

您错过了循环格式。

参考:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

因此,这是您需要执行的操作才能在单个页面上显示帖子。 你似乎已经错过了shave_post所以它需要像下面

注意:这将进入index.php

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'category_name' => 'offer',
);
$offerBlog = new WP_Query( $args );
?>
<!-- Blog Article -->
<section id="blog">
    <?php
    if ( $offerBlog->have_posts() ) :
        while ( $offerBlog->have_posts() ) : $offerBlog->the_post();

        the_content();

        endwhile;
    else : ?>
        <div class="no-content">
            <h3>Well... it looks like we forgot to put content here.</h3>
        </div>
    <?php
    endif;
    wp_reset_postdata();
    ?>
</section>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM