• I am using custom taxonomies that I created manually. I can make the list of taxonomies and terms appear above or bellow a post but not in it. I am playing with the idea of using shortcodes to add the tax terms within the post but I am not sure how to go about it. For example, adding [taxonomy] to the middle of a post and having all the post’s tax info appear right there!

    Currently, my tax info is displayed like so:

    <dl class="my-stats">
    <?php
    
    $terms_as_text = get_the_term_list( $post->ID, 'gender', '<dt>Gender:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    $terms_as_text = get_the_term_list( $post->ID, 'age', '<dt>Age:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    the_terms( $post->ID, 'location', '<dt>Loc:&nbsp</dt><dd>', ', ', '</dd>' );
    
    $terms_as_text = get_the_term_list( $post->ID, 'status', '<dt>Status:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    the_terms( $post->ID, 'contact', '<dt>Contact:&nbsp</dt><dd>', ' or ', '</dd>' );
    
    ?>
    </dl>
    
    <?php the_content(); ?>
Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Seijun

    (@seijun)

    Ok, I have the shortcode part figured out, but I don’t know how to make the shortcode use a block of php without getting errors. Here is my shortcode code:

    function get_tax() {
    	return 'php code here';
    }
    add_shortcode('tax', 'get_tax');

    And this is the php code I want the shortcode to execute (displays a list of taxonomy terms associated with the post):

    <dl class="my-stats">
    
    $terms_as_text = get_the_term_list( $post->ID, 'gender', '<dt>Gender:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    $terms_as_text = get_the_term_list( $post->ID, 'age', '<dt>Age:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    the_terms( $post->ID, 'location', '<dt>Loc:&nbsp</dt><dd>', ', ', '</dd>' );
    
    $terms_as_text = get_the_term_list( $post->ID, 'status', '<dt>Status:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    the_terms( $post->ID, 'contact', '<dt>Contact:&nbsp</dt><dd>', ' or ', '</dd>' );
    
    </dl>

    Can anyone help, please?

    Thread Starter Seijun

    (@seijun)

    Ok, I’ve been working on this some more, and I’ve got the shortcode to render the php, but it’s still putting the output ABOVE the post and not in it. Please, PLEASE help me out if you can. My code is as follows:

    function get_tax() {
    
    $terms_as_text = get_the_term_list( $post->ID, 'gender', '<dl class="my-stats"><dt>Gender:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    $terms_as_text = get_the_term_list( $post->ID, 'age', '<dt>Age:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    the_terms( $post->ID, 'location', '<dt>Loc:&nbsp</dt><dd>', ', ', '</dd>' );
    
    $terms_as_text = get_the_term_list( $post->ID, 'status', '<dt>Status:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    echo strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    the_terms( $post->ID, 'contact', '<dt>Contact:&nbsp</dt><dd>', ' or ', '</dd></dl>' );
    
    }
    add_shortcode('tax', 'get_tax');

    You need to return the data, not echo it..

    Your callback function should store the data in a variable and return that data, else it’s not passing back the data…

    Alternative for the_terms..
    https://codex.www.ads-software.com/Function_Reference/get_the_terms

    Thread Starter Seijun

    (@seijun)

    I replaced echo with return and replaced the_terms with get_the_terms. After replacing echo with return the output is now within the post, but only shows the first taxonomy in the list (gender:female)

    function get_tax() {
    
    $terms_as_text = get_the_term_list( $post->ID, 'gender', '<dl class="my-stats2"><dt>Gender:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    return strip_tags($terms_as_text, '<dl><dt>,<dd>,&nbsp');
    
    $terms_as_text = get_the_term_list( $post->ID, 'age', '<dt>Age:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    return strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    get_the_terms( $post->ID, 'location', '<dt>Loc:&nbsp</dt><dd>', ', ', '</dd>' );
    
    $terms_as_text = get_the_term_list( $post->ID, 'status', '<dt>Status:&nbsp</dt><dd>', ', ', '</dd>' ) ;
    return strip_tags($terms_as_text, '<dt>,<dd>,&nbsp');
    
    get_the_terms( $post->ID, 'contact', '<dt>Contact:&nbsp</dt><dd>', ' or ', '</dd></dl>' );
    
    }
    add_shortcode('tax', 'get_tax');

    It’s not moving on the the second item in the list. I’m guessing there is something else I need to separate each “get_the_terms” statement, but I have no idea what.

    Once you return the function stops executing, that’s the end of the call to the function.

    You need to store the data as you go along, then return it at the end…

    function get_tax() {
    	global $post;
    	$tax_and_terms = '';
    	$tax_and_terms .= get_the_term_list( $post->ID, 'gender', 'Gender: ', ', ', '<br />' ) ;
    	$tax_and_terms .= get_the_term_list( $post->ID, 'age', 'Age: ', ', ', '<br />' ) ;
    	$tax_and_terms .= get_the_term_list( $post->ID, 'location', 'Loc: ', ', ', '<br />' );
    	$tax_and_terms .= get_the_term_list( $post->ID, 'status', 'Status: ', ', ', '<br />' ) ;
    	$tax_and_terms .= get_the_term_list( $post->ID, 'contact', 'Contact: ', ' or ', '<br />' );
    	return $tax_and_terms;
    }
    add_shortcode('tax', 'get_tax');

    Not sure why you were adding HTML, only to then strip it out.. so i removed definition list code and switched all the calls over to get_the_term_list() (easy to add HTML back into if you want to)..

    Hope that helps… ??

    Thread Starter Seijun

    (@seijun)

    Thanks, that helped a lot!
    I need the strip_tags though to remove the url that is attached to every term. I only want the terms for “loc.” and “contact” to be links. Strip_tags was removing my dl/dt tags as well so I had those added back in. How do I add strip_tags back to the new code?

    Wrap the appropriate get_the_term_list calls with strip_tags..

    For example:

    $tax_and_terms .= strip_tags( get_the_term_list( $post->ID, 'gender', 'Gender: ', ', ', '<br />' ) );

    Thread Starter Seijun

    (@seijun)

    Thanks. And for allowing certain tags? This seemed logical, but didn’t work.
    $tax_and_terms .= strip_tags( get_the_term_list( $post->ID, 'gender' <dl><dt>Gender: ', ', ', '</dt>' )$tax_and_terms, '<dl><dt>,<dd>,&nbsp' );

    You seem to have accumulated some extra code, there’s an extra variable in there, try this..

    $tax_and_terms .= strip_tags( get_the_term_list( $post->ID, 'gender' <dl><dt>Gender: ', ', ', '</dt>' ), '<dl><dt><dd>' );

    ??

    Thread Starter Seijun

    (@seijun)

    You are awesome!
    Thank you so much, you have made my life infinitely easier today.
    For anyone interested, my final code is

    function get_tax() {
    	global $post;
    	$tax_and_terms = '';
            $tax_and_terms .= strip_tags( get_the_term_list( $post->ID, 'gender', '<dl><dt>Gender: </dt><dd>', ', ', '</dd>' ), '<dl><dt><dd>' );
            $tax_and_terms .= strip_tags( get_the_term_list( $post->ID, 'age', '<dt>Age: </dt><dd>', ', ', '</dd>' ), '<dl><dt><dd>' );
    	$tax_and_terms .= get_the_term_list( $post->ID, 'location', '<dt>Loc: </dt><dd>', ', ', '</dd>' );
            $tax_and_terms .= strip_tags( get_the_term_list( $post->ID, 'status', '<dt>Status: </dt><dd>', ', ', '</dd>' ), '<dl><dt><dd>' );
    	$tax_and_terms .= get_the_term_list( $post->ID, 'contact', '<dt>Contact: </dt><dd>', ' or ', '</dd></dl>' );
    	return $tax_and_terms;
    }
    add_shortcode('tax', 'get_tax');

    Happy to help… ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Display custom taxonomy output within posts’ is closed to new replies.