Taxonomy Element: Filter Categories that are shown and Show only Primary Category

Is there a way to show only the primary category in the taxonomy element?

Is there any way to filter only the categories that you want to show in the taxonomy element?

1 Like

How would Bricks (or any function) know which was primary?

The only way to filter categories (apart from with an add-on plugin) is via code. The basics and starter function are here:

You can use: {post_terms_product_cat:2} - but a) you have to style it… and b) it puts a comma on the end… so less than perfect.

There is no primary category unless some plugin is making one, like Yoast. But then you would have to query the database for Yoast value in the post meta table _yoast_wpseo_primary_category. :slight_smile:

I wonder if that works for post categories? I am not using woocommerce.

Thank you!

I know you’re not using wooC… it just happens that the page shows the function you could adapt for your use case…

@macksix - did you ever find a solution for this? I’m in the same boat and trying to return the Primary Category for items in a query loop.

I found a solution for this if anyone is looking and using Yoast.

/**
* Show Primary Category from Yoast
* will allow for Dynamic Data in Bricks query
* {echo:show_yoast_primary_category}
* 
* Note: It is possible that Yoast may change the way that it stores the primary
* category in the future. For now we are relying on this being stored in the
* post meta “_yoast_wpseo_primary_category”.
*
* Source: https://joshuawinn.com/using-yoasts-primary-category-in-wordpress-theme/
*/

function show_yoast_primary_category( $useCatLink = true ) {

	// Retrieves post categories
	$category = get_the_category();

	// If post has a category assigned.
	if ( $category ) {
		$category_display = '';
		$category_link = '';
		
		// Get post's 'Primary' category from post meta
		$yoast_primary_key = get_post_meta( get_the_id(), '_yoast_wpseo_primary_category', TRUE );

		if ( !empty($yoast_primary_key) )
		{
			$term = get_term( $yoast_primary_key );

			if ( is_wp_error($term) ) { 
				// Default to first category (not Yoast) if an error is returned
				$category_display = $category[0]->name;
				$category_link = get_category_link( $category[0]->term_id );
			} else { 
				// Yoast's Primary category
				$category_display = $term->name;
				$category_link = get_category_link( $term->term_id );
			}
		}
		else {
			// Default, display the first category in WP's list of assigned categories
			$category_display = $category[0]->name;
			$category_link = get_category_link( $category[0]->term_id );
		}
    
    if ( !empty($category_display) ){
			if ( $useCatLink == true && !empty($category_link) ){
               echo '<div class="brxe-post-meta">';
               echo '<span class="item">';
               echo '<a href="', esc_url($category_link), '">', esc_html_e($category_display), '</a>';
               echo '</span>';
               echo '</div>';
			} else {
               echo '<div class="brxe-post-meta">';
               echo '<span class="item">';
               echo esc_html_e($category_display);
               echo '</span>';
               echo '</div>';
			}
		}
	}
}

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'show_yoast_primary_category',
  ];
} );```
1 Like

Wow! How to use this code inside Bricks uery loop?