Site logo

Archived topic

Limit Dynamic Term Output to 1

11 replies · Started by Matt Stern on January 11, 2022

Viewing posts 1–12 of 12

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.

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?

Hi Elvin,

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

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

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. :)

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

Same issue I'm afraid.

Hi there,

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

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!

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

Hi Elvin, that worked! Many thanks.

No problem. :D

This archived topic is closed to new replies.