Ask your WordPress questions! Pay money and get answers fast! (more info)

Show only first category name in RSS post WordPress

  • SOLVED

I want the only the first category name to show on RSS posts.

Tags show as category names in RSS and I was able to remove them with this code:


add_filter('the_category_rss', 'remove_rss_categories');

function remove_rss_categories( $the_list ) {

$categories = get_the_tags();

foreach ($categories as $category)
$the_list = str_replace("<category><![CDATA[{$category->name}]]></category>", '', $the_list);

return $the_list;
}


But there are still many category names showing and I only want the first in the list to show.

The code to display categories in the RSS file is <?php the_category_rss('rss2') ?>

In the list of categories in the image, I only want "Breakfast + Brunch" to show.

Answers (3)

2017-12-06

Mohamed Ahmed answers:

Hello Beth,

This function will show only first category name in RSS post


add_filter('the_category_rss', 'remove_rss_categories');

function remove_rss_categories( $the_list ) {
$cats = array_slice(get_the_category(), 0, $n);
$category = $cats[0];
$first_cat = "<category><![CDATA[{$category->name}]]></category>";
return $first_cat;
}


If you need anything else you can contact us

Regards
Mohamed Ahmed


Beth Alessi comments:

Mohamed, that's it! Thank you so much! Fantastic.

2017-12-06

mod mi answers:

Hi Beth,

Try this:
$category = get_the_category_rss( 'rss2' ); //Gets all categories of the post
echo $category[0]->cat_name; //Shows the name for the first category only


To use it as a function you can do this filter similar to your existing function:

add_filter('the_category_rss', 'first_rss_category');

function first_rss_category( $first_category ) {

$category = get_the_category_rss( 'rss2' ); // Gets all categories of the post
$first_category = $category[0]->cat_name; // Name of the first category only

return $first_category;
}


Beth Alessi comments:

This didn't work unfortunately.


mod mi comments:

Please try this filter:


function add_feed_content($content) {
if(is_feed()) {
$category = get_the_category(); //Gets all categories of the post
$content .= '<p>'.$category[0]->cat_name.'</p>'; // Displays only the first category name
}
return $content;
}

add_filter('the_excerpt_rss', 'add_feed_content');


Beth Alessi comments:

That changes the content of the blog post to the first category name instead of changing the categories. In the code I listed that strips the tags from the list, it uses $the_list.

So I just tried to tweak yours to

function add_feed_content($the_list) {
if(is_feed()) {
$category = get_the_category(); //Gets all categories of the post
$the_list .= '<p>'.$category[0]->cat_name.'</p>'; // Displays only the first category name
}
return $the_list;
}

add_filter('the_excerpt_rss', 'add_feed_content');


but it doesn't work either.


mod mi comments:

Hi,
Please try the following (it has to be the variable $content to be recognizable by the hook and add the custom content):

function add_feed_content($content) {
if(is_feed()) {
$category = get_the_category(); //Gets all categories of the post
$new_content = '<p>'.$category[0]->cat_name.'</p>'; // Displays only the first category name
$content .= $new_content;
}
return $content;
}
add_filter('the_content', 'add_feed_content');


mod mi comments:

Just a correction to return the new content without replacing the post content

function add_feed_content($content) {
if(is_feed()) {
$category = get_the_category(); //Gets all categories of the post
$new_content = '<p>'.$category[0]->cat_name.'</p>'; // Displays only the first category name
}
return $content . $new_content;
}
add_filter('the_content', 'add_feed_content');


Beth Alessi comments:

I'm not just tacking it onto the content of the post though. It needs to be listed as a category. If you see in the image I included it's showing title, categories, date, and then the blog post shows below. This is going in a MailChimp template, which has a template tag to grab the categories. So it can't just be custom content added to the body of the post.

That's why the code that strips the tags has the <category> tags: $the_list = str_replace("<category><![CDATA[{$category->name}]]></category>", '', $the_list);

2017-12-06

MDan answers:

Hello,

You can try this function. Put this in your functions.php file:


add_filter('pre_get_posts', 'filter_category_RSS_Query');

function filter_category_RSS_Query($query) {
if ( $query->is_feed ) {
$query->set('category_name', 'Your Category');
}
return $query;
}


Change the ‘category_name’ argument from ‘Your Category’ to whatever category you want to show up, in your case
"Breakfast + Brunch" .

If that doesn't work, please try with this code. Put this in your functions.php file:


function myFilter($query) {
if ($query->is_feed) {
$query->set('cat','-23, -23, -23', -23', -23', -23', -23', -23', -23', -23', -23', -23'); //Don't forget to change the category IDs
}
return $query;
}

add_filter('pre_get_posts','myFilter');


For this one, you’ll need to find out the ID of the categories you want to exclude.
After you have the ids, you can replace those from my example with yours.
(Don’t forget to include the “-” before the ID number)
If you need further help, I could do it for you.

You could also hide the other categories with CSS too.


Beth Alessi comments:

I don't want a particular category to show though. Just the first one in the list, whatever that category is.