• Resolved Sam

    (@wamshatley)


    Hi there,

    I hooked in a block element that uses dynamic content for headlines on a custom taxonomy archive page (it displays both the term name and some term meta fields).

    Sometimes the dynamic content renders, and other times not. I’ve narrowed it down to taxonomy archive pages with no posts in the main loop being when it doesn’t render the dynamic content. However, I still need to display the term name and the term meta on the page when there are no posts.

    How can I make sure the dynamic content still renders, even when there are no posts in the main loop?

    If I create a hook element and code the lookup myself in PHP (see below for code I’ve tested), it works fine, so I can’t see why dynamic content for the term itself wouldn’t work in a headline when there are no posts in the loop.

    $term = get_queried_object_id();
    $currency = get_term_meta( $term, 'meta_key' , true);

    Thanks in advance,
    Sam

    • This topic was modified 2 years, 3 months ago by Sam.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support fernandoazarcon2

    (@fernandoazarcon2)

    Hi Sam,

    By default getting the archive title doesn’t work for archive pages with no posts. How exactly have you set up the Headline Block right now? Can you take a screenshot of it?

    We would need to filter it probably to make it work.

    Thread Starter Sam

    (@wamshatley)

    Hi Fernando,

    Thanks for the reply. This is the block I’m creating:
    https://i.ibb.co/TmkRWnS/Screenshot-2022-11-16-at-08-16-45.png

    At the moment, I’ve ended up making a shortcode and embedding that in a container to do what I need it to do with the term metas I need, but it just felt like it shouldn’t be needed when a separate header element can access the term name on the same page and the GP dynamic content block can access the term description fine.

    But as you say, if there’s a filter to make this happen or if there’s a way of allowing it in the first place, that would be better than my shortcode approach.

    Thanks!

    • This reply was modified 2 years, 3 months ago by Sam.
    Plugin Support fernandoazarcon2

    (@fernandoazarcon2)

    I see. To do so, set up the dynamic data through the Block Settings. Example: https://share.getcloudapp.com/qGuZjgZo

    Then, give the GB Headline Block a class of cu-currency through the advanced section of its Block settings.

    Then, add this PHP snippet:

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    	if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-currency' ) !== false ) {
    		$content = 'Local currency: ' . get_term_meta(get_queried_object_id(), 'currency', true);
    	}
    	return $content;
    }, 10, 3);

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

    Thread Starter Sam

    (@wamshatley)

    Hi Fernando, perfect – thanks for this.

    Good to know the block settings dynamic content will still render even with the empty loop and can then be modified – I was scratching my head trying to understand why the toolbar dynamic content stopped the element being rendered with just the prefix at least.

    Really appreciated!
    Sam

    • This reply was modified 2 years, 3 months ago by Sam.
    Plugin Support fernandoazarcon2

    (@fernandoazarcon2)

    You’re welcome Sam!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Dynamic content not rendering in element on taxonomy archive with no posts’ is closed to new replies.