• Resolved dagan

    (@daganloygmailcom)


    Fantastic plugin by the way – I am just having one issue.

    I have a custom taxonomy(listings) I use for city’s. I would like to include the custom taxonomy city into the title of my custom taxonomy post.

    Is this possible? Would I use %customfield(field-name)% somehow? Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter dagan

    (@daganloygmailcom)

    Further testing, I got it to kind-of-work. I understand now that custom field shows the customfields from the post/page within wordpress editing page. However, I put in city for my field name so %customfield(city)% but problem is it displays the ID number of the city, instead of the name of the city. Is there a way to display the customfield name instead of ID?

    Thread Starter dagan

    (@daganloygmailcom)

    Further further testing, I figured out that my custom field generated from my theme uses ID’s there. Meaning customfield works correct. Onto my final question, is there a way to use custom taxonomy of the post into the title. Maybe include a way to use php in the title through Rank Math? This would make it much more flexible.

    Plugin Author Rank Math SEO

    (@rankmath)

    Hello @daganloygmailcom

    Thank you for contacting the support.

    Yes, you are right, the custom field shows the custom field value of the current post.

    It seems like the Term ID is stored in the city custom field. Please check if the city name is also stored in any custom field or you can use the following filter code to show the taxonomy term name in the title:

    add_filter( 'rank_math/frontend/title', function( $title ) {
        if ( is_singular() ) {
            global $post;
            $terms = wp_get_post_terms( $post->ID, 'listings' ); // Please change listings with your taxonomy name.
            if ( ! empty( $terms ) ) {
                return $terms[0]->name;
            }
        }
    
      return $title;
    });

    You can find the list of all the available filters and hooks in this KB:
    https://rankmath.com/kb/filters-hooks-api-developer/

    Hope that helps. If you have any further question(s), please let us know.

    Thread Starter dagan

    (@daganloygmailcom)

    Thanks for your help, the filter code semi-works, however it replaces the ENTIRE title with only the term. Can it only replace the %customfield(city)% part instead of the entire title? Now it just says the city name as the title and that’s it.

    Plugin Author Rank Math SEO

    (@rankmath)

    Hello @daganloygmailcom

    Instead of using %customfield(city)% please use some other placeholder like %city%. You can then replace it using str_replace in the functions.php file:

    add_filter( 'rank_math/frontend/title', function( $title ) {
        if ( is_singular() ) {
            global $post;
            $terms = wp_get_post_terms( $post->ID, 'listings' ); // Please change listings with your taxonomy name.
            if ( ! empty( $terms ) ) {
               $title = str_replace( '%city%', $terms[0]->name, $title );
            }
        }
    
      return $title;
    });

    Hope this helps. Thank you.

    Thread Starter dagan

    (@daganloygmailcom)

    I just want to say I really appreciate your help. I wasn’t able to work on it until today. It’s not replacing the string though. I reverted back to your original code to see if it was maybe our server move but the old code works. It’s not replacing the string for some reason. I double checked, this line doesn’t work:
    $title = str_replace( ‘%ecity%’, $terms[0]->name, $title );

    Thread Starter dagan

    (@daganloygmailcom)

    I could be wrong but I think it’s because $title is already rendered prior to this function being able to replace the %city% – meaning there is no %city% to replace as Rank Math’s plugin removes it before it outputs.

    Thread Starter dagan

    (@daganloygmailcom)

    I figured it out a workaround; Because Rank Math removes any %tag% strings regardless if they are valid or not, I just used a specific string without %, and it works perfect! Appreciate your help guys! Love the plugin, it’s really helped my SEO so far and I am sure this will help boost it!

    Plugin Author Rank Math SEO

    (@rankmath)

    Hello @daganloygmailcom

    Did you try with the following code?
    $title = str_replace( '%city%', $terms[0]->name, $title );

    Instead of
    $title = str_replace( '%ecity%', $terms[0]->name, $title );

    Looking forward to hearing from you soon.

    Thank you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Taxonomy in Title?’ is closed to new replies.