Yoast Primary Category

Yoast has a function of setting a primary category. Is there a way to pull that into a loop?

For context, this is the code I currently have. It pulls the first term alphabetically. Instead, I would like it only pull the primary category.

<Taxonomy project_type count=1><em><a href="{Term url}"><Term title /></a></em></Taxonomy>

Turns out the thread I shared on your Facebook post was pretty close, you just need to append the name of the category to the end of _yoast_wpseo_primary_. So for the built-in categry taxonomy, you could display the term ID using <Field _yoast_wpseo_primary_category /> on a post where you’ve set a primary category. You could then pull that term ID into a taxonomy term loop if you wanted to display the term’s title.

Does this seem right? It doesn’t show anything so I’m sure I’ve done something wrong here. <Field _yoast_wpseo_primary_project_type /> does produce a number :+1:

<Set name=term_id>
  <Field _yoast_wpseo_primary_project_type />
</Set>

<Taxonomy id="{Get term_id}"><em><a href="{Term url}"><Term title /></a></em></Taxonomy>

I figured it out, it still needed the taxonomy type. Full code below for anyone looking.

<Set name=term_id>
  <Field _yoast_wpseo_primary_project_type />
</Set>

<Taxonomy project_type id="{Get term_id}"><em><a href="{Term url}"><Term title /></a></em></Taxonomy>

Thank you Benjamin!!

1 Like

Nice! If you wanted, you could also write it without the extra step of saving a variable:

<Taxonomy project_type id="{Field _yoast_wpseo_primary_project_type}"><em><a href="{Term url}"><Term title /></a></em></Taxonomy>