【问题标题】:How to display posts by category in a static page on WordPress如何在 WordPress 的静态页面中按类别显示帖子
【发布时间】:2018-11-20 00:29:58
【问题描述】:

你知道我应该使用哪个代码在静态页面中按类别显示帖子吗?

<div>
// This is a static page


// Here I want to display posts from specific category ex. category named hello


</div>

【问题讨论】:

    标签: wordpress post categories display


    【解决方案1】:

    从特定类别获取帖子并在页面布局中显示帖子(如果您有更多帖子)的完整实现。

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    
    $args = array(
        'posts_per_page' => 10,
        'category_name' => ‘your category’,
        'paged' => $paged,
    );
    
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        while ( $the_query->have_posts() ) {
            $the_query->the_post();?>
                <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                    <h5><?php the_time('F j, Y'); ?></h5>
                    <div>
                       <?php if( has_excerpt() ): the_excerpt(); else: the_content(); endif; ?>
                    </div>
                <?php  }?>
        //Page navigation
            <div id="pagenav" class="navigation clearfix">
                <div class="nav-prev alignleft"><?php next_posts_link( '<i class="fa fa-angle-double-left" aria-hidden="true"></i> Older Entries', $the_query->max_num_pages ); ?></div>
                <div class="nav-prev alignright"><?php previous_posts_link( 'Newer Entries <i class="fa fa-angle-double-right" aria-hidden="true"></i>' ); ?></div>
            </div><!-- #pagenav -->
            <?php echo '</div>';
    
         } else {
            // no posts found
            echo '<h1>No Posts Found</h1>';
        }
    
        // Restore original Post Data
        wp_reset_postdata();
        ?>
    

    【讨论】:

    • 谢谢,但你能告诉我如何在此处使用变量 prnt.sc/jyawtd 创建此部分,因为我为每个页面都有一个类别,如果页面具有相同的类别,我希望显示类别帖子就像我的第 1 页有第 1 类,所以当我希望第 1 类的帖子显示在第 1 页时,如果我有另一个页面是第 2 页并且它有第 2 类,所以我希望第 2 类的帖子是显示在第 2 页
    • 为了简单起见,请看prnt.sc/jyayir 如果我创建了一个新页面,然后检查了其中一个类别,我希望选中的类别中的帖子显示在页面中。
    【解决方案2】:

    使用以下内容:

    [display-posts category="fishing,hiking"]
    

    您还可以提出许多其他论点。这个链接很有用:https://en.support.wordpress.com/display-posts-shortcode/

    【讨论】:

    • 谢谢,但您的代码仅适用于 Wordpress.com,而不适用于默认的 wordpress 框架 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多