How to get title for a page/post/product?

#80415
  • Resolved Mattias Fjellvang
    Rank Math free

    I have found this link: https://support.rankmath.com/ticket/code-to-use-meta-title-in-php-markup/ to fetch the post / page / product. It looks very helpful (in fact much better than Yoast), however, it returns an empty long text string: ” ”

        // $product = object(WP_Post)#14336 (24) [....]
        $title = RankMath\Helper::get_settings( "titles.pt_{$product->post_type}_title" ); // "%buy% %customterm(pa_designer)% %title% %page% %sep% %singular_category_name% %materials%"
        dd(RankMath\Helper::replace_vars( $title, $product )); // returns: string(6) "      "

    What am I doing wrong? And how come it doesn’t work?

    I am developing a custom api restpoint where the title has to be returned, so this call is being accessed through wp-json (the rest api of wordpress)

Viewing 1 replies (of 1 total)
  • Michael Davis
    Rank Math pro

    Hello,

    Thank you for contacting Rank Math today.

    You can use the code provided in the referred thread without changing $post to $product and only assign the $post variable to a product id or add n if check to ensure it only affects product pages

    
    if(is_product){
     $title = RankMath\Helper::get_settings( "titles.pt_{$post->post_type}_title" );
    echo RankMath\Helper::replace_vars( $title, $post );
    }
    

    Hope this is helpful to you. Thank you.

    ​​​​​​

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘How to get title for a page/post/product?’ is closed to new replies.