我已经到处寻找这个问题的解决方案,但似乎找不到任何符合我要求的解决方案。
基本上,我想做的是根据特定自定义字段的值在帖子中显示一个特定的div。
例如
如果“featured”自定义字段的值为“1”,那么我希望能够在single中显示div。特定帖子的php
但是如果“featured”自定义字段的值不是1,那么我不希望出现任何内容。
这有意义吗?
我已经到处寻找这个问题的解决方案,但似乎找不到任何符合我要求的解决方案。
基本上,我想做的是根据特定自定义字段的值在帖子中显示一个特定的div。
例如
如果“featured”自定义字段的值为“1”,那么我希望能够在single中显示div。特定帖子的php
但是如果“featured”自定义字段的值不是1,那么我不希望出现任何内容。
这有意义吗?
然后你需要一个过滤器the_content
.
function add_conditional_div($content) {
global $post;
$meta_field = get_post_meta($post->ID, \'your-field-name\', true);
if (1 === $meta_field) {
$content .= \'<div>whatever</div>\';
}
return $content;
}
The1
当然,值是应该与meta\\u字段匹配的任何值。http://codex.wordpress.org/Function_Reference/get_post_meta
http://codex.wordpress.org/Function_Reference/the_content
您也可以直接编辑主题文件或制作child theme. 如果您正在编辑未创建的主题,则强烈建议使用后者。