Site logo

Archived topic

Count post in category description

8 replies · Started by vincent on August 4, 2022

Viewing posts 1–9 of 9

Hello,

I want to show category description and display post count in this category description.

Is that possible ?

Thank you

Hi Vincent,

You can add this PHP snippet:

add_shortcode( 'get_count', function() {
    ob_start();
    // Start your PHP below
  	$count = $GLOBALS['wp_query']->post_count;
    
    echo '<p class="cat-post-count>' . $count . '</p>';
  
    // End your PHP above
    return ob_get_clean();
} );

add_shortcode( 'get_description', function() {
    ob_start();
    // Start your PHP below
  	$desc = category_description();
    
   echo '<p class="cat-description>' . $desc . '</p>';
  
    // End your PHP above
    return ob_get_clean();
} );

With this, you can grab the post count and description in an archive page with these shortcodes respectively - [get_count] and [get_description]

Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets

sorry doesn't work.

I add a hook on my theme :
Hook : generate_before_main_content
display : Post Catégory Archive

sorry doesn’t work.

I add a hook on my theme :
Hook : generate_before_main_content
display : Post Catégory Archive

Hi there,

can you share a link to your Category archive ?

yes

Hi Vincent,

Can you try this PHP snippet instead:

add_shortcode( 'get_count', function() {
    ob_start();
    // Start your PHP below
  	$count = $GLOBALS['wp_query']->post_count;
    
    echo '<p class="cat-post-count">' . $count . '</p>';
  
    // End your PHP above
    return ob_get_clean();
} );

add_shortcode( 'get_description', function() {
    ob_start();
    // Start your PHP below
  	$desc = category_description();
    
   echo '<div class= "cat-description">' . $desc . '</div>';
  
    // End your PHP above
    return ob_get_clean();
} );

And make sure the execute shortcodesbox is ticked in the hook element.

Thank you now it's perfect :)

You are welcome :)

This archived topic is closed to new replies.