Show only the Primary Category (yoast) for custom taxonomy2019 Community Moderator ElectionWordpress Custom Taxonomies - Conversion and Filtering?What can I use instead of get_the_category() for custom taxonomiesShowing posts using custom taxonomy name - wordpressTaxonomy link not showing in custom admin menu wordpressWordpress: custom walker for taxonomy termsBuilt in taxonomy categories.Display custom taxonomy associated to a specific custom postWordpress - Getting the categories within a custom taxonomySet permalink with parent and child custom categories in WordPressNew Taxonomies and Default Post Category Issue

Be in awe of my brilliance!

The use of "touch" and "touch on" in context

PTIJ: Who should pay for Uber rides: the child or the parent?

Why are there 40 737 Max planes in flight when they have been grounded as not airworthy?

What is a good source for large tables on the properties of water?

Know when to turn notes upside-down(eighth notes, sixteen notes, etc.)

How is the Swiss post e-voting system supposed to work, and how was it wrong?

How do anti-virus programs start at Windows boot?

2D counterpart of std::array in C++17

Why doesn't the EU now just force the UK to choose between referendum and no-deal?

Does this AnyDice function accurately calculate the number of ogres you make unconcious with three 4th-level castings of Sleep?

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

Why using two cd commands in bash script does not execute the second command

Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?

Why are the outputs of printf and std::cout different

Happy pi day, everyone!

Replacing Windows 7 security updates with anti-virus?

PlotLabels with equations not expressions

Can the damage from a Talisman of Pure Good (or Ultimate Evil) be non-lethal?

How to deal with taxi scam when on vacation?

Did CPM support custom hardware using device drivers?

What has been your most complicated TikZ drawing?

Can elves maintain concentration in a trance?

What is IP squat space



Show only the Primary Category (yoast) for custom taxonomy



2019 Community Moderator ElectionWordpress Custom Taxonomies - Conversion and Filtering?What can I use instead of get_the_category() for custom taxonomiesShowing posts using custom taxonomy name - wordpressTaxonomy link not showing in custom admin menu wordpressWordpress: custom walker for taxonomy termsBuilt in taxonomy categories.Display custom taxonomy associated to a specific custom postWordpress - Getting the categories within a custom taxonomySet permalink with parent and child custom categories in WordPressNew Taxonomies and Default Post Category Issue










0















I need to show only the primary category (a function added by default from YOAST) for a custom taxonomy in my template (wordpress).
The taxonomy is taken from the custom "course_category" in learnpress plugin.
It's a taxonomy that adds the categories into the courses created by the plugin.



I would like to create a function to call it in my loops, but i don't know how to write it properly for the custom taxonomies.



I found this code to use for standard categories (NOT FOR CUSTOM TAXONOMIES)



<?php
function get_primary_category( $post = 0 )
if ( ! $post )
$post = get_the_ID();

// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category( $post );
$primary_category = array();
// If post has a category assigned.
if ($category)
$category_display = '';
$category_slug = '';
$category_link = '';
$category_id = '';

if ( class_exists('WPSEO_Primary_Term') )

// Show the post's 'Primary' category, if this Yoast feature is available, & one is set
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id( $post ) );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term))
// Default to first category (not Yoast) if an error is returned
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $category[0]->term_id;

else
// Yoast Primary category
$category_display = $term->name;
$category_slug = $term->slug;
$category_link = get_category_link( $term->term_id );
$category_id = $term->term_id;


else
// Default, display the first category in WP's list of assigned categories
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $term->term_id;

$primary_category['url'] = $category_link;
$primary_category['slug'] = $category_slug;
$primary_category['title'] = $category_display;
$primary_category['id'] = $category_id;


return $primary_category;

?>


which i can call later with



<?php echo '<pre>'.print_r(get_primary_category(get_the_ID()), true).'</pre>'; ?>









