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.

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' );
| improve this answer | |
  • Thanks, that's working! – n00bly Sep 27 '19 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.