如何在类别页面上获得子类别和帖子?

时间:2016-10-25 15:48:03

标签: php wordpress categories posts

我知道如何在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 );

?>

1 个答案:

答案 0 :(得分:0)

将其放在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
}