Using Advanced Custom Field (ACF) to insert meta description on each page

I have used ACF hundreds of times but never to edit the meta description.I have created a simple text field in ACF, then

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632092a4637173.html

相关推荐

  • Using Advanced Custom Field (ACF) to insert meta description on each page

    I have used ACF hundreds of times but never to edit the meta description.I have created a simple text field in ACF, then

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信