Set sub term as Primary selection

#80403
  • Resolved Edro
    Rank Math free

    Hi, Need help with Primary select term options at the moment we have costume locations set up like( All > Eu > Uk ) now on the post it shows option All is as selected location and in the snippets it shows EU as location I want the the sub term Uk to be selected as the main_term,
    Any one knows what to add to this filter to make the child term selected as Primary term please?
    /**
    * Filter to change the primary term output of the breadcrumbs class.
    *
    * @param WP_Term $term Primary term.
    * @param array $terms Terms attached to the current post.
    */
    add_filter( ‘rank_math/frontend/breadcrumb/main_term’, function( $current_term, $term ) {
    return $current_term;
    }, 10, 2 );

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    Thank you for contacting Rank Math today.

    When you have more than 1 category selected, please choose the one you want to show in the breadcrumbs as “Make term primary”

    img

    Make sure Primary Category is also select from the drop-down in WordPress Dashboard > Rank Math > Titles & Meta > Products > Product Taxonomy.

    If yu still wish to use the code, you can use it as follows:

    
    add_filter( 'rank_math/frontend/breadcrumb/main_term', function( $current_term, $terms ) { 
        global $post;
            $terms = get_the_terms( $post, 'category' );
            if ( ! empty( $terms ) ) {
                return $terms[0];
            }
        return $current_term;
    }, 10, 2 );
    

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    ​​​​​​

    Edro
    Rank Math free

    Thanks for your answer @Michael Davis, it really works, but like in the example you have provided the parent category is selected by default, and I want the last child to be selected by default, (Hoodies) in the picture you provided not (Clothing), any suggestions?

    Alberto
    Rank Math business

    Hello,

    Just to be sure, do you mean you want whenever you add a new product, to have that children selected as primary? Or not only with new products but with all the ones already added?

    Looking forward to help you.

    Edro
    Rank Math free

    Hi, Yes I mean every time a new product added the last child is selected as primary
    Thanks @Alberto

    Edro
    Rank Math free

    Managed to fix it with a new variable for new and old posts, copied and edited this function, Thanks

    add_action( ‘rank_math/vars/register_extra_replacements’, function() {

    rank_math_register_var_replacement(
    ‘childCat’,
    [
    ‘name’ => esc_html__( ‘last child Category’, ‘rank-math’ ),
    ‘description’ => esc_html__( ‘Category last child’, ‘rank-math’ ),
    ‘variable’ => ‘childCat’,
    ‘example’ => MY FILTER NAME(),
    ],
    ‘MY FILTER NAME’
    );

    Hello,

    We are glad you were able to find a way to fix your issue.

    Do you wish to close this thread as resolved?

    If you have any further question(s), please let us know. Thank you.

    Edro
    Rank Math free

    Thank you for your time.

    Hello,

    We are super happy that your issue is resolved.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)

The ticket ‘Set sub term as Primary selection’ is closed to new replies.