filters - Print only parent categories of post in custom RSS feed

admin2025-06-03  3

I'm trying to print only the parent category for each post and a permalink to that category within a custom Wordpress RSS feed.

Using <?php the_category_rss(); ?> prints all categories for a post (parent and child categories) like this:

<category><![CDATA[Category 1]]></category>
<category><![CDATA[Category 2]]></category>
<category><![CDATA[Category 3]]></category>

I'd like to print only the parent categories for a post rather than parent and child categories.

I'm also looking for the proper way to print the parent category's permalink within the RSS feed.

I have a feeling the way to do this is a filter, but I'm not sure how to implement it. This is what I have right now, but it returns a 1 instead of the parent category name:

add_filter('the_category_rss', 'only_parent_rss_categories');

function only_parent_rss_categories( $allparents ) {

    $categories = get_the_category();
    $category = $category->category_parent == 0;

    $allparents= esc_html("<category><![CDATA[{$category}]]></category>");

    return $allparents;
}

I'm trying to print only the parent category for each post and a permalink to that category within a custom Wordpress RSS feed.

Using <?php the_category_rss(); ?> prints all categories for a post (parent and child categories) like this:

<category><![CDATA[Category 1]]></category>
<category><![CDATA[Category 2]]></category>
<category><![CDATA[Category 3]]></category>

I'd like to print only the parent categories for a post rather than parent and child categories.

I'm also looking for the proper way to print the parent category's permalink within the RSS feed.

I have a feeling the way to do this is a filter, but I'm not sure how to implement it. This is what I have right now, but it returns a 1 instead of the parent category name:

add_filter('the_category_rss', 'only_parent_rss_categories');

function only_parent_rss_categories( $allparents ) {

    $categories = get_the_category();
    $category = $category->category_parent == 0;

    $allparents= esc_html("<category><![CDATA[{$category}]]></category>");

    return $allparents;
}
Share Improve this question edited Feb 18, 2019 at 20:32 aronmoshe_m asked Feb 18, 2019 at 20:19 aronmoshe_maronmoshe_m 1396 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I'm still interested in hearing how this can be accomplished using the Wordpress function the_category_rss(), but I did find another way to do this:

<?php $parentsonly = ""; 
  // loop through categories
  foreach((get_the_category()) as $category) {
    // exclude child categories
    if ($category->category_parent == 0) {
      $parentsonly = '<category domain="' . get_category_link($category->cat_ID) . '">' . $category->name . '</category>'; 
    } 
  }
echo $parentsonly; ?>

This returns each parent category in the proper RSS validated format, using <category> and domain for the category URL:

<category domain="http://www.yourdomain/category-archive/">My Category</category>

Hope this helps someone else!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748892198a314589.html

最新回复(0)