share|improve this question
























  • What have you done so far to get it to work with your taxonomy instead of category? If you let us know the problems you ran into when you tried, we can help you solve those. You need to do your own research first and at least make an attempt at doing it... we might not all be familiar with this area so the more you do, the more it helps us to help you :)

    – FluffyKitten
    Mar 6 at 19:27











  • i made some attempts on this For example i used this directly in the loop (without creating a separate function) but i get a weird output (the "if" on error without any category name) gist.github.com/accrane/6334e078e9cc9b140de395f0f197c66d

    – Lorenzo Pimps
    Mar 6 at 19:48











  • So did you use the code in your question or the code you linked to? What exactly did you change in whatever code you were using that gave you the error. I'm also not sure what you mean by "the "if" on error without any category name"... did you get an error message? Or do you mean it triggered the if (is_wp_error($term)) clause? If so ,how do you know it was specifically that line, and did you print out any debugging info to see what values were causing it to fail there?

    – FluffyKitten
    Mar 6 at 20:07











  • Yes, it triggered the error clause. I know it was that line because i tryed also to edit the function without any success. No i didn't debug anything... actually i'm looking other solutions

    – Lorenzo Pimps
    Mar 6 at 20:15











  • The first step for any developer is to debug where the issue is, whether you are going to ask for help or not!! Do some debugging and let us know what is shows, so we can help.

    – FluffyKitten
    Mar 6 at 20:17















0















I need to show only the primary category (a function added by default from YOAST) for a custom taxonomy in my template (wordpress).
The taxonomy is taken from the custom "course_category" in learnpress plugin.
It's a taxonomy that adds the categories into the courses created by the plugin.



I would like to create a function to call it in my loops, but i don't know how to write it properly for the custom taxonomies.



I found this code to use for standard categories (NOT FOR CUSTOM TAXONOMIES)



<?php
function get_primary_category( $post = 0 )
if ( ! $post )
$post = get_the_ID();

// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category( $post );
$primary_category = array();
// If post has a category assigned.
if ($category)
$category_display = '';
$category_slug = '';
$category_link = '';
$category_id = '';

if ( class_exists('WPSEO_Primary_Term') )

// Show the post's 'Primary' category, if this Yoast feature is available, & one is set
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id( $post ) );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term))
// Default to first category (not Yoast) if an error is returned
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $category[0]->term_id;

else
// Yoast Primary category
$category_display = $term->name;
$category_slug = $term->slug;
$category_link = get_category_link( $term->term_id );
$category_id = $term->term_id;


else
// Default, display the first category in WP's list of assigned categories
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $term->term_id;

$primary_category['url'] = $category_link;
$primary_category['slug'] = $category_slug;
$primary_category['title'] = $category_display;
$primary_category['id'] = $category_id;


return $primary_category;

?>


which i can call later with



<?php echo '<pre>'.print_r(get_primary_category(get_the_ID()), true).'</pre>'; ?>









share|improve this question
























  • What have you done so far to get it to work with your taxonomy instead of category? If you let us know the problems you ran into when you tried, we can help you solve those. You need to do your own research first and at least make an attempt at doing it... we might not all be familiar with this area so the more you do, the more it helps us to help you :)

    – FluffyKitten
    Mar 6 at 19:27











  • i made some attempts on this For example i used this directly in the loop (without creating a separate function) but i get a weird output (the "if" on error without any category name) gist.github.com/accrane/6334e078e9cc9b140de395f0f197c66d

    – Lorenzo Pimps
    Mar 6 at 19:48











  • So did you use the code in your question or the code you linked to? What exactly did you change in whatever code you were using that gave you the error. I'm also not sure what you mean by "the "if" on error without any category name"... did you get an error message? Or do you mean it triggered the if (is_wp_error($term)) clause? If so ,how do you know it was specifically that line, and did you print out any debugging info to see what values were causing it to fail there?

    – FluffyKitten
    Mar 6 at 20:07











  • Yes, it triggered the error clause. I know it was that line because i tryed also to edit the function without any success. No i didn't debug anything... actually i'm looking other solutions

    – Lorenzo Pimps
    Mar 6 at 20:15











  • The first step for any developer is to debug where the issue is, whether you are going to ask for help or not!! Do some debugging and let us know what is shows, so we can help.

    – FluffyKitten
    Mar 6 at 20:17













0












0








0








I need to show only the primary category (a function added by default from YOAST) for a custom taxonomy in my template (wordpress).
The taxonomy is taken from the custom "course_category" in learnpress plugin.
It's a taxonomy that adds the categories into the courses created by the plugin.



I would like to create a function to call it in my loops, but i don't know how to write it properly for the custom taxonomies.



