0

Im looking at how to breakdown the output from wp_list_categories. Currently it shows:

<a href="http://www.oddpandadesign.co.uk/shaun/cat-band/drummer/" title="View all posts filed under Drummer">Drummer</a>

But I am looking to only get the category name without the link. So would be Drummer.

My full code to get the category:

                    <?php //Set Variable URL for ALL catefory
                    $alllink = get_post_type_archive_link( 'opd-band' ); ?>

                    <?php //Category Display for Custom Post type - Video
                    $customPostTaxonomies = get_object_taxonomies('opd-band');
                    if(count($customPostTaxonomies) > 0)
                    {
                        foreach($customPostTaxonomies as $tax)
                        {
                            $args = array(
                                'orderby' => 'name',
                                'show_count' => 0,
                                'pad_counts' => 0,
                                'hierarchical' => 1,
                                'taxonomy' => $tax,
                                'title_li' => '',
                                'data-filter' => '',
                                'echo' => 0,
                              );

                            echo 'Categories: <ul id="opd-filters" class="tags-button">';
                            echo  '<li><a href="#" data-filter="*">All</a>';
                            echo str_replace( '<br />', '', wp_list_categories( $args ) );
                            echo '</ul>';


                        }
                    } ?>
1

A custom category walker should do what you need.

class My_Cat_Walker extends Walker_Category {
  function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
    extract($args);

    $cat_name = esc_attr( $category->name );
    $cat_name = apply_filters( 'list_cats', $cat_name, $category );

    $output .= '<li>'.$cat_name.'</li>';
  }
}

$args = array(
  'orderby' => 'name',
  'show_count' => 0,
  'hierarchical' => 1,
  'taxonomy' => $tax,
  'title_li' => '',
  'data-filter' => '',
  'hide_empty' => false,
  'walker' => new My_Cat_Walker
);
echo '<ul>';
  wp_list_categories($args);
echo '</ul>';

If you look at the source for the parent walker, you will notice that the start_el() method does a lot more work that this one does. I don't know how complex you need this walker to be. What you see above is a very bare bones version.

0

You can try:

<?php
  $category_ids = get_all_category_ids();
  foreach($category_ids as $cat_id) {
  $cat_name = get_cat_name($cat_id);
  echo $cat_name."<br/>";
 }
?>

For more details, check https://codex.wordpress.org/Function_Reference/get_all_category_ids

I hope you get an idea...

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.