Site logo

Archived topic

Custom Category Template -> Problem using dynamic title

15 replies · Started by Jonas on May 19, 2021

Viewing posts 1–15 of 16

Hi team, thank you for your fantastic support!

I created a custom loop for a taxonomy-archive, filtering posts by two taxonomies.
Everything works fine so far.

The only thing that does not work 100% is applying dynamic title with a layout template using a block element.
When I apply the block element to the archive page, it shows the taxonomy of the archive as the title of the post.

Here is the code I'm using. I have two more loops like that but with different taxonomies on the page.

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
/* loop1 */
$the_query = new WP_Query( array(
    'post_type' => 'listing',
    'tax_query' => array(
		    'relation' => 'AND',
        array (
            'taxonomy' => 'placement',
            'field' => 'slug',
            'terms' => 'premium',
        ),
        array (
            'taxonomy' => 'sector',
            'field' => 'slug',
            'terms' => $term->slug,
        ),
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
	generate_do_template_part( 'archive' );
endwhile;

wp_reset_postdata();
/*end loop 1*/

Hi there,

can you share a link to the archive so we can see whats going on?

Attached the link to this post for you David. Thank you!

That's definitely strange.

In your template, can you add the_title(); above generate_do_template_part( 'archive' );?

What does it output?

the_title(); outputs the correct title of the post.

As long as I don't use an "elements"->block->content template for that page, the titles are displayed correctly too.

Also, when I change generate_do_template_part('archive'); to get_template_part('archive'); it works fine, but it the loops through the header and footer several times.

Think I see the issue. When we determine what dynamic title to output, we check to see if in_the_loop() is true. This allows our title to output the category title above a list of posts (the loop) below it.

However, you're using a custom query, so in_the_loop() returns false, which means the dynamic title will grab the archive title instead of the individual post titles.

I checked to see if there's a way to manipulate in_the_loop(), but it doesn't look possible.

One solution would be to set up your own global variable:

$GLOBALS['is_custom_query'] = false;

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $GLOBALS['is_custom_query'] = true;
    generate_do_template_part( 'archive' );
    $GLOBALS['is_custom_query'] = false;
endwhile;

Now you can filter the dynamic title to check for your global variable:

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    $text_type = $block['attrs']['gpDynamicTextType'];

    if ( ! empty( $text_type ) && 'post-title' === $text_type && $GLOBALS['is_custom_query'] ) {
        $text = get_the_title();
    }

    return $text;
}, 10, 2 );

Let me know if that works or not :)

Hi Tom,
thank you for the suggestion. Do I just insert both snippets at the end of my loop? As in:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
/* loop1 */
$the_query = new WP_Query( array(
    'post_type' => 'listing',
    'tax_query' => array(
		    'relation' => 'AND',
        array (
            'taxonomy' => 'placement',
            'field' => 'slug',
            'terms' => 'premium',
        ),
        array (
            'taxonomy' => 'sector',
            'field' => 'slug',
            'terms' => $term->slug,
        ),
    ),
) );

$GLOBALS['is_custom_query'] = false;

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $GLOBALS['is_custom_query'] = true;
    generate_do_template_part( 'archive' );
    $GLOBALS['is_custom_query'] = false;
endwhile;

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    $text_type = $block['attrs']['gpDynamicTextType'];

    if ( ! empty( $text_type ) && 'post-title' === $text_type && $GLOBALS['is_custom_query'] ) {
        $text = get_the_title();
    }

    return $text;
}, 10, 2 );

wp_reset_postdata();

Because this doesn't seem to work. But I might be wrong about where to insert the "add_filter" part?

The filter should be added as a separate function in your child theme functions.php file or the Code Snippets plugin: https://docs.generatepress.com/article/adding-php/

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    $text_type = $block['attrs']['gpDynamicTextType'];

    if ( ! empty( $text_type ) && 'post-title' === $text_type && $GLOBALS['is_custom_query'] ) {
        $text = get_the_title();
    }

    return $text;
}, 10, 2 );

Hi Tom,

thank you for your help. The code does not work for me. I think I will abandon this approach at this point as it seems to cause more problems than it solves. It's just not an intended use of the theme I guess :)

We didn't consider custom queries when building it, but I'm surprised that the above doesn't work. If you decide to test further, I would do a var_dump($GLOBALS['is_custom_query']); in the filter to make sure the global variable is coming through.

Thanks Tom. It returns "bool(true) bool(true)" above the posts.
The Link to the post is correct but the title, date etc. still come from the taxonomy archive page rather than the post.

As I said it all works fine if I don't use the element -> block -> content template.

What happens if you var_dump() something directly above $text = get_the_title();? Does anything display?

No, nothing displays in this case.

So that means the other condition is failing. Is the element you're targeting set to display the "Title"?

Hi, I "solved" this by using sorting my posts based on a custom taxonomy instead of using several loops.

Thank you for your awesome support and for trying, Tom!

This archived topic is closed to new replies.