Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do I exclude category parents, but show all the children? WordPress

  • SOLVED

How do I exclude category parents but show all the children of those parents?

I'm using wp_list_categories to list a custom taxonomy.
Here is my code:

<ul>
<?php wp_list_categories('exclude=1,4&title_li=&taxonomy=specs' ); ?>
</ul>

This produces the following:

<strong>Model</strong>
> Positano
> Amalfi
<strong>Style</strong>
> Ladies
> Mens
> Single Speed

When I display the taxonomy in the sidebar using wp_list_categories I want it to show "Positano" and "Amalfi", but not "Model".

When I tried adding the parent categories to the exclude parameter within the function, it excluded the whole category structure (parent and child). I want the children to show, but not the parents.

Ideally I would like the end result to be:

Positano
Amalfi
Ladies
Mens
Single Speed

Let me know if any of this requires more clarification and thanks in advance to anyone that can help me out!

Cheers,
Matt

Answers (5)

2011-02-13

Sébastien | French WordpressDesigner answers:

You can use the code of Nilesh shiragave (above) but i think there is a problem : the categories will be classified by ID and not by category parent.

The result with this code could be :

> Ladies
> Positano
> Mens
> Amalfi
> Single Speed


instead of
> Positano
> Amalfi

> Ladies
> Mens
> Single Speed


if the IDs are
> Ladies id=1
> Positano id=2
> Mens id=3
> Amalfi id=4
> Single Speed id=5


I think you want to display the children category by category parent. No ?

If you want display the categories by parents, paste this code in you functions.php

class custom_walker_category extends Walker_Category {


function start_el(&$output, $category, $depth, $args) {
extract($args);

$cat_name = esc_attr( $category->name);
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$link = '<a href="' . get_term_link( $category, $category->taxonomy ) . '" ';
if ( $use_desc_for_title == 0 || empty($category->description) )
$link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
else
$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
$link .= '>';
$link .= $cat_name . '</a>';

if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';

if ( empty($feed_image) )
$link .= '(';

$link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"';

if ( empty($feed) )
$alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
else {
$title = ' title="' . $feed . '"';
$alt = ' alt="' . $feed . '"';
$name = $feed;
$link .= $title;
}

$link .= '>';

if ( empty($feed_image) )
$link .= $name;
else
$link .= "<img src='$feed_image'$alt$title" . ' />';
$link .= '</a>';
if ( empty($feed_image) )
$link .= ')';
}

if ( isset($show_count) && $show_count )
$link .= ' (' . intval($category->count) . ')';

if ( isset($show_date) && $show_date ) {
$link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
}

if ( isset($current_category) && $current_category )
$_current_category = get_category( $current_category );

if($category->parent!=0)
{
if ( 'list' == $args['style'] ){
$output .= "\t<li";
$class = 'cat-item cat-item-'.$category->term_id;
if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
$class .= ' current-cat';
elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
$class .= ' current-cat-parent';
$output .= ' class="'.$class.'"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
}
}

}

function custom_list_categories($args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new custom_walker_category;
wp_list_categories( $args );
}


and use custom_list_categories
instead of wp_list_categories

the parameters of wp_list_categories are the same of custom_list_categories

example 1 :
custom_list_categories('title_li=&taxonomy=specs');

example 2 (with all parameters) :
custom_list_categories( array(
'exclude' => '56,32',
'exclude_tree' => '',
'child_of' => 0,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC',
'use_desc_for_title'=> 1,
'number' => NULL,
'hierarchical' => true,
'show_count' => 0,
'pad_counts' => 0,
'style' => 'list',
'show_option_all' => '',
'show_option_none' => __('No categories'),
'show_last_update' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'current_category' => 0,
'taxonomy' => 'specs',
'title_li' => __( '' ),
'echo' => 1,
'depth' => 0 ) );






2011-02-13

Jarret Minkler answers:

Have to change the SQL to not include cats with no parents. (poor kitties!)

2011-02-13

Vidyut Kale answers:

Try the child_of argument.

Something like:
<ul>

<?php wp_list_categories('child_of=1,4&title_li=&taxonomy=specs' ); ?>

</ul>


If you want the children of the two categories to be output separately, use the code twice. Like this:

<ul>

<?php wp_list_categories('child_of=1&title_li=&taxonomy=specs' ); ?>
<?php wp_list_categories('child_of=4&title_li=&taxonomy=specs' ); ?>

</ul>

2011-02-13

Nilesh shiragave answers:

Try this code.. It will display only subcategories. Parent categories will be excluded.


<?php
$args=array(
'orderby' => 'id',
'order' => 'ASC',
'taxonomy' => 'specs'
);
$categories=get_categories($args);
echo '<ul>';
foreach($categories as $category) {
if($category->parent!=0)
{
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . $category->name. '" ' . '>' . $category->name.'</a> </li> ';
}
}
echo '</ul>';
?>


Nilesh shiragave comments:

let me know how you want to order the end result.. my previous code will order the category names by category ID in ascending order.

2011-02-13

Honzik A. answers:

Use this:

<ul>
<?php wp_list_categories('orderby=id&show_count=0&title_li=Categories&child_of=1'); ?>
<?php wp_list_categories('orderby=id&show_count=0&title_li=&child_of=4'); ?>
</ul>


Sorry for multiple editing.


Honzik A. comments:

<ul>
<?php wp_list_categories('orderby=name&order=DESC&show_count=0&title_li=Categories&child_of=1'); ?>
<?php wp_list_categories('orderby=name&order=ASC&show_count=0&title_li=&child_of=4'); ?>
</ul>


and result will be:

<strong>Categories</strong>
Positano
Amalfi
Ladies
Mens
Single Speed