2

I have used ACF hundreds of times but never to edit the meta description.

I have created a simple text field in ACF, then tried to use this script:

<?php
$meta_description = get_field('meta_description'); ?>

<?php if ($meta_description != '') : ?>

    <meta name="description" content="<?php echo $meta_description; ?>"/>

<?php else : ?>

    <meta name="description" content="This could be an interesting meta description"/>

<?php endif; ?>

But, it doesn't acknowledge the field, it simply does the 'else' from the if statement.

Here is a screenshot of my ACF set-up:

enter image description here

Do I need to sanitise the field or call it earlier or something? Not sure why this would work so well for all other fields but this one.

Here is the field being filled in on the pages:

enter image description here

Is this even possible?

Any help would be great!

Ta, Jason.

4
  • 2
    sounds like you're not in the loop yet. add a var_dump(get_the_id()) in there and see if you can get the appropriate page ID that way, if so then pass it to get_field() explicitly.
    – mrben522
    Commented Apr 3, 2019 at 12:29
  • Hello, Jason might be you need to pass post_id like get_field('meta_description','123') Commented Apr 3, 2019 at 12:31
  • The meta description is written within the head, so if I do a var_dump, I'm not too sure where I would see the result. I have also tried $meta_description = get_field('meta_description', get_the_id()) and his doesn't work Commented Apr 3, 2019 at 12:37
  • 2
    A few ideas here: How to get current page ID outside the loop
    – Rup
    Commented Apr 3, 2019 at 12:40

1 Answer 1

7

As mentioned in the comments, you are outside of the loop, so get_field will not know the ID. You can use the code below:

<?php 

    $meta_description = get_field('meta_description', get_queried_object_id());

    if(empty($meta_description)) {
        $meta_description = 'This could be an interesting meta description';
    }

?>

<meta name="description" content="<?php echo $meta_description; ?> "/>

If you use var_dump() in the header.php file, you can just add it at the very top, followed by a die() to test:

<?php

var_dump(get_queried_object_id());
the_field('meta_description', get_queried_object_id());
die();

?><!doctype html>
<html <?php language_attributes(); ?>>.....

Give that a try and let us know how you get on.

Update: You may also want to look at something like the Wordpress SEO plugin as it lets you manage your SEO meta tags without having to edit any code.

3
  • 2
    Nor will get_the_ID() at first glance ("... in the WordPress loop"). If that doesn't work, it looks like you want get_queried_object_id() instead.
    – Rup
    Commented Apr 3, 2019 at 12:45
  • Yes. Testing locally get_the_id() returned the ID for a post on the page, rather than the page itself. I've updated my answer. Commented Apr 3, 2019 at 12:49
  • Very well explained, clean concise code. Works a charm! Commented Apr 3, 2019 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.