admin管理员组文章数量:1316023
I need to know how to display only the selected categories per post page..
Example. I am working on a website for movies. I have a category called Genre, then I have sub categories attached to that like comedy, horror etc...
Then I have a category called Availability and sub categories with like, USA, Mexico, England.. etc..
So I'll need to have it set like this per post page.
Genres Action/Adventure, Drama, New Releases
Availability Untied States, England, France
I have this code, but I can't figure out how to separate between genre and Availability
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );
} ?>
I need to know how to display only the selected categories per post page..
Example. I am working on a website for movies. I have a category called Genre, then I have sub categories attached to that like comedy, horror etc...
Then I have a category called Availability and sub categories with like, USA, Mexico, England.. etc..
So I'll need to have it set like this per post page.
Genres Action/Adventure, Drama, New Releases
Availability Untied States, England, France
I have this code, but I can't figure out how to separate between genre and Availability
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );
} ?>
Share
Improve this question
edited May 28, 2017 at 11:03
Mark Kaplun
23.7k7 gold badges43 silver badges65 bronze badges
asked May 28, 2017 at 10:50
Timothy RookerTimothy Rooker
113 bronze badges
1
- Related: wordpress.stackexchange/questions/231976/… – Jesse Nickles Commented Nov 20, 2023 at 12:20
1 Answer
Reset to default 1If what I understand from your question is correct. You have a post that categorized in multiple categories and subcategories. I have already made the same in a website. use these following code bellow so a sub categories will displayed after the parent category.
//first part
$allcats = get_the_category();
$parent_id = $all_ids = array();
foreach ($allcats as $cats) {
//get all category id first
$all_ids[] = $cats->term_id;
// get top parent category
if ($cats->category_parent === 0 ) {
$parent_id[] = $cats->term_id;
}
}
//second part
$separator = ' » ';
$term_ids = implode( ',' , $all_ids );
$hiearchy_cat = '';
foreach ($parent_id as $parents) {
$parent_name = get_cat_name($parents);
$parent_link = get_category_link($parents);
$parent_cat = '<span class="parent"><a href="'.$parent_link.'">'.$parent_name.'</a></span>';
//get all sub category with certain ids
$child_cats = wp_list_categories(
array(
'child_of' => $parents,
'title_li' => '',
'style' => 'none',
'echo' => false,
'taxonomy' => 'category',
'include' => $term_ids,
)
);
$child_cats = rtrim( trim( str_replace( '<br />', $separator, $child_cats ) ), $separator );
$hiearchy_cat .= '<b>'.$parent_cat.'</b> » '.$child_cats.'<br>';
}
//last output
echo $hiearchy_cat;
Hope it helps
本文标签: Display selected categories onto post page
版权声明:本文标题:Display selected categories onto post page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741994989a2409860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论