Archived topic
Template Tags in Elements not showing up
22 replies · Started by Aldrin on January 21, 2020
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
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.
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.
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.
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;
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:
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>
Glad to see you found the solution.