3

In a page, I need to display a list of all sub categories of a specify category.

For example, in the category Sport have 6 subcategories:

  • Swim
  • Football
  • Basket
  • Ski
  • Hockey

Each subcategory has a featured image, title and description, which I'd like to display.

I've add featured image using this code (in functions.php):

add_action('init', 'my_category_module');
function my_category_module() {
 add_action ( 'edit_category_form_fields', 'add_image_cat');
 add_action ( 'edited_category', 'save_image');
 }

function add_image_cat($tag){
 $category_images = get_option( 'category_images' );
 $category_image = '';
 if ( is_array( $category_images ) && array_key_exists( $tag->term_id, $category_images ) ) {
 $category_image = $category_images[$tag->term_id] ;
 }
 ?>
 <tr>
 <th scope="row" valign="top"><label for="auteur_revue_image">Image</label></th>
 <td>
 <?php
 if ($category_image !=""){
 ?>
 <img src="<?php echo $category_image;?>" alt="" title=""/>
 <?php
 }
 ?>
 <br/>
 <input type="text" name="category_image" id="category_image" value="<?php echo $category_image; ?>"/><br />
 <span>This field allows you to add a picture to illustrate the category. Upload the image from the media tab WordPress and paste its URL here.</span>
 </td>
 </tr>
 <?php
 }
function save_image($term_id){
 if ( isset( $_POST['category_image'] ) ) {
 //load existing category featured option
 $category_images = get_option( 'category_images' );
 //set featured post ID to proper category ID in options array
 $category_images[$term_id] =  $_POST['category_image'];
 //save the option array
 update_option( 'category_images', $category_images );
 }
 }

category.php

<?php
 if(is_category()){
 $category_images = get_option( 'category_images' );
 $category_image = '';
 if ( is_array( $category_images ) && array_key_exists( get_query_var('cat'), $category_images ) ){

$category_image = $category_images[get_query_var('cat')] ;
 ?>
 <img src="<?php echo $category_image;?>"/>
 <?php
 }
 }
 ?>

I need to have a grid look like this (this is for recent post )

With this I can display all categories with their descriptions, but how can I add the featured image and display only subcategories of certain category parent?

<?php
$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );

foreach( $categories as $category ) {
    $category_link = sprintf( 
        '<a href="%1$s" alt="%2$s">%3$s</a>',
        esc_url( get_category_link( $category->term_id ) ),
        esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
        esc_html( $category->name )
    );

    echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
    echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';}

Also is possible to display as a grid?

Thanks

0

How can I add the featured image?

From what I can tell in your code, you've stored all category featured images in the category_images option. So you can use get_option to retrieve the featured image for each category:

$image_urls = get_option( 'category_images', array() );    

foreach ( $categories as $category ) {
    $cat_id = $category->term_id;
    $image_url = isset( $image_urls[$cat_id ) ? $image_urls[$cat_id] : '';

    ...
}

You can then use $image_url as the src attribute of an <img>.

How can I display only the subcategories of a certain category parent?

Like get_terms, get_categories has a 'child_of' parameter. Again, in your foreach loop:

    $sub_categories = get_categories( array(
        'orderby' => 'name',
        'order' => 'ASC',
        'child_of' => $cat_id,
    ) );

    foreach ( $sub_categories as $sub_category ) {
        // Process the sub-categories here.
    }

Is it possible to display these as a grid?

Yes, you can! But that's an HTML/CSS question that I think is beyond the scope of this question. Go ahead and open a new question under that topic. You can never ask too many. :)

Hope this helps!

0

I guess you've already found the answer. If you haven't, Use the function below.skip first category by using counter

function your_function( $taxonomy = 'category', $show_links = true, $echo = true ) {

// Make sure taxonomy exists
if ( ! taxonomy_exists( $taxonomy ) ) {
    return;
}

// Get terms
$list_terms = array();
$terms      = wp_get_post_terms( get_the_ID(), $taxonomy );

// Return if no terms are found
if ( ! $terms ) {
    return;
}

// Loop through terms
foreach ( $terms as $term ) {
    $permalink = get_term_link( $term->term_id, $taxonomy );
    if ( $show_links ) {
        $list_terms[] = '<a href="'. $permalink .'" title="'. esc_attr( $term->name ) .'" class="term-'. $term->term_id .'">'. $term->name .'</a>';
    } else {
        $list_terms[] = '<span class="term-'. $term->term_id .'">'. esc_attr( $term->name ) .'</span>';
    }
}

// Turn into comma seperated string
if ( $list_terms && is_array( $list_terms ) ) {
    $list_terms = implode( ', ', $list_terms );
} else {
    return;
}

// Echo terms
if ( $echo ) {
    echo $list_terms;
} else {
    return $list_terms;
}

}

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.