我想将自定义帖子类型中的自定义字段添加到位于的该帖子类型的RSS提要中http://example.com/feed/?post_type=my_custom_post_type
我看到了关于为常规提要执行此操作的信息,但没有看到关于如何重写自定义帖子类型提要的信息。
我需要在提要中添加10-15个项目(第一幕、第二幕、第三幕、价格、购买链接…)
我想将自定义帖子类型中的自定义字段添加到位于的该帖子类型的RSS提要中http://example.com/feed/?post_type=my_custom_post_type
我看到了关于为常规提要执行此操作的信息,但没有看到关于如何重写自定义帖子类型提要的信息。
我需要在提要中添加10-15个项目(第一幕、第二幕、第三幕、价格、购买链接…)
function add_custom_fields_to_rss() {
if(get_post_type() == \'my_custom_post_type\' && $my_meta_value = get_post_meta(get_the_ID(), \'my_meta_key\', true)) {
?>
<my_meta_value><?php echo $my_meta_value ?></my_meta_value>
<?php
}
}
add_action(\'rss2_item\', \'add_custom_fields_to_rss\');
您应该能够替换和任何其他需要添加到提要的元数据。你好@curtismchale:
借鉴prettyboymp的优秀答案,再加上我的技巧,以下是如何创建多个自定义字段(我做了3个,你可以做更多):
add_action(\'rss2_item\', \'yoursite_rss2_item\');
function yoursite_rss2_item() {
if (get_post_type()==\'my_custom_post_type\') {
$fields = array( \'field1\', \'field2\', \'field3\' );
$post_id = get_the_ID();
foreach($fields as $field)
if ($value = get_post_meta($post_id,$field,true))
echo "<{$field}>{$value}</{$field}>\\n";
}
}
另外,一定要给@prettyboymp一些道具,因为在他回答之前我不知道怎么做。我也只是回答,因为我不确定他还要多久才能回来,所以我决定同时给你一个答案。谢谢你,谢谢你提供了这条极好的信息。
我想扩展其他两个人已经写过的内容。。。要对此进行验证,必须具有自定义命名空间。您可以这样做:
/* IN ORDER TO VALIDATE you must add namespace */
add_action(\'rss2_ns\', \'my_rss2_ns\');
function my_rss2_ns(){
echo \'xmlns:mycustomfields="\'. get_bloginfo(\'wpurl\').\'"\'."\\n";
}
然后用自定义名称空间作为字段名称项的前缀在本例中,我使用了“mycustomfields”,请参见以下内容:/* add elements */
add_action(\'rss2_item\', \'yoursite_rss2_item\');
function yoursite_rss2_item() {
if (get_post_type()==\'my_custom_post_type\') {
$fields = array( \'field1\', \'field2\', \'field3\' );
$post_id = get_the_ID();
foreach($fields as $field)
if ($value = get_post_meta($post_id,$field,true))
echo "<mycustomfields:{$field}>{$value}</mycustomfields:{$field}>\\n";
}
}
在旁注中,您可以使用动作钩住3个 rss2_ns : to add a specific namespace
add_action(\'rss2_ns\', \'my_rss2_ns\');
rss2_head : to add tags in the feed header
add_action(\'rss2_head\', \'my_rss2_head\');
rss2_item : to add tags in each feed items
add_action(\'rss2_item\', \'my_rss2_item\');
我已经创建了一个新网站(here), 我想在主题的右上角添加一个RSS图标/徽标(如this theme).最好的方法是什么?(PHP/CSS方面)