0

I have a code which shows categories list with images. Im using plugin to attach image to category.

Working code is:

<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC',
)
?>
 <?php foreach (get_categories( $args ) as $cat) : ?>
<h3><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></h3>
<a href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
 <?php endforeach; ?>

Problem is theres a lot of categories. I want to separate them by year (just add a line or text or something). Is it even possible? I know that categories dont store date. Posts are posted only in categories of current year so maybe this could help?

  • Why do you have year categories? Could you not use date archives instead of categories named after years? – Tom J Nowell Nov 19 '16 at 21:04
1

I don't know what is the purpose of having each each categories for every month because WordPress has monthly archive. You can use it.

However, it is possible that what you are going to do.

I can't write the code here because of lack of time. But I can give a guide to develop it.

Read the comments in code section.

<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC',
)

$year="" // Assign a oldest year for this. If your first post is published in  2011, $year=2011
?>

<?php foreach (get_categories( $args ) as $cat) : 
    $cat_post_id=""; //Get the latest post id of current category and assign it to 
    $cat_post_publish_year=""; //Then find the post date of that post and assign the YEAR of that date (Only the year)

    if($year!==$cat_post_publish_year)
    {
        echo "<hr>";// Write anything to separate it.
        $year=$cat_post_publish_year;
    }


?>

    <h3><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></h3>
    <a href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>

<?php endforeach; ?>
  • Thx for help. WP monthly archive wont work for me because each month needs to have image assigned to it and this image is different for every month. – th3rion Nov 20 '16 at 10:27
  • With your help (and wordpress codex) I managed to make it work (maybe not in the nicest way but im not php developer. Thx again. – th3rion Nov 20 '16 at 16:24
  • @th3rion That is called as learning :-) There are many people here to get the full code from others. But you did it yourself after getting small hint. Great :-) I will check your code when I get a free time. :-) – Ranuka Nov 21 '16 at 7:10
  • Im quite suprised it worked :) Youve written about getting the latest post ID of current category and assigning it. I didnt know how to get last post ID but realised I dont need last - just need any of this category because for example september posts are always in september category of correct year. 'get_posts' got these posts IDs but returned array of them. I just need one so theres 'array_rand' to choose one of them. Thats why I said that I could be done better :) – th3rion Nov 21 '16 at 9:23
0

Here is full working code. Im not php developer, so there are probably some things that could and should be corrected but it works.

    <?php 

            $args=array('orderby' => 'ID', 'order' => 'DESC');
            $year="2005";

            foreach (get_categories( $args ) as $cat) : 

            $post_ids = get_posts(array(
                'posts_per_page'   => 1, 
                'tax_query'     => array(
                    array(
                        'taxonomy'  => 'category',
                        'field'     => 'id',
                        'terms'     => $cat,
                    ),
                ),
                'fields'        => 'ids', 
            ));

            $cat_post_id=$post_ids[array_rand($post_ids)];
            $cat_post_publish_year=get_the_date( 'Y', $cat_post_id);

            if($year!==$cat_post_publish_year)
            {

    ?>      
            <div style="display: block; clear: both; width: 100%; height: 20px; border-top: 1px solid #fff;"></div>

    <?php

            $year=$cat_post_publish_year; }

    ?>
            <div style="float: left;">

                <a href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
                <h3><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></h3>

            </div>

    <?php endforeach; ?>

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.