I found this code to use for standard categories (NOT FOR CUSTOM TAXONOMIES)



<?php
function get_primary_category( $post = 0 )
if ( ! $post )
$post = get_the_ID();

// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category( $post );
$primary_category = array();
// If post has a category assigned.
if ($category)
$category_display = '';
$category_slug = '';
$category_link = '';
$category_id = '';

if ( class_exists('WPSEO_Primary_Term') )

// Show the post's 'Primary' category, if this Yoast feature is available, & one is set
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id( $post ) );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term))
// Default to first category (not Yoast) if an error is returned
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $category[0]->term_id;

else
// Yoast Primary category
$category_display = $term->name;
$category_slug = $term->slug;
$category_link = get_category_link( $term->term_id );
$category_id = $term->term_id;


else
// Default, display the first category in WP's list of assigned categories
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $term->term_id;

$primary_category['url'] = $category_link;
$primary_category['slug'] = $category_slug;
$primary_category['title'] = $category_display;
$primary_category['id'] = $category_id;


return $primary_category;

?>


which i can call later with



<?php echo '<pre>'.print_r(get_primary_category(get_the_ID()), true).'</pre>'; ?>









share|improve this question
















I need to show only the primary category (a function added by default from YOAST) for a custom taxonomy in my template (wordpress).
The taxonomy is taken from the custom "course_category" in learnpress plugin.
It's a taxonomy that adds the categories into the courses created by the plugin.



I would like to create a function to call it in my loops, but i don't know how to write it properly for the custom taxonomies.



I found this code to use for standard categories (NOT FOR CUSTOM TAXONOMIES)



<?php
function get_primary_category( $post = 0 )
if ( ! $post )
$post = get_the_ID();

// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category( $post );
$primary_category = array();
// If post has a category assigned.
if ($category)
$category_display = '';
$category_slug = '';
$category_link = '';
$category_id = '';

if ( class_exists('WPSEO_Primary_Term') )

// Show the post's 'Primary' category, if this Yoast feature is available, & one is set
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id( $post ) );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term))
// Default to first category (not Yoast) if an error is returned
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $category[0]->term_id;

else
// Yoast Primary category
$category_display = $term->name;
$category_slug = $term->slug;
$category_link = get_category_link( $term->term_id );
$category_id = $term->term_id;


else
// Default, display the first category in WP's list of assigned categories
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $term->term_id;

$primary_category['url'] = $category_link;
$primary_category['slug'] = $category_slug;
$primary_category['title'] = $category_display;
$primary_category['id'] = $category_id;


return $primary_category;

?>


which i can call later with



<?php echo '<pre>'.print_r(get_primary_category(get_the_ID()), true).'</pre>'; ?>






wordpress taxonomy yoast






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 19:28









FluffyKitten

4,84742438




4,84742438










asked Mar 6 at 18:30









Lorenzo PimpsLorenzo Pimps

50111




