Site logo

Archived topic

Add dynamic content in multiple pages

1 reply · Started by Francesco on June 14, 2021

Viewing posts 1–2 of 2

Hi
I like to add dynamic contents visible in multiple pages.
Example. a category description visible in all pages of that category, INSIDE main content area (no top no footer no sidebar)
How to create this snippet with Generatepress-Gutemberg-Blockeditor?
Thanks for your support
Frank

Hi Francesco,

We can create a shortcode that pulls in the category description of the first category of a post.

Example PHP snippet:

add_shortcode('display_cat_desc', function($atts){
	$atts = shortcode_atts( array(
			'wrapper_element' => '',
			'wrapper_class' => ''
		), $atts, 'display_cat_desc' );

	$catID = get_the_category();

	$html = $atts['wrapper_element'];
	$classes = $atts['wrapper_class'];

	if(!$html){
		echo category_description( $catID[0] );
	} else {
		if(!$classes){
			echo '<'.$atts['wrapper_element'].'>'. category_description( $catID[0] ) .'</'.$atts['wrapper_element'].'>';
		} else {
			echo '<'.$atts['wrapper_element'].' class="'.$atts['wrapper_class'].'">'. category_description( $catID[0] ) .'</'.$atts['wrapper_element'].'>';

		}
	}
});

Here's how to add PHP - https://docs.generatepress.com/article/adding-php/

This PHP allows you to use the shortcode [display_cat_desc] on your content using the shortcode block.

I've also added shortcode atts in case you want to wrap it with a wrapper element.

Example shortcode usage: [display_cat_desc wrapper_element="div" wrapper_class="test-class"]

This archived topic is closed to new replies.