【问题标题】:display wordpress posts by sub category按子类别显示 wordpress 帖子
【发布时间】:2019-11-06 14:45:16
【问题描述】:

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

父类别名称

子类别 1

Post 1

Post 2

子类别 2

Post 3

Post 4

子类别 3

Post 5

Post 6

...

提前致谢

【问题讨论】:

  • 检查类别__in
  • 你应该为此编写自己的插件

标签: php wordpress


【解决方案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>';
                    echo '</li>';
            }
        echo '</ul>';
    } 
?>

【讨论】:

  • 谢谢,它正在工作,但是在按子类别显示所有帖子后,它会再次显示父类别下的帖子,就像这样子类别 1 帖子 1 帖子 2 子类别 2 帖子 3 帖子 4 父类别 帖子 1 帖子 2 帖子 3 帖子 4
  • 不想再在父类下显示帖子了,求助
  • 还要检查类别中的层次结构,您可能有一个与父类别同名的子类别??
  • plaese go to enterhelix.com/shan/wordpress4.5/category/surgerical-instrument parent category - “手术器械”下的手术器械子类别 - 腹部牵开器、绷带剪刀
  • 不,我只有 1 个父类和父类下的 2 个子类
【解决方案2】:

如果您想显示所有具有子类别及其各自帖子的子类别的类别,您可以使用以下功能轻松做到这一点,只要您知道分类法的 slug 和帖子类型.

例如,如果您有一个名为“书籍”的自定义帖子类型和一个用于对您的书籍进行分类的名为“主题”的自定义分类法,

那么你可以使用

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

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

类似

戏剧

  • 戏剧子类别 1

    • 书1
    • 书2
    • 书3
  • 戏剧子类别 2

    • book4
    • 书5

喜剧

  • 喜剧子类别 1

    • 书6
    • 书7
  • 喜剧子类别 2

    • book8
    • 书9

以下是代码:

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
}

那么,如果您只想获得与喜剧相关的子类别,并且您想提供“喜剧”slug 作为过滤器以获得类似的内容:

  • 喜剧子类别 1

    • 书6
    • 书7
  • 喜剧子类别 2

    • book8
    • 书9

然后你会像这样调用以下函数:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多