[Resolved] Showing only one Primary category

Home Forums Support [Resolved] Showing only one Primary category

Home Forums Support Showing only one Primary category

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #1135926
    Ahmed Eid

    Hello,

    How can i display only one post category for each post On page hero
    . We are using Rank Math SEO that create Primary Category.

    Thanks!

    111111.JPG

    #1136469
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You will likely need to create a shortcode with some PHP to get the primary category.

    Let’s try this:

    add_shortcode( 'primary-category', function() {
        $cat = false;
        $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
    
        if ( $primary_cat_id ) {
            $cat = get_term( $primary_cat_id, 'category' );
            $cat_link = get_term_link( $primary_cat_id, 'category' );
        }
        
        if ( ! is_wp_error( $cat ) && ! is_wp_error( $cat_link ) ) {
            return '<span class="primary-category"><a href="' . $cat_link . '">' . $cat->name . '</a></span>';
        }
    } );

    Then add the [primary-category] shortcode into the Hero.

    Let me know ๐Ÿ™‚

    #1136712
    Ahmed Eid

    Thanks for the help “Tom”
    Unfortunately the shortcode does not work

    #1137053
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Does nothing output at all?

    What if you do this?:

    add_shortcode( 'primary-category', function() {
        $product_cat = false;
        $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
    
        if ( $primary_cat_id ) {
            $product_cat = get_term( $primary_cat_id, 'product_cat' );
        }
    
        var_dump($product_cat);
    
        if ( $product_cat ) {
            return '<span class="primary-category">' . $product_cat->name . '</span>';
        }
    } );
    #1137084
    Ahmed Eid

    This error occurs

    object (WP_Error) # 5759 (2) {["errors"] => array (1) {["invalid_taxonomy"] => array (1) {[0] => string (25) "invalid class." }} ["error_data"] => array (0) {}}

    #1137319
    Tom
    Lead Developer
    Lead Developer
    #1137784
    Ahmed Eid

    Great works well
    But it cannot be clicked as a link
    Also
    But css cannot be implemented

    #1138269
    Tom
    Lead Developer
    Lead Developer

    I just updated the function to include the link: https://generatepress.com/forums/topic/showing-only-one-primary-category/#post-1136469

    You can use this class to style it: .primary-category

    #1138362
    Ahmed Eid

    Sorry Tom
    But the error appears
    PHP Recoverable fatal error: Object of class WP_Error could not be converted to string
    On the next line:
    return '<span class="primary-category"><a href="' . $cat_link . '">' . $cat->name . '</a></span>';

    #1138546
    Tom
    Lead Developer
    Lead Developer

    Hmm, one more debug try:

    add_shortcode( 'primary-category', function() {
        $cat = false;
        $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
    
        if ( $primary_cat_id ) {
            $cat = get_term( $primary_cat_id, 'category' );
            $cat_link = get_term_link( $primary_cat_id, 'category' );
        }
    
        var_dump($cat_link);
        
        if ( $cat ) {
            return '<span class="primary-category"><a href="#">' . $cat->name . '</a></span>';
        }
    } );

    What does that var_dump output? It should get the link to the term..

    Let me know ๐Ÿ™‚

    #1138607
    Ahmed Eid

    Unfortunately, this error occurred

    object (WP_Error) # 5775 (2) {["errors"] => array (1) {["invalid_term"] => array (1) {[0] => string (18) "is an empty element." }} ["error_data"] => array (0) {}}

    #1139307
    Tom
    Lead Developer
    Lead Developer

    Ok, can you try this?: https://generatepress.com/forums/topic/showing-only-one-primary-category/#post-1136469

    If that doesn’t work I’ll install Rank Math and will debug.

    Thanks!

    #1139427
    Ahmed Eid

    Sorry, Tom, you are tired of me
    It didn’t work at all

    #1139706
    Tom
    Lead Developer
    Lead Developer

    No worries!

    Just played with it – this should work:

    add_shortcode( 'primary-category', function() {
        $cat = false;
        $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
    
        if ( $primary_cat_id ) {
            $cat = get_term( $primary_cat_id, 'category' );
            $cat_link = get_term_link( $cat, 'category' );
        }
    
        if ( ! is_wp_error( $cat ) ) {
            return '<span class="primary-category"><a href="' . $cat_link . '">' . $cat->name . '</a></span>';
        }
    } );
    #1139759
    Ahmed Eid

    Thank you Tom
    It works perfectly
    good job

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