最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add extra content to RSS feed

programmeradmin0浏览0评论

I want to add some extra information to my RSS feed, it's an Array that I need to display only the "name" from.

This is the code in my functions.php

function crunchify_feed($content) {  
    if(is_feed()) {  
        $post_id = get_the_ID(); // sample reference. remove this if you don't want to use this
        $fv_jobtype = get_the_terms( $post_id, "job_type" );
        $fv_jobtype_name = $fv_jobtype->name;
        $output .= $post_id;
        $output .= print_r($fv_jobtype);
        $content = $content.$output;
    }  
    return $content;  
}  
add_filter( 'the_content', 'crunchify_feed' );

The output shows this in the RSS feed:

Array
(
[0] => WP_Term Object
    (
        [term_id] => 229
        [name] => Parttime
        [slug] => parttime
        [term_group] => 0
        [term_taxonomy_id] => 229
        [taxonomy] => job_type
        [description] => 
        [parent] => 0
        [count] => 380
        [filter] => raw
    )

)

I only need the Name to show so it can be read out by a program.

I want to add some extra information to my RSS feed, it's an Array that I need to display only the "name" from.

This is the code in my functions.php

function crunchify_feed($content) {  
    if(is_feed()) {  
        $post_id = get_the_ID(); // sample reference. remove this if you don't want to use this
        $fv_jobtype = get_the_terms( $post_id, "job_type" );
        $fv_jobtype_name = $fv_jobtype->name;
        $output .= $post_id;
        $output .= print_r($fv_jobtype);
        $content = $content.$output;
    }  
    return $content;  
}  
add_filter( 'the_content', 'crunchify_feed' );

The output shows this in the RSS feed:

Array
(
[0] => WP_Term Object
    (
        [term_id] => 229
        [name] => Parttime
        [slug] => parttime
        [term_group] => 0
        [term_taxonomy_id] => 229
        [taxonomy] => job_type
        [description] => 
        [parent] => 0
        [count] => 380
        [filter] => raw
    )

)

I only need the Name to show so it can be read out by a program.

Share Improve this question edited Sep 26, 2019 at 16:05 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Sep 26, 2019 at 12:42 n00blyn00bly 1234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The problem lies in these lines:

    $fv_jobtype = get_the_terms($post_id, "job_type");
    ...
    $output .= print_r($fv_jobtype);

First line takes all job types assigned to given post and stores it as an array of objects in fv_jobtype variable.

Second last one uses print_r function which tries to print given complex value in readable form - so you get what you get.

How to fix it?

Output what you really want to see there. If only name should be visible, do exactly that:

function crunchify_feed( $content ) {  
    if ( is_feed() ) {  
        $post_id = get_the_ID(); // sample reference. remove this if you don't want to use this
        $output = implode( ', ', wp_list_pluck( get_the_terms( $post_id, 'job_type' ), 'name' ) ); 
        $content = $content.$output;
    }  
    return $content;  
}

add_filter( 'the_content', 'crunchify_feed' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论