Nicolas
Nicolas

Reputation: 11

Get category title with shortcode

i'd like to create a shortcode for wordpress to display the category name and i found this code :

function categories_list_func( $atts ){
 $categories = get_the_category();

     if($categories) {
        foreach($categories as $category) {
            $output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "Read more posts from : %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></li>';
        }
        $second_output = trim($output);
      }
      $return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';

 return $return_string;

} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );

But i know nothing in PHP and i'd like to remove everything (the link and "Catégorie :" in h4) except the category name. I managed to do this with CSS but i'd like a clean php code if possible.

Could you help me please ? Thanks. Nicolas.

Upvotes: 0

Views: 4378

Answers (1)

Fadi Nouh
Fadi Nouh

Reputation: 354

this is the code you want with link to categories :

<?php  
function categories_list_func( $atts ){
 $categories = get_the_category();

     if($categories) {
        foreach($categories as $category) {
            $output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "Read more posts from : %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></li>';
        }
        $second_output = trim($output);
      }
      $return_string = '<ul>' . $second_output . '</ul>';

 return $return_string;

} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );

?>

and this is only the names without any listing and any links

function categories_list_func( $atts ){
 $categories = get_the_category();

     if($categories) {
        foreach($categories as $category) {
            $output .= '<li>'.$category->cat_name.'</li>';
        }
        $second_output = trim($output);
      }
      $return_string = '<ul>' . $second_output . '</ul>';

 return $return_string;

} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );

or if you want it in another way dont hesitate to tell me :)

Upvotes: 1

Related Questions