[Resolved] Template Tags in Elements not showing up

Home Forums Support [Resolved] Template Tags in Elements not showing up

Home Forums Support Template Tags in Elements not showing up

  • This topic has 22 replies, 2 voices, and was last updated 4 years ago by David.
Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #1150857
    Aldrin

    Hi Dave…not sure what you mean by

    display rules to the Product > All Products then you will see the Title and the Terms displayed on all products.

    Can you give me an example ? Thank you

    #1150872
    David
    Staff
    Customer Support

    1. Create a New Header Element.
    2. Add this content:

    {{post_title}}
    {{post_terms.product_cat}}

    3. Display Rules -> Product > All Products.

    4. View a Single Product – you will see the Product Title followed by All the Categories that product belongs to.

    What you CANNOT do is display the {{post_terms.taxonomy}} on an Archive Page.

    #1150937
    Aldrin

    Gotcha…yes my main purpose was to do it on the archive pages..oh well. Thank you very much for the help. I’ll see if I can implement some custom coding via another route.

    #1151205
    David
    Staff
    Customer Support

    How would that be different to displaying the post title ?

    Using your example:

    1. for the /product/Cars archive – the {{post_title}} will display Cars.
    2. the /product/Cars/BMW archive -t he {{post_title}} will display BMW.

    #1633701
    Raul

    I want to use them in my singel posts to show to respective category but it works only with elements->header but not with elements-hook ๐Ÿ™

    And php is not working as hook like

    $category = get_the_category();
    $firstCategory = $category[0]->cat_name;
    #1633994
    David
    Staff
    Customer Support

    Hi there,

    Template Tags only work within the Header element.

    That code should work fine in a hook.
    1. Make sure the code is wrapped in the correct tags eg.

    <?php
    $category = get_the_category();
    $firstCategory = $category[0]->cat_name;
    echo $firstCategory;
    ?>

    2. Check the Execute PHP option in the hook.
    3. Make sure you’re using a Hook that is present on that template:

    https://docs.generatepress.com/article/hooks-visual-guide/

    #1634123
    Raul

    Hi, I used this here to getr the same like the header in the archive:

    <header class="page-header"><h1 class="page-title"><?php
    $category = get_the_category();
    $firstCategory = $category[0]->cat_name;
    echo $firstCategory;
    	?></h1></header>

    Seems to be working ๐Ÿ™‚

    Or with a link if someone need this

    <header class="page-header"><h1 class="page-title">
    <?php
    $category = get_the_category();
    $first_category = $category[0];
    echo sprintf( '<a href="%s">%s</a>', get_category_link( $first_category ), $first_category->name );
    ?>
    </h1></header>

    And for all categorys:

    <header class="page-header"><h1 class="page-title">
    	<?php 
    $categories = get_the_category();
    $separator = ' >> ';
    $output = '';
    if ( ! empty( $categories ) ) {
        foreach( $categories as $category ) {
            $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
        }
        echo trim( $output, $separator );
    }
    	?>
    	</h1></header>
    #1634402
    David
    Staff
    Customer Support

    Glad to see you found the solution.

Viewing 8 posts - 16 through 23 (of 23 total)
  • You must be logged in to reply to this topic.