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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved