- This topic has 5 replies, 2 voices, and was last updated 3 years, 3 months ago by Elvin.
-
AuthorPosts
-
January 5, 2021 at 1:50 pm #1607156Neil
I want to check to see if there is a term and display the [label for the term:] [term] [ | ] in the header. Multiple taxonomies are to be separated by a pipe. I only have two custom taxonomies.
I use this to only show a taxonomy and its label if there is a term on the post from my theme:
<?php $term_list = get_the_term_list( get_the_ID(), ‘topics’ );
if(!empty($term_list)):?><b>Topics</b><?php echo get_the_term_list( $post->ID, ‘topics’, ‘<li class=”topics_item”>’, ‘, ‘, ‘‘ ) ?><?php endif;?>
But I would like to use the shortcode for the term in the Header Element but also show the label.
I read about echo not working – and I tried to make a shortcode with a conditional check but it did not work.
Thank you for you support
January 5, 2021 at 10:11 pm #1607436ElvinStaffCustomer SupportHi,
You can turn your code into a shortcode if you want.
You can use the PHP snippets provided on these links to see how it’s done.
https://generatepress.com/forums/topic/custom-field-color-to-change-element-background/
https://generatepress.com/forums/topic/short-code-edit-subcategory-label/
https://generatepress.com/forums/topic/how-to-create-a-categories-list-similar-to-this-link-in-post/
https://generatepress.com/forums/topic/help-with-template-tags-for-custom-post-type/
Most of them use
get_the_category()
but you should be able to modify them to doget_the_term_list()
instead. πJanuary 6, 2021 at 10:57 am #1608393NeilHi Elvin,
Here is my working code. Not pretty? I am a PHP newbie. I tested it and it works, though and I am grateful for the links you provided.
Is there something you can see that can be done better?
add_shortcode( ‘authors_terms’, function( $atts ) {
ob_start();
$options = shortcode_atts( array(
‘term’ => ‘authors’,
), $atts );$terms = get_the_term_list( get_the_ID(), $options[‘term’], ”, ‘, ‘, ” );
echo ‘<i class=”fas fa-pen-nib”></i> Authors: ‘, ($terms);
echo ‘ ‘;if ( ! empty( $terms ) ) {
return ob_get_clean();
}
} );add_shortcode( ‘topics_terms’, function( $atts ) {
ob_start();
$options = shortcode_atts( array(
‘term’ => ‘topics’,
), $atts );$terms = get_the_term_list( get_the_ID(), $options[‘term’], ”, ‘, ‘, ” );
echo ‘<i class=”fas fa-folder”></i> Topics: ‘, ($terms);
echo ‘ ‘;if ( ! empty( $terms ) ) {
return ob_get_clean();
}
} );January 6, 2021 at 11:12 am #1608423NeilHi again,
I did not look carefully, but when I do not have a taxonomy term, the html string appears above the header.
It is probably because I need to use a unique name for each shortcode – I am using terms.Then I edited the 2 shortcode functions to have unique terms – aterms and tterms but the echo for the string was not getting checked by the if empty section.
I have never used ob_start before – do I need to capture the string into a variable?
I was not able to put my taxonomy display that checks if a term is present directly into a shortcode.
I suppose $terms is core.
Sorry I am new to this.
January 6, 2021 at 11:48 am #1608471NeilNever mind,
I moved the echo statements into the if section and it appears to be fine
January 6, 2021 at 5:32 pm #1608731ElvinStaffCustomer SupportNice one! Glad you got it sorted.
I have never used ob_start before β do I need to capture the string into a variable?
Here’s a short and nice explanation about it.
https://stackoverflow.com/a/4401992More details about it here:
https://www.php.net/manual/en/function.ob-start.php
https://www.php.net/manual/en/function.ob-get-clean.phpOur brief documentation about shortcode here:
https://docs.generatepress.com/article/creating-a-shortcode/Official WordPress docs about add_shortcode:
https://developer.wordpress.org/reference/functions/add_shortcode/That said, I’d change where you place
return ob_get_clean();
. Put it on the end of the code.Or don’t use it at all. It should work without it. It’s only added for code “good housekeeping”.
Glad you got it sorted. π
-
AuthorPosts
- You must be logged in to reply to this topic.