Add post category above post title

Hi, I’m using the Integrity stack and would like to have the category of my blog post appear above the title (.entry-header class). Could you please let me know how this could be achieved?

Cheers,
David

@DavidL,

Thanks for reaching out.

To add post category above post title, please add this code in your child themes functions.php

Child Themes

// Add category above post title
// =============================================================================
function add_custom_post_category(){ 
	$category = get_the_category();
    $firstCategory = $category[0]->name;
	if ( is_single() ) : ?>
      <h1 class="post-category"><span><?php echo $firstCategory; ?></span></h1>
	<?php endif;
 }
add_filter('x_before_view_integrity__content-post-header', 'add_custom_post_category');
// ============================================================================= 

I hope it will help you to solve your concern.

Thank you.

Thanks so much, this works perfectly!

Hi @DavidL,

It’s our pleasure to help you.

Thank you.

Sorry I just realised it doesn’t link to that category. Are you able to send me the modified code so that it links back to the category archive when clicked?

Thanks,
David

Hi @DavidL,

I have updated the code below so that it will link to the category page.

// Add category above the post title
// =============================================================================
function add_custom_post_category(){ 
	$category = get_the_category();
    $firstCategory = $category[0]->name;
    // Get the ID of a given category
    $category_id = get_cat_ID( $firstCategory );
    // Get the URL of this category
    $category_link = get_category_link( $category_id );
	if ( is_single() ) : ?>
      <h1 class="post-category"> <a href="<?php echo esc_url( $category_link ); ?>"><span><?php echo $firstCategory;?></span></a></h1>
	<?php endif;
 }
add_filter('x_before_view_integrity__content-post-header', 'add_custom_post_category');
// ============================================================================= 

You can also check in our docs for available filter and actions.

The code should work provided it’s copied completely. If that doesn’t work on your end, that means there’s an error with other custom codes you’ve added in your site. For that case, please consult with a 3rd party developer because providing custom code or custom development is outside the scope of our theme support.

Also, we’re creating a Layout Builder now to allow the users to build a custom archive and single pages. That means you won’t need custom code in the future.

Hope that helps and thank you for understanding.

Thanks, worked a treat! Super keen for the new layout builder to be rolled out (as are all of us X enthusiasts).

@DavidL,

It’s our pleasure to help you, just stay tuned for the upcoming updates.

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.