admin管理员组

文章数量:1434956

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:

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:

Is this even possible?

Any help would be great!

Ta, Jason.

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:

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:

Is this even possible?

Any help would be great!

Ta, Jason.

Share Improve this question edited Apr 3, 2019 at 12:15 Jason Is My Name asked Apr 3, 2019 at 11:54 Jason Is My NameJason Is My Name 3782 gold badges7 silver badges21 bronze badges 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') – Evince Development 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 – Jason Is My Name 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
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: Using Advanced Custom Field (ACF) to insert meta description on each page