按子类别显示wordpress帖子

时间:2016-05-05 05:18:54

标签: php wordpress

我是wordpress的新手,我使用wordpress 4.5版,我喜欢按子类别显示帖子,有人请帮忙我怎么做。 这就是我想要的

父母类别名称

SUB CATEGORY 1

Post 1

Post 2

SUB CATEGORY 2

Post 3

Post 4

SUB CATEGORY 3

Post 5

Post 6

...

提前致谢

2 个答案:

答案 0 :(得分:1)

如果我没错,你需要这个,你需要双循环来获取子类别下的帖子

这是您获取当前页面类别的方式

<?php
    $categories = get_the_category();
    $catID = $categories[0]->cat_ID;
?>

然后使用上面的catID

执行此操作
<?php $subcats = get_categories('child_of=' . $catID);
    foreach($subcats as $subcat) {
    echo '<h3>' . $subcat->cat_name . '</h3>';
    echo '<ul>';
    $subcat_posts = get_posts('cat=' . $subcat->cat_ID);
    foreach($subcat_posts as $subcat_post) {
        $postID = $subcat_post->ID;
    echo '<li>';
    echo '<a href="' . get_permalink($postID) . '">';
    echo get_the_title($postID);
    echo '</a></li>';
    }
    echo '</ul>';
    } ?>

答案 1 :(得分:0)

如果要显示所有包含子类别的类别以及包含其各个帖子的各个子类别,则只要知道分类法的类别和帖子类型,就可以使用以下功能轻松地进行操作

例如,如果您有一个名为“ books”的自定义帖子类型和一个名为“ topic”的自定义分类法,则为

然后您将使用

调用此函数
ow_categories_with_subcategories_and_posts( 'topic', 'book' );

这将显示所有主题的列表及其各自的子类别,然后显示属于每个子类别的书籍。

类似

戏剧

  • 戏剧子类别1

    • book1
    • book2
    • book3
  • 戏剧子类别2

    • book4
    • book5

喜剧

  • 喜剧子类别1

    • book6
    • book7
  • 喜剧子类别2

    • book8
    • book9

以下是此代码:

function ow_categories_with_subcategories_and_posts( $taxonomy, $post_type ) {

    // Get the top categories that belong to the provided taxonomy (the ones without parent)
    $categories = get_terms( 
        array(
            'taxonomy'   => $taxonomy,
            'parent'     => 0, // <-- No Parent
            'orderby'    => 'term_id',
            'hide_empty' => true // <!-- change to false to also display empty ones
        )
    );
    ?>
    <div>
        <?php
        // Iterate through all categories to display each individual category
        foreach ( $categories as $category ) {

            $cat_name = $category->name;
            $cat_id   = $category->term_id;
            $cat_slug = $category->slug;

            // Display the name of each individual category
            echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug  . '</h3>'; 


            // Get all the subcategories that belong to the current category
            $subcategories = get_terms(
                array(
                    'taxonomy'   => $taxonomy,
                    'parent'     => $cat_id, // <-- The parent is the current category
                    'orderby'    => 'term_id',
                    'hide_empty' => true
                )
            );
            ?>
            <div>
                <?php

                // Iterate through all subcategories to display each individual subcategory
                foreach ( $subcategories as $subcategory ) {

                    $subcat_name = $subcategory->name;
                    $subcat_id   = $subcategory->term_id;
                    $subcat_slug = $subcategory->slug;

                    // Display the name of each individual subcategory with ID and Slug
                    echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';

                    // Get all posts that belong to this specific subcategory
                    $posts = new WP_Query(
                        array(
                            'post_type'      => $post_type,
                            'posts_per_page' => -1, // <-- Show all posts
                            'hide_empty'     => true,
                            'order'          => 'ASC',
                            'tax_query'      => array(
                                array(
                                    'taxonomy' => $taxonomy,
                                    'terms'    => $subcat_id,
                                    'field'    => 'id'
                                )
                            )
                        )
                    );

                    // If there are posts available within this subcategory
                    if ( $posts->have_posts() ):
                        ?>
                        <div>
                            <?php

                            // As long as there are posts to show
                            while ( $posts->have_posts() ): $posts->the_post();

                                //Show the title of each post with the Post ID
                                ?>
                                <p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
                                <?php

                            endwhile;
                            ?>
                        </div>
                        <?php
                    else:
                        echo 'No posts found';
                    endif;

                    wp_reset_query();
                }
                ?>
            </div>
            <?php
        }
        ?>
    </div>
    <?php
}

然后,如果您只想获取与喜剧相关的子类别,并且希望提供“喜剧”子句作为过滤器,以获取类似以下内容:

  • 喜剧子类别1

    • book6
    • book7
  • 喜剧子类别2

    • book8
    • book9

然后您将调用以下函数:

ow_subcategories_with_posts_by_category( 'topic', 'book', 'comedy' );

要做的功能将是:

function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
    $category = get_term_by( 'slug', $term, $taxonomy );
    $cat_id   = $category->term_id;

    // Get all subcategories related to the provided $category ($term)
    $subcategories = get_terms(
        array(
            'taxonomy'   => $taxonomy,
            'parent'     => $cat_id,
            'orderby'    => 'term_id',
            'hide_empty' => true
        )
    );
    ?>
    <div>
        <?php
        // Iterate through all subcategories to display each individual subcategory
        foreach ( $subcategories as $subcategory ) {

            $subcat_name = $subcategory->name;
            $subcat_id   = $subcategory->term_id;
            $subcat_slug = $subcategory->slug;

            // Display the name of each individual subcategory with ID and Slug
            echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';

            // Get all posts that belong to this specific subcategory
            $posts = new WP_Query(
                array(
                    'post_type'      => $post_type,
                    'posts_per_page' => -1, // <-- Show all posts
                    'hide_empty'     => true,
                    'order'          => 'ASC',
                    'tax_query'      => array(
                        array(
                            'taxonomy' => $taxonomy,
                            'terms'    => $subcat_id,
                            'field'    => 'id'
                        )
                    )
                )
            );

            // If there are posts available within this subcategory
            if ( $posts->have_posts() ):
                ?>
                <div>
                    <?php
                    while ( $posts->have_posts() ): $posts->the_post();

                        //Show the title of each post with the Post ID
                        ?>
                        <p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
                        <?php
                    endwhile;
                    ?>
                </div>
                <?php
            else:
                echo 'No posts found';
            endif;

            wp_reset_query();
        }
        ?>
    </div>
    <?php
}