Php 在内容之前插入自定义字段值

Php 在内容之前插入自定义字段值,php,wordpress,custom-fields,add-filter,Php,Wordpress,Custom Fields,Add Filter,我有一个自定义元字段,我想自动插入到\u内容中,这样我的AMP插件就可以以与\u内容相同的方式呈现自定义字段值 目前我正在使用此代码显示它: <?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?> <?php if ( ! empty( $video_value ) ) {?> <div class="video-container"><?php echo $

我有一个自定义元字段,我想自动插入到\u内容中,这样我的AMP插件就可以以与\u内容相同的方式呈现自定义字段值

目前我正在使用此代码显示它:

<?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?>

<?php if ( ! empty( $video_value ) ) {?>
    <div class="video-container"><?php echo $video_value; ?></div>
<?php } else { ?>
    <?php the_post_thumbnail(); ?>
<?php } ?>


但是我想在
内容之前自动插入
$video\u值

你可以这样做-

并添加所需的条件-

您可能需要访问全局$post以获取元值

function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true)  {
    $beforecontent = 'This line will go before the content - populate with whatever.';
    $aftercontent = 'This will come after the content - makes sense right?';
    $fullcontent = $beforecontent . $content . $aftercontent;
} else {
    $fullcontent = $content;
}

return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');

您可以在functions.php中添加此项。您可以执行以下操作-

并添加所需的条件-

您可能需要访问全局$post以获取元值

function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true)  {
    $beforecontent = 'This line will go before the content - populate with whatever.';
    $aftercontent = 'This will come after the content - makes sense right?';
    $fullcontent = $beforecontent . $content . $aftercontent;
} else {
    $fullcontent = $content;
}

return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');

您可以在functions.php中添加此项。您可以使用
内容过滤器来执行此操作。你可以在网上看到更多关于它的信息

但代码可能是这样的:

function my_custom_content_filter($content){
  global $post;
  $video_value = get_post_meta($post->ID, '_video', true);
  if($video_value){
    return '<div class="video-container">' . $video_value . '</div>' . $content;
 }else{
   return get_the_post_thumbnail($post->ID) . $content;
 }
}
add_filter('the_content', 'my_custom_content_filter');
函数我的自定义内容过滤器($content){
全球$员额;
$video\u value=get\u post\u meta($post->ID,'u video',true);
如果($video\u值){
返回“.$video_value.”.$content;
}否则{
返回get_the_post_缩略图($post->ID)。$content;
}
}
添加_过滤器(“_内容”、“我的_自定义_内容_过滤器”);
您可以在
functions.php
文件中添加此代码


注意此筛选器仅对
内容()起作用,而不是
获取内容()
您可以使用
内容
筛选器来执行此操作。你可以在网上看到更多关于它的信息

但代码可能是这样的:

function my_custom_content_filter($content){
  global $post;
  $video_value = get_post_meta($post->ID, '_video', true);
  if($video_value){
    return '<div class="video-container">' . $video_value . '</div>' . $content;
 }else{
   return get_the_post_thumbnail($post->ID) . $content;
 }
}
add_filter('the_content', 'my_custom_content_filter');
函数我的自定义内容过滤器($content){
全球$员额;
$video\u value=get\u post\u meta($post->ID,'u video',true);
如果($video\u值){
返回“.$video_value.”.$content;
}否则{
返回get_the_post_缩略图($post->ID)。$content;
}
}
添加_过滤器(“_内容”、“我的_自定义_内容_过滤器”);
您可以在
functions.php
文件中添加此代码


注意此筛选器仅对
内容()起作用
而不
获取内容()

argh-您为他编写了它!:-D我的答案是让他自己去想一想-u-呵呵,好吧,你的答案对教育来说更好一点:谢谢你们!啊-是你为他写的!:-D我的答案是让他自己去想一想-u-呵呵,好吧,你的答案对教育来说更好一点:谢谢你们!