[Resolved] Limit Dynamic Term Output to 1

Home Forums Support [Resolved] Limit Dynamic Term Output to 1

Home Forums Support Limit Dynamic Term Output to 1

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2077416
    Matt Stern

    Hi Guys,

    I’ve got a content template block element that outputs category terms above the post title. Can you suggest a way to only display a single term (the first one)? Instead of showing all the terms associated with a given post.

    Url for reference: https://4sternstaging.com/

    Screenshot clarification: https://nimb.ws/v4pkUV

    Thanks kindly.

    #2077426
    Elvin
    Staff
    Customer Support

    Hi Matt,

    Will the single term to be displayed be purely whatever the first term is?

    Or perhaps, do you have some sort of a pre-determined primary term to be single displayed?

    if it’s a pre-determined, primary term (ie. like how yoast assigns a primary category), can you share with us how your site determines which one is the primary term to be displayed?

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

    #2077434
    Matt Stern

    Hi Elvin,

    Let’s just go with whatever the first term is. There isn’t a predetermined term at this point.

    #2077484
    Elvin
    Staff
    Customer Support

    I see,

    In that case we can make a shortcode you can use on a shortcode block that displays only the first term of the current post.

    Here’s the shortcode: [display_single_category_term]

    here’s the snippet that makes the shortcode.

    add_shortcode( 'display_single_category_term', function() {
    	$termslist = get_the_term_list( get_the_ID(), 'category', '', ', ');
    	$terms = get_the_terms( get_the_ID(), 'category');
    	$term_link = get_term_link( (int) $terms[0]->term_id, 'category');
    	$term_name = $terms[0]->name;
        ob_start();
        // Start your PHP below
      
        echo '<a href="'.$term_link.'" rel="category" class="custom-single-category">'.$term_name.'</a>';
      
        // End your PHP above
        return ob_get_clean();
    } );

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

    #2077497
    Matt Stern

    That did work to output a single term, but now if I try to edit the block element, I get a “Critical Error” page. It’s not a huge deal, I can just deactivate the snippet to edit the element, but it would be nice to fix since it’s a client site. ๐Ÿ™‚

    #2077501
    Elvin
    Staff
    Customer Support

    Can you try and see what happens if the snippet is modified to this?

    add_shortcode( 'display_single_category_term', function() {
    
        ob_start();
        // Start your PHP below
      	$termslist = get_the_term_list( get_the_ID(), 'category', '', ', ');
    	$terms = get_the_terms( get_the_ID(), 'category');
    	$term_link = get_term_link( (int) $terms[0]->term_id, 'category');
    	$term_name = $terms[0]->name;
        echo '<a href="'.$term_link.'" rel="category" class="custom-single-category">'.$term_name.'</a>';
      
        // End your PHP above
        return ob_get_clean();
    } );

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

    #2077508
    Matt Stern

    Same issue I’m afraid.

    #2078162
    David
    Staff
    Customer Support

    Hi there,

    do you have any more info from error_logs as to what the Critical Error is ?

    #2078248
    Matt Stern

    Hi David,

    Here’s some info from a debug plugin log:

    Notice: Trying to access array offset on value of type bool in /home/sterndes/public_html/4sternstaging.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code on line 7

    Notice: Trying to get property ‘term_id’ of non-object in /home/sterndes/public_html/4sternstaging.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code on line 7

    Notice: Trying to access array offset on value of type bool in /home/sterndes/public_html/4sternstaging.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code on line 8

    Notice: Trying to get property ‘name’ of non-object in /home/sterndes/public_html/4sternstaging.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code on line 8

    Fatal error: Uncaught Error: Object of class WP_Error could not be converted to string in /home/sterndes/public_html/4sternstaging.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code:9 Stack trace: #0 /home/sterndes/public_html/4sternstaging.com/wp-includes/shortcodes.php(356): {closure}(”, ”, ‘display_single_…’) #1 [internal function]: do_shortcode_tag(Array) #2 /home/sterndes/public_html/4sternstaging.com/wp-includes/shortcodes.php(228): preg_replace_callback(‘/\\[(\\[?)(displa…’, ‘do_shortcode_ta…’, ‘

    apply_filters(‘<div class=”gb-…’, Array) #5 /home/sterndes/public_html/4sternstaging.com/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(1792): apply_filters(‘the_content’, ‘<!– wp:generat…’) #6 /home/sterndes/public_html/4sternstaging.com/wp-in in /home/sterndes/public_html/4sternstaging.com/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code on line 9

    Thank you!

    #2078609
    Elvin
    Staff
    Customer Support

    I see.

    It was working with a Hook element but let’s simplify it a bit more to work on the Block editor.

    Try this one:

    add_shortcode( 'display_single_category_term', function() {
    
        ob_start();
        // Start your PHP below
    	
    	$category = get_the_category();
    
    	if ( ! empty( $category ) ) {
    		echo '<a href="'.esc_url( get_category_link( $category[0]->term_id ) ).'">'.$category[0]->cat_name.'</a>';
    	}
      
        // End your PHP above
        return ob_get_clean();
    } );

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

    #2078710
    Matt Stern

    Hi Elvin, that worked! Many thanks.

    #2078715
    Elvin
    Staff
    Customer Support

    No problem. ๐Ÿ˜€

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

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