Site logo

[Support request] Get Yoast primary category in shortcode for custom post type?

Home Forums Support [Support request] Get Yoast primary category in shortcode for custom post type?

Home Forums Support Get Yoast primary category in shortcode for custom post type?

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #2451519
    Cheryl

    Hello,

    In another post somewhere here, I found this:

    add_shortcode( 'display_category', function() {
    	
    	// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
    $category = get_the_category();
    	
    // If post has a category assigned.
    if ($category){
    	$category_display = '';
    	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() );
    		$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;
    		} else { 
    			// Yoast Primary category
    			$category_display = $term->name;
    		}
    	} 
    	else {
    		// Default, display the first category in WP's list of assigned categories
    		$category_display = $category[0]->name;
    	}
    
    	// Display category
    		return $category_display;
    
    }
    	
    } );

    …which works great to get the post category name. What is the right way to modify this to get the primary category name of a custom post type?
    For instance if I have a category for events called event-category, how would I modify this to get the primary event-category?

    Thanks

    #2451729
    David
    Staff
    Customer Support

    Hi there,

    probably like this:

    
    add_shortcode( 'display_category', function() {
        // set your taxonomy name
        $tax_name = 'your_tax_name';
        // SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
        $category = get_terms( array(
            'taxonomy' => $tax_name,
        ) );
    
        // If post has a category assigned.
        if ($category){
            $category_display = '';
            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( $tax_name , get_the_id() );
                $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;
                } else { 
                    // Yoast Primary category
                    $category_display = $term->name;
                }
            } 
            else {
                // Default, display the first category in WP's list of assigned categories
                $category_display = $category[0]->name;
            }
    
            // Display category
            return $category_display;
    
        }
    
    } );

    Just update this variable: $tax_name = 'your_tax_name';

    #2451844
    Cheryl

    Thanks! That works great.
    I’m also using:

    /*dynamic category link shortcode*/
    function dynamic_category_link_function( $atts ) {
        ob_start();
    
        $categories = get_the_category();
        $category_link = '';
    
        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() );
            $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_link = get_category_link( $categories[0]->term_id );
            } else {
                // Yoast Primary category
                $category_link = get_category_link( $term->term_id );
            }
        } else {
            // Default, display the first category in WP's list of assigned categories
            $category_link = get_category_link( $categories[0]->term_id );
        }
    
        echo $category_link;
    
    	return ob_get_clean();
    }
    add_shortcode( 'dynamic_category_link', 'dynamic_category_link_function' );

    to get the category link for regular posts. But I’m having difficulty with modifying it to get the dynamic category link for custom post types. Any suggestions there?
    Thanks

    #2451923
    Cheryl

    …Though I’m finding that at least with the event category, if there isn’t a “primary” category assigned, somehow, it’s fetching an entirely unrelated category.

    #2452264
    Fernando
    Customer Support

    Hi Cheryl,

    That should work for Custom Post types as well. I also tested it on my test site, and it’s working. It’s getting the Primary Category Link.

    Or, are you referring to something different when you say “Dynamic Category Link”?

    What “event” category is this? I believe Yoast should select a Primary Category by default. It shouldn’t be unassigned.

    #2452350
    Cheryl

    It works fine for regular posts, but on custom events, the link is to the page itself. I was using events as an example. I have WP Events Manager and was trying to get the dynamic link for the event category (the code that David posted previously works fine for getting the category name). But the behavior is the same for other custom post types.

    In the case of the primary category, there’s a link when you’re selecting a category to “make primary.” At least for events I found that I had an event for which I hadn’t selected the primary category and it showed a completely unrelated category until I manually chose the primary category. Then it was fine. Note that I had categorized it, just hadn’t selected any category as “primary.”

    Thanks

    #2452358
    Fernando
    Customer Support

    I see. that’s odd. It should work for Custom Post Types.

    Can you provide the link to a specific “event” where the issue is evident?

    You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2452368
    Cheryl

    OK, I added a link in the private field. Here’s what I have in the header I’m setting up, where [display_event_category] is my revision of David’s snippet above to get the name of the event category.

    [display_event_category]

    But for categories, I have something like “Arts” and “Theater” selected, and as you can see the category it’s getting is Agricultural Shows & Tours which isn’t even selected as a category. I think it’s choosing the first category alphabetically if I don’t have a primary selected. If I go in and click the “Make Primary” link in the editor for the “Theater” category, then it will show that category.

    As for the link, the [dynamic_category_link] works great for regular posts, but here and other custom post types it’s returning the URL of the page itself.

    Thanks

    #2452425
    Fernando
    Customer Support

    I see. Let’s try to tackle the link first.

    Are you using a different code for the link?

    Can you try this exact code first?: https://generatepress.com/forums/topic/get-yoast-primary-category-in-shortcode-for-custom-post-type/#post-2451844

    It should just echo out the URL.

    #2453325
    Cheryl

    Hi,
    That redirects me back to the code I entered above. That is the exact code I’m using to get the link for the category. For that I only have the one. I customized the other code for specific custom post types.
    Thanks

    #2453854
    Fernando
    Customer Support

    I see. Can you provide admin login credentials so we can take a closer look at your setup?

    Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2514505
    Jacopo

    Hi David, Fernando,

    thanks for your amazing support! I have a similar problem: I would like to show only the primary taxonomy “breeds” from Yoast for the custom post type “dogs”. On the frontend I would like the primary taxonomy to be clickable and link back to its corresponding taxonomy repository.

    I tried the code as it is now but it doesn’t work.

    #2514701
    Ying
    Staff
    Customer Support

    Hi Jacopo,

    I would like to show only the primary taxonomy “breeds” from Yoast for the custom post type “dogs”.

    Does Yoast SEO have the primary taxonomy option? I don’t know about that.
    Can you show us the settings by taking a screenshot?

    #2514840
    Jacopo

    Hi,

    Of course Yoast has it, just like any categories 🙂

    #2514850
    Leo
    Staff
    Customer Support

    Hi there,

    Would you be able to check with Yoast’s support team for this request as it’s specifically related to their product?

    The questions here are very out of the scope of what we can support here:
    https://generatepress.com/what-support-includes/

    If Yoast’s support team cannot help then a WP general forum like this might be your best bet:
    https://wordpress.stackexchange.com/

    Thanks for your understanding 🙂

Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.