50111












  • What have you done so far to get it to work with your taxonomy instead of category? If you let us know the problems you ran into when you tried, we can help you solve those. You need to do your own research first and at least make an attempt at doing it... we might not all be familiar with this area so the more you do, the more it helps us to help you :)

    – FluffyKitten
    Mar 6 at 19:27











  • i made some attempts on this For example i used this directly in the loop (without creating a separate function) but i get a weird output (the "if" on error without any category name) gist.github.com/accrane/6334e078e9cc9b140de395f0f197c66d

    – Lorenzo Pimps
    Mar 6 at 19:48











  • So did you use the code in your question or the code you linked to? What exactly did you change in whatever code you were using that gave you the error. I'm also not sure what you mean by "the "if" on error without any category name"... did you get an error message? Or do you mean it triggered the if (is_wp_error($term)) clause? If so ,how do you know it was specifically that line, and did you print out any debugging info to see what values were causing it to fail there?

    – FluffyKitten
    Mar 6 at 20:07











  • Yes, it triggered the error clause. I know it was that line because i tryed also to edit the function without any success. No i didn't debug anything... actually i'm looking other solutions

    – Lorenzo Pimps
    Mar 6 at 20:15











  • The first step for any developer is to debug where the issue is, whether you are going to ask for help or not!! Do some debugging and let us know what is shows, so we can help.

    – FluffyKitten
    Mar 6 at 20:17

















  • What have you done so far to get it to work with your taxonomy instead of category? If you let us know the problems you ran into when you tried, we can help you solve those. You need to do your own research first and at least make an attempt at doing it... we might not all be familiar with this area so the more you do, the more it helps us to help you :)

    – FluffyKitten
    Mar 6 at 19:27











  • i made some attempts on this For example i used this directly in the loop (without creating a separate function) but i get a weird output (the "if" on error without any category name) gist.github.com/accrane/6334e078e9cc9b140de395f0f197c66d

    – Lorenzo Pimps
    Mar 6 at 19:48











  • So did you use the code in your question or the code you linked to? What exactly did you change in whatever code you were using that gave you the error. I'm also not sure what you mean by "the "if" on error without any category name"... did you get an error message? Or do you mean it triggered the if (is_wp_error($term)) clause? If so ,how do you know it was specifically that line, and did you print out any debugging info to see what values were causing it to fail there?

    – FluffyKitten
    Mar 6 at 20:07











  • Yes, it triggered the error clause. I know it was that line because i tryed also to edit the function without any success. No i didn't debug anything... actually i'm looking other solutions

    – Lorenzo Pimps
    Mar 6 at 20:15











  • The first step for any developer is to debug where the issue is, whether you are going to ask for help or not!! Do some debugging and let us know what is shows, so we can help.

    – FluffyKitten
    Mar 6 at 20:17
















What have you done so far to get it to work with your taxonomy instead of category? If you let us know the problems you ran into when you tried, we can help you solve those. You need to do your own research first and at least make an attempt at doing it... we might not all be familiar with this area so the more you do, the more it helps us to help you :)

– FluffyKitten
Mar 6 at 19:27





What have you done so far to get it to work with your taxonomy instead of category? If you let us know the problems you ran into when you tried, we can help you solve those. You need to do your own research first and at least make an attempt at doing it... we might not all be familiar with this area so the more you do, the more it helps us to help you :)

– FluffyKitten
Mar 6 at 19:27













i made some attempts on this For example i used this directly in the loop (without creating a separate function) but i get a weird output (the "if" on error without any category name) gist.github.com/accrane/6334e078e9cc9b140de395f0f197c66d

– Lorenzo Pimps
Mar 6 at 19:48





i made some attempts on this For example i used this directly in the loop (without creating a separate function) but i get a weird output (the "if" on error without any category name) gist.github.com/accrane/6334e078e9cc9b140de395f0f197c66d

– Lorenzo Pimps
Mar 6 at 19:48













So did you use the code in your question or the code you linked to? What exactly did you change in whatever code you were using that gave you the error. I'm also not sure what you mean by "the "if" on error without any category name"... did you get an error message? Or do you mean it triggered the if (is_wp_error($term)) clause? If so ,how do you know it was specifically that line, and did you print out any debugging info to see what values were causing it to fail there?

– FluffyKitten
Mar 6 at 20:07





So did you use the code in your question or the code you linked to? What exactly did you change in whatever code you were using that gave you the error. I'm also not sure what you mean by "the "if" on error without any category name"... did you get an error message? Or do you mean it triggered the if (is_wp_error($term)) clause? If so ,how do you know it was specifically that line, and did you print out any debugging info to see what values were causing it to fail there?

– FluffyKitten
Mar 6 at 20:07













Yes, it triggered the error clause. I know it was that line because i tryed also to edit the function without any success. No i didn't debug anything... actually i'm looking other solutions

– Lorenzo Pimps
Mar 6 at 20:15





Yes, it triggered the error clause. I know it was that line because i tryed also to edit the function without any success. No i didn't debug anything... actually i'm looking other solutions

– Lorenzo Pimps
Mar 6 at 20:15













The first step for any developer is to debug where the issue is, whether you are going to ask for help or not!! Do some debugging and let us know what is shows, so we can help.

– FluffyKitten
Mar 6 at 20:17





The first step for any developer is to debug where the issue is, whether you are going to ask for help or not!! Do some debugging and let us know what is shows, so we can help.

– FluffyKitten
Mar 6 at 20:17












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029944%2fshow-only-the-primary-category-yoast-for-custom-taxonomy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029944%2fshow-only-the-primary-category-yoast-for-custom-taxonomy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко