[Resolved] Display label and taxonomy in custom Elements Header if there is a value

Home Forums Support [Resolved] Display label and taxonomy in custom Elements Header if there is a value

Home Forums Support Display label and taxonomy in custom Elements Header if there is a value

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1607156
    Neil

    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

    #1607436
    Elvin
    Staff
    Customer Support

    Hi,

    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 do get_the_term_list() instead. πŸ™‚

    A wise man once said:
    "Have you cleared your cache?"

    #1608393
    Neil

    Hi 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();
    }
    } );

    #1608423
    Neil

    Hi 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.

    #1608471
    Neil

    Never mind,

    I moved the echo statements into the if section and it appears to be fine

    #1608731
    Elvin
    Staff
    Customer Support

    Nice 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/4401992

    More details about it here:
    https://www.php.net/manual/en/function.ob-start.php
    https://www.php.net/manual/en/function.ob-get-clean.php

    Our 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. πŸ™‚

    A wise man once said:
    "Have you cleared your cache?"

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.