繁体   English   中英

将Post Meta附加到帖子内容的开头

[英]Appending Post Meta to Beginning of Post Content

我正在尝试改变发布到Apple New Wordpress插件的行为。 我的主题使用自定义字段进行视频嵌入,但插件无法识别该内容。 我试图将元数据附加到Apple News的帖子开头。 这是我的代码不起作用:

 function add_post_meta_content($content) { $meta = get_post_meta( get_the_ID(), 'csco_post_embed', true ); return .$meta.$content; } add_filter('apple_news_exporter_content_pre', add_post_meta_content); apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID ); 

例如,如果我将代码更改为以下内容:

 function add_post_meta_content($content) { $meta = get_post_meta( get_the_ID(), 'csco_post_embed', true ); return 'Print this content before the post'.$meta.$content; } add_filter('apple_news_exporter_content_pre', add_post_meta_content); apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID ); 

它将“在帖子前打印此内容”附加到帖子的开头而没有问题。 我在这里错过了什么?

插件已经将$post->ID发送到您的过滤器,因此无需调用get_the_ID()

试试这段代码:

function add_post_meta_content($content, $post_id) {
    $meta = get_post_meta( $post_id, 'csco_post_embed', true );
    return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);

-

如果这不起作用,请确保实际上在csco_post_embed键下的数据库元表中保存了一些内容。 确认这一点的简便方法是打开数据库并快速查询:

SELECT *
FROM wp_postmeta
WHERE post_id = ENTER_YOUR_POST_ID_HERE AND meta_key = 'csco_post_embed';

真棒! 你让我99%的路程。 不得不返回oembed以使视频正常工作。 这是最终的代码,以防其他人来看。

 function add_post_meta_content($content, $post_id) { $meta = wp_oembed_get( get_post_meta( $post_id, 'csco_post_embed', true ) ); return $meta.$content; } add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM