I want to show all WordPress categories in a page. I use this code:

 $categories = get_categories( array(
 'orderby' => 'name',
 'order'   => 'ASC',
 'exclude' => '1,116',

    ) );
  foreach  ($categories as $category) {
      echo '<div class="item_product">';
      // display category image
              echo '<div class="image_wrapper is-image">';
                  echo do_shortcode(sprintf('[wp_custom_image_category term_id="%s"]',$category->term_id));
            echo '</div>';
            // display category name 
              echo '<div class="desc_left">';
                    echo '<h2 class="podcast_h2"><a href=" ' .get_permalink($category->ID). ' "> '.$category->name.' </a></h2>';
              echo '</div>';
          echo '</div>';

} ?>

But each category is linked to the same page as the category list
I want each category link to that category


To get link to category use the function get_category_link (doc here).
Function get_permalink is intended for posts.

echo '<h2 class="podcast_h2"><a href=" ' .get_category_link($category->term_id). ' "> '.$category->name.' </a></h2>'
  • I used this, but there was the same problem again - i used : get_category_link( $category->term_id ) And my problem is over – omid chahardoli Aug 5 '18 at 14:04
  • You're right, the WP_Term object has a property term_id instead ID. – nmr Aug 5 '18 at 14:22

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.