admin管理员组文章数量:1025482
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 |1 Answer
Reset to default 7As 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.
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 toget_field()
explicitly. – mrben522 Commented Apr 3, 2019 at 12:29 -
Hello, Jason might be you need to pass
post_id
likeget_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
1 Answer
Reset to default 7As 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
版权声明:本文标题:Using Advanced Custom Field (ACF) to insert meta description on each page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632091a2160228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump(get_the_id())
in there and see if you can get the appropriate page ID that way, if so then pass it toget_field()
explicitly. – mrben522 Commented Apr 3, 2019 at 12:29post_id
likeget_field('meta_description','123')
– Evince Development Commented Apr 3, 2019 at 12:31