Custom image (with custom field) for taxonomy and category

#783853
  • Hi dear. i use custom field to show image on my wordpress posts
    For posts i use this code (found here on an old ticket)

    add_action('wp_head','banner_image_opengraph');
    function banner_image_opengraph() {
    if(get_post_type() == 'post') { //name of the post type
    $attachment_url = get_post_meta(get_the_ID(),'rehub_offer_product_thumb', true); //name of the custom field
    echo "<meta property='og:image' content=$attachment_url>";
    }
    }

    The preview seo image on categories and custom taxonomies now it is related to the last post in the category or taxonomy (it is shown as a preview of the SEO image of the last post) and is not the image of the category or taxonomy

    Thanks in advance for the support

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

    Thank you for reaching out to us with your concern about setting a custom image for taxonomy and category pages in WordPress. To display a custom image for categories and taxonomies, you can use Rank Math’s filter hooks to customize the Open Graph image.

    You can achieve this by adding the following code snippet to your theme’s functions.php file or a custom plugin:

    add_action('wp_head', 'custom_taxonomy_image_opengraph');
    function custom_taxonomy_image_opengraph() {
        if (is_category() || is_tax()) {
            $term_id = get_queried_object_id();
            $attachment_url = get_term_meta($term_id, 'custom_taxonomy_image_field', true); // Replace 'custom_taxonomy_image_field' with the name of your custom field
            if ($attachment_url) {
                echo "";
            }
        }
    }

    In this code snippet:

    • is_category() and is_tax() check if the current page is a category or taxonomy page.
    • get_queried_object_id() retrieves the ID of the current category or taxonomy term.
    • get_term_meta() fetches the value of the custom image field for the term.
    • If a custom image is found, it will be added as the Open Graph image for the category or taxonomy page.

    Please replace 'custom_taxonomy_image_field' with the actual name of your custom field that stores the image URL for categories and taxonomies.

    If you encounter any issues or need further assistance, feel free to ask. We are here to help!

    Kind regards.

    Sources:
    Open Graph Meta Tags » Rank Math
    Using Variables in the SEO Title and Description » Rank Math
    Open Graph/Social Preview Image Is Not Displayed » Rank Math
    How to Make Your Theme Rank Math Compatible » Rank Math
    Rank Math ‘Titles and Meta’ Options and Settings

    This is an automated reply from Rank Math Support Bot. Your ticket has been assigned to a human agent and we will get back to you shortly.

    Hi dear
    Thanks for the support
    i Try to put code and i’ll try if work or not

    Hi dear, i’m here again.
    The code does not work. Category and custom taxonomy shows last image post, again!

    Hello,

    We’ve checked the category, and the og:image meta tag is not coming from Rank Math.

    Assuming you are using the latest version of all the plugins and the themes (please update if not yet), the Opengraph meta tag could be generated by one of your plugins or active theme.

    To determine that, please follow this troubleshooting guide:
    https://rankmath.com/kb/check-plugin-conflicts/

    Here is a video you can follow as well: https://www.youtube.com/watch?v=FcIWRhcUP2c

    Only the site admin would know and your users won’t be affected by this troubleshooting.

    Let us know how this goes.

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

You must be logged in to reply to this ticket.