Advanced Custom Fields… not load the field

#37625
  • Resolved Michele
    Rank Math free

    Hello,
    I need to load the values (text) from an advanced custom field (e.g. “team-archive-meta-tag-description”) in the “{CPT} Archive Title” (see screenshot https://ibb.co/SJSK1tx).
    I’m using this code but not works:
    %customfield(team-archive-meta-tag-description)%

    How can I do it?
    Thanks.
    Regards,
    Michele

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

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    The variable %customfield()% will only work when the custom field exists on the same page where it is getting used.

    Have you tried to use the native ACF shortcode [acf field="field_name" post_id="123"] instead?

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Hello Michael,
    I tried but if I use (single or double apex is the same)…
    [acf field='team-archive-meta-tag-description' post_id='391']

    … this is the result (not load the values):
    <meta name="description" content="[acf field='team-archive-meta-tag-description' post_id='391']"/>

    What is the correct way to insert the above shortcode in the “{CPT} Archive Title/Description”?

    Thanks.

    • This reply was modified 4 years, 8 months ago by Michele.
    • This reply was modified 4 years, 8 months ago by Michele.

    The “ACF” module in Rank Math is activated/enabled.

    • This reply was modified 4 years, 8 months ago by Michele.

    Below the exact result (this forum elaborate the ascii characters):
    https://ibb.co/VSZxxNk
    https://ibb.co/Z8SBN27

    Hello,

    Thank you for contacting Rank Math today.

    You will need to allow shortcodes on the meta descriptions by adding the following code snippet to your theme’s functions.php file:

    /**
     * Allow shortcodes in the Meta Data.
     */
    add_filter( 'rank_math/paper/auto_generated_description/apply_shortcode', '__return_true' );

    Looking forward to helping you. Thank you.

    ​​​​​​

    I try but the results is always the same.

    Do I need to change something in the path “rank_math/paper/auto_generated_description/apply_shortcode”?
    I can’t find nothing in that path.

    Or should I activate something on functions.php for ACF as well?

    Hi there,

    Thanks for the follow up.

    I have just tested this on my end and the following code works to display the shortcode data. Please delete the last one that I shared:

    /**
     * Allow changing the meta description sentence from within the theme.
     *
     * @param string $description The description sentence.
     */
    add_filter( 'rank_math/frontend/description', function( $description ) {
    	return do_shortcode($description);
    });

    I hope this info helps. Thank you.

    Yes, now the shortcode works good, thanks.
    However, now I’ve a problem with the change language of the field.

    As you said above, the acf not works when the custom field exists on the same page where it is getting used, so is necessary to indicate the “post_id”, but… how can change it to allow to load the correct text when a user change language?

    For example, I’ve three languages on the website, English, Italian and Japanese.
    So for the English version the Archive page has the post id “391”, Italian “”1208, Japanese “1209”.

    Now the page show always for all the three languages the acf field with post id 391 (English):
    [acf field=”team-archive-meta-tag-description” post_id=”391″]

    How can I change the post id for the other two languages?
    I’ve tried to search on the “String Translation” of WPML but I couldn’t find the above shortcode.
    Thanks.

    Hi there,

    Thanks for the follow up.

    Could you please check if this will work without the post_id parameter ie
    [acf field=”team-archive-meta-tag-description”]

    Thank you.

    Unfortunately it doesn’t work, I’ve already tried.

    … maybe with some conditional behaviour if-else, “en” show 391, “it” “1208”, ja “1209”.
    Do you know how to create this function inside of the “{CPT} Archive Description” field or in the functions.php?

    Hello,

    Thank you for contacting Rank Math today.

    This will require that you create a custom description for your archive page by hooking into the same filter that I shared earlier.

    Here is an example snippet that you can use, please note that the code is untested and you may need to modify it further:

    /**
     * Allow changing the meta description sentence from within the theme.
     *
     * @param string $description The description sentence.
     */
    add_filter( 'rank_math/frontend/description', function( $description ) {
          if(is_archive() && ICL_LANGUAGE_CODE =='en'){
             $description = get_field('field_name','391');
             return $description;
          }elseif(is_archive() && ICL_LANGUAGE_CODE =='it'){
             $description = get_field('field_name','1208');
             return $description;
    
          }
    	return $description;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Thanks!

    Hi Michele,

    Thanks for the reply.

    I am glad that this info was helpful 🙂

    Feel free to contact us for any other questions, comments or suggestions.

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

The ticket ‘Advanced Custom Fields… not load the field’ is closed to new replies.