基本上正如标题所说,我想获得类别和子类别的列表,然后针对这些类别/子类别发布文章(带有链接)。
这就是我试图实现的结构:
第1类第1子类第1子类第2子类第2子类第1子类第2子类第2子类第2子类第1子类第3子类子类别2中的第3个帖子子类别1中的子类别1中的子类别1中的子类别1中的子类别2中的子类别32在子类别2内,第3篇在子类别2内,第3篇在子类别2内,第2篇在子类别2内,第1篇在子类别2内,第2篇在子类别2内,第3篇在子类别2内,第1篇在子类别2内第一篇没有子类别的帖子,目前为止,在阅读了关于该主题的所有内容后,我有以下代码:
<ul>
<?php
$get_parent_cats = array(
\'parent\' => \'0\' //get top level categories only
);
$all_categories = get_categories( $get_parent_cats );//get parent categories
foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;
echo \'<li><a href=" \' . get_category_link( $catID ) . \' ">\' . $single_category->name . \'</a>\'; //category name & link
$get_children_cats = array(
\'child_of\' => $catID //get children of this parent using the catID variable from earlier
);
$child_cats = get_categories( $get_children_cats );//get children of parent category
echo \'<ul class="children">\';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;
//for each child category, give us the link and name
echo \'<a href=" \' . get_category_link( $childID ) . \' ">\' . $child_cat->name . \'</a>\';
}
echo \'</ul></li>\';
} //end of categories logic ?>
</ul>
现在,这段代码很好地显示了类别和子类别,但我需要以某种方式循环浏览我的帖子,并用类别/子类别显示它们。我还尝试使用休闲代码:
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=100");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php echo \'<hr/>\'; ?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
</div><!-- #content -->
</div><!-- #container -->
此代码显示特定类别中的所有类别和帖子,但结构不是我想要的。两天来,我一直在尝试将这两段代码组合在一起,但我尝试的结果都不符合我的要求。我对Wordpress没有经验,我真的需要帮助。
最合适的回答,由SO网友:Bikash Waiba 整理而成
更新:完整代码
<ul>
<?php
$get_parent_cats = array(
\'parent\' => \'0\' //get top level categories only
);
$all_categories = get_categories( $get_parent_cats );//get parent categories
foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;
echo \'<li><a href=" \' . get_category_link( $catID ) . \' ">\' . $single_category->name . \'</a>\'; //category name & link
echo \'<ul class="post-title">\';
$query = new WP_Query( array( \'cat\'=> $catID, \'posts_per_page\'=>10 ) );
while( $query->have_posts() ):$query->the_post();
echo \'<li><a href="\'.get_the_permalink().\'">\'.get_the_title().\'</a></li>\';
endwhile;
wp_reset_postdata();
echo \'</ul>\';
$get_children_cats = array(
\'child_of\' => $catID //get children of this parent using the catID variable from earlier
);
$child_cats = get_categories( $get_children_cats );//get children of parent category
echo \'<ul class="children">\';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;
//for each child category, give us the link and name
echo \'<a href=" \' . get_category_link( $childID ) . \' ">\' . $child_cat->name . \'</a>\';
echo \'<ul class="post-title">\';
$query = new WP_Query( array( \'cat\'=> $childID, \'posts_per_page\'=>10 ) );
while( $query->have_posts() ):$query->the_post();
echo \'<li><a href="\'.get_the_permalink().\'">\'.get_the_title().\'</a></li>\';
endwhile;
wp_reset_postdata();
echo \'</ul>\';
}
echo \'</ul></li>\';
} //end of categories logic ?>
</ul>
SO网友:drjorgepolanco
此函数可以立即运行,只需确保将名称更改为您自己的分类法和帖子类型即可。请随意阅读评论以了解更多说明。
function ow_categories_with_subcategories_and_posts( $taxonomy, $post_type ) {
$taxonomy = $taxonomy;
$post_type = $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
}
ow_categories_with_subcategories_and_posts( \'the_name_of_your_taxonomy\', \'the_name_of_your_post_type\' );