Archived topic
Display categories on the top of the blogpost?
12 replies · Started by Janne on April 9, 2020
Hi.
Currently the categories are displayed at the end of the post. I want to display them below the title.
Is it possible to display the parent category on the top of the blogpost?
Hi there,
Are you wanting to display it inline with your other meta items? If so, where would you like them to display? After everything else?
Let me know :)
We want it below the title of the blogpost. Is it also possible to only show the parent category, not all?
What if there isn't a parent?
Should be possible using a Hook Element, just need to figure out the code we'll need :)
If there isnt a parent, we dont want anything to be shown.
You could try this:
add_action( 'generate_after_entry_title', function() {
$cat = get_the_category();
if ( isset( $cat[0]->parent ) ) {
var_dump($cat[0]->parent);
echo '<div class="parent-category">' . get_cat_name($cat[0]->parent) . '</div>';
}
} );
Let me know :)
Dont lookslike its working. Are now using this code, and that is working. But this displays all categories:
add_action( 'generate_after_entry_title', 'lh_add_cat_below_title' );
function lh_add_cat_below_title() {
if (is_single() ) {
$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
$categories_list = sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
_x( 'Categories', 'Used before category names.', 'generatepress' ),
$categories_list
);
}
?>
<div class="entry-meta">
<?php echo $categories_list; ?>
</div>
<?php
}
Both code snippets are running on my site now.
Now I understand what happened, your code added the categories on the blog page, not on the blogpost. It created a mess on my site, but removed your code now.
Can you try the updated code?: https://generatepress.com/forums/topic/display-categories-on-the-top-of-the-blogpost/#post-1234602
Nothing happened. Removed my code, and loaded your code, and nothing showed on the blogpost page. Tried running both my and your code, and then all the categories showed. Now both code snippets are active on my site.
Can you link me to your site so I can take a look?
Have added a link in the first post.
Can you try the updated code?: https://generatepress.com/forums/topic/display-categories-on-the-top-of-the-blogpost/#post-1234602
I added a debugging line in there to see if we have a parent object.