1

With this code I show all categories:

<?php
$args = array(
  'orderby' => 'name',
  'parent' => 0
  );
$categories = get_categories( $args );
foreach ( $categories as $category ) {
    echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
}
?>

I want to show all categories with featured image, load featured image from post, example:

Category Tours, first post: my first travel / with featured image

Load image from my first travel in / Category Tours can be load from first post or last post or aleatory this is not important, but have to be from any post.

I can do this with plugin featured image category with the code below, I list all categories with featured image, but I have to set independently image to category, but I not want to use plugin.

<?php
$args = array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories = get_categories($args);
  foreach($categories as $category) { 

    echo '<img src="' . cfi_featured_image_url( array( 'cat_id' => $category->term_id, 'size' => 'full' ) ) . '">';
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  }
?>

Thanks for reading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.