• YK

    (@yurikacharava)


    I’ve got the following snippet to display all subcategories of a given category:

    $args = array('parent' => 'XX', 'orderby' => 'ID', 'order' => 'desc');
    $categories = get_categories( $args );
    foreach($categories as $category) { 
    echo '<p><strong>' . $category->name.'</strong></p>';
    }

    I want to extend it to display the dates of the earliest and latest posts for each subcategory.

    For example:
    SUBCATEGORY 1
    Active since YYYY-MM-DD till YYYY-MM-DD

    Or:
    SUBCATEGORY 1
    Years active: YYYY – YYYY

    etc…

    Frankly I have no idea how to achieve that. Any help would be highly appreciated

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter YK

    (@yurikacharava)

    So I will reply to my post myself.

    Not so much researching and reading The Codex led me to the following result:

    <?php
    $args = array('parent' => XX, 'orderby' => 'ID', 'order' => 'desc');
    $categories = get_categories( $args );
    foreach  ($categories as $category) {
    	echo '<p><strong><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "%s" ), $category->name ) . '" ' . '>' . $category->name.'</a></strong>';
    	if ( $cat_desc = $category->description ) echo '<br />'. $cat_desc .''; else echo '';
    	echo '<br />Posts in this subcategory: '. $category->count .''. $comments .'';
    	foreach (get_posts('cat='.$category->term_id.'&order=asc&orderby=date&posts_per_page=1') as $post) {
    		setup_postdata( $post );
    		echo '<br />'.get_the_date('j F Y').': <a href="'.get_permalink($post->ID).'">'.get_the_title().'</a>';
    	}
    	echo '</p>';
    
    // for the FIRST variant
    
    	foreach (get_posts('cat='.$category->term_id.'&order=asc&orderby=date&posts_per_page=1') as $post) {
    		setup_postdata( $post );
    		echo 'Active since '.get_the_date();
    	}
    	foreach (get_posts('cat='.$category->term_id.'&order=desc&orderby=date&posts_per_page=1') as $post) {
    		setup_postdata( $post );
    		echo ' till '.get_the_date();
    	}
    
    // and for the SECOND variant
    
    	foreach (get_posts('cat='.$category->term_id.'&order=asc&orderby=date&posts_per_page=1') as $post) {
    		setup_postdata( $post );
    		echo 'Years active: '.get_the_date(Y);
    	}
    	foreach (get_posts('cat='.$category->term_id.'&order=desc&orderby=date&posts_per_page=1') as $post) {
    		setup_postdata( $post );
    		echo '-'.get_the_date(Y);
    	}
    }
    ?>

    You may also list some or all (by adding the number which is surely larger than maximum imaginable number of posts into appropriate variable) posts in each subcategory by including the following snippet:

    	foreach (get_posts('cat='.$category->term_id.'&order=desc&orderby=date&posts_per_page=1') as $post) {
    		setup_postdata( $post );
    		echo '<br />'.get_the_date('j F Y').': <a href="'.get_permalink($post->ID).'">'.get_the_title().'</a>';
    	}

    Mind the variables.

    Thread Starter YK

    (@yurikacharava)

    As it is almost my first experience in PHP coding the result might appear not so elegant as it could be. But it works.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display dates of the earliest and the latest posts in each subcategory’ is closed to new replies.