I'm try to exclude a specific category from a list of categories that a custom post has (in this case 'Uncategorized' - ID: 1).
I've tried exclude
:
wp_list_categories([
'include' => wp_list_pluck(get_the_category(), 'term_id'),
'title_li' => '',
'exclude' => 1
]);
But it still appears. How can I make sure it never appears, even if a post is tagged 'Uncategorized'?
I'm try to exclude a specific category from a list of categories that a custom post has (in this case 'Uncategorized' - ID: 1).
I've tried exclude
:
wp_list_categories([
'include' => wp_list_pluck(get_the_category(), 'term_id'),
'title_li' => '',
'exclude' => 1
]);
But it still appears. How can I make sure it never appears, even if a post is tagged 'Uncategorized'?
Share Improve this question asked Sep 23, 2016 at 10:54 Django ReinhardtDjango Reinhardt 1,7145 gold badges21 silver badges38 bronze badges5 Answers
Reset to default 4The wp_list_categories()
function uses get_terms()
behind the scenes, where the documentation for the exclude
argument says:
If
$include
is non-empty,$exclude
is ignored.
Instead you could try to exclude the term_id
from the include
values:
$include = wp_filter_object_list(
get_the_category(), // Data
[ 'term_id' => 1 ], // Filter Data
'NOT', // Filter Option (exclude)
'term_id' // Pluck Data
);
where we use wp_filter_object_list()
to both filter and pluck. In general it could be better to check if the $include
array is empty or not:
if( $include )
{
// ... stuff above ...
wp_list_categories( [
'include' => $includes,
'title_li' => '',
] );
// ... stuff below...
}
I build some tricky code to exclude category having ID 1. I'm trying to exclude category(ID:1) even remove from pluck also. Your code has include and exclude both parameter and this conflict the result.
//List the pluck...
$exclude_cat_id = 1;
$list_pluck = wp_list_pluck(get_the_category(), 'term_id');
//Get exlude pluck(ID:1) index...
$exclude_pluck = array_search($exclude_cat_id, $list_pluck);
//unset excluded pluck...
unset($list_pluck[$exclude_pluck]);
//Get all category except ID=1
$arrCat = wp_list_categories([
'include' => $list_pluck,
'title_li' => '',
'exclude' => array($exclude_cat_id),
'exclude_tree' => array($exclude_cat_id),
]);
Hope this help you well!
If my case, the only time I didn't want the list of categories to appear was if a post was 'Uncategorized'. The simplest solution in the end was just to use in_category()
:
if (!in_category(1)) {
// Display the categories this post belongs to, as links
wp_list_categories([
'include' => wp_list_pluck(get_the_category(), 'term_id'),
'title_li' => ''
]);
}
$exclude = array();
foreach (get_categories() as $category)
{$exclude[] = 1;}
if (! empty($exclude))
{ $args .= ('' === $args) ? '' : '&';$args .= exclude='.implode(',', $exclude);}
wp_list_categories($args);
How can I display only the specific category when accessing the posts related to that category? For example, I want to show only CSR Events under Categories When accessing posts related to CSR events. Here's the link to CSR post https://www.mi-eq/blood-donation-compaign/
Screenshot: https://i.sstatic/YoTfk.jpg
Similarly, when visiting posts related to other categories, only the specific category will be shown. Is there any simplest to achieve that?