【问题标题】:How to get on a Category Page the subcategory and posts?如何在类别页面上获取子类别和帖子?
【发布时间】:2016-10-25 15:48:02
【问题描述】:

我会知道如何在 Wordpress 类别页面 (category.php) 上获取子类别和该子类别的帖子?

我现在下面有这段代码,我想在“echo 'test';”的位置显示父类别的帖子。

<?php
$args = array('child_of' => get_query_var('cat') );
$categories = get_categories( $args );

$numOfItems = 20; // Set no of category per page
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$to = $page * $numOfItems;
$current = $to - $numOfItems;
$total = sizeof( $categories );

echo '<ul class="cat-content">';
for( $i=$current; $i<$to; ++$i ) {
    $category = $categories[$i];
    if( $category->name ) {
        echo '<li><h2>'. $category->name.'</h2>';

        echo 'test';

        echo '</li>';
    }
}
echo '</ul>';

unset( $category );

?>

【问题讨论】:

    标签: php wordpress categories posts


    【解决方案1】:

    将其放在echo "test"; 所在的位置。

    // The Query
    $args = array('post_type' => post, 'posts_per_page' => -1, 'cat' => $category->term_id );
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li><a href="'.get_permalink().'" >' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
    }
    

    【讨论】:

    • 嗨@WordpressDave 非常感谢。它对我有用。我还需要获取帖子链接。现在我只有标题。我怎样才能得到链接?
    猜你喜欢
    • 2018-11-28
    • 1970-01-01
    • 2014-05-13
    • 2019-01-17
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多