[英]display wordpress posts by sub category
我是 wordpress 新手,我使用的是 wordpress 4.5 版,我喜歡按子類別顯示帖子,請任何人幫助我如何做到這一點。 這就是我想要的
家長類別名稱
子類別 1
Post 1
Post 2
子類別 2
Post 3
Post 4
子類別 3
Post 5
Post 6
...
提前致謝
如果我沒有錯,你需要這個,你需要雙循環來獲取子類別下的帖子
這是您獲取當前頁面類別的方式
<?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>';
}
?>
如果您想顯示所有具有子類別的類別,其各自的子類別具有各自的帖子,只要您知道分類法和帖子類型,您就可以使用以下功能輕松地做到這一點。
例如,如果您有一個名為“books”的自定義帖子類型和一個名為“topic”的自定義分類法來對您的書籍進行分類,
那么你將使用調用這個函數
ow_categories_with_subcategories_and_posts( 'topic', 'book' );
這將顯示所有主題及其各自子類別的列表,然后是屬於每個子類別的書籍。
類似的東西
戲劇
戲劇子類別 1
戲劇子類別2
喜劇
喜劇子類別 1
喜劇子類別 2
以下是這方面的代碼:
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
喜劇子類別 2
然后你會像這樣調用以下函數:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.