Hi Alchymyth,
I’m again here to ask you a hand if it’s possible.
I need to make a couples of little tweaks in the code you kindly sent me.
In the Home Page I have this code:
<?php
$just_shown = $last_shown; $last_shown = array(); $output = '';
foreach((get_the_category()) as $category) {
$catname = $category->cat_name;
if(!in_array($catname,$just_shown)) {
$cat = get_category($category->term_id); //new line to show only top level cat name
if(!$cat->parent) { $output .= $catname . ' '; } //new if statement
}
$last_shown[] = $catname;
}
if($output != '') {
echo '<div class="sectioncat">' . $output . '</div>';
}
?>
I’d like to use an if…else statement in the home page I guess, in order to have posts that belong to “Events” category show only children categories separated by comma in the header (and never show the header “Events”).
So I tried to use <?php if (in_category(‘events’)) : ?> and edit your code but I got lost.
Here is what I did:
<?php if (in_category('events')) { ?>
echo '<?php $just_shown = $last_shown; $last_shown = array(); $sep = ''; $output = '';
foreach((get_the_category()) as $category) {
$catname = $category->cat_name;
if(!in_array($catname,$just_shown)) {
$cat = get_categories('parent='.$category->term_id);
if(!$cat) { $output .= $sep . $catname; $sep = ', '; }
}
$last_shown[] = $catname;
}
?>'
else
echo '<?php
$just_shown = $last_shown; $last_shown = array(); $output = '';
foreach((get_the_category()) as $category) {
$catname = $category->cat_name;
if(!in_array($catname,$just_shown)) {
$cat = get_category($category->term_id); //new line to show only top level cat name
if(!$cat->parent) { $output .= $catname . ' '; } //new if statement
}
$last_shown[] = $catname;
}
if($output != '') {
echo '<div class="sectioncat">' . $output . '</div>';
}
?>' <?php } ?>
Thanks for your help!!!