- This topic has 11 replies, 3 voices, and was last updated 2 years, 3 months ago by Elvin.
-
AuthorPosts
-
January 11, 2022 at 9:38 pm #2077416Matt 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.
January 11, 2022 at 9:52 pm #2077426ElvinStaffCustomer SupportHi 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?
January 11, 2022 at 10:10 pm #2077434Matt SternHi Elvin,
Let’s just go with whatever the first term is. There isn’t a predetermined term at this point.
January 11, 2022 at 11:45 pm #2077484ElvinStaffCustomer SupportI 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(); } );
January 12, 2022 at 12:01 am #2077497Matt SternThat 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. ๐
January 12, 2022 at 12:06 am #2077501ElvinStaffCustomer SupportCan 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(); } );
January 12, 2022 at 12:17 am #2077508Matt SternSame issue I’m afraid.
January 12, 2022 at 8:39 am #2078162DavidStaffCustomer SupportHi there,
do you have any more info from error_logs as to what the Critical Error is ?
January 12, 2022 at 9:45 am #2078248Matt SternHi 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 9Thank you!
January 12, 2022 at 5:45 pm #2078609ElvinStaffCustomer SupportI 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(); } );
January 12, 2022 at 10:58 pm #2078710Matt SternHi Elvin, that worked! Many thanks.
January 12, 2022 at 11:12 pm #2078715ElvinStaffCustomer SupportNo problem. ๐
-
AuthorPosts
- You must be logged in to reply to this topic.