4

Alright, this might be a tricky one to explain but I'll do my best.

At the front page I want to display all the posts from all the categories. However I want them to be divided. Take this for an example:

enter image description here

The years are actually categories. Each category represents a different year. So I insert my posts (which only display the thumbnail image in the loop) in the category that applies to when the photos were taken.

At the start of each category I want it to show the category's name, as displayed above. I'm also using the jQuery infinite-scroll from Paul Irish, so basically the pagination buttons are replaced with the loading bar, and it just loads at the same page when the user scrolls down.

My current code is:

<div class="post clear">
    <a href="<?php echo get_category_link(4); ?>"><div class="date"><?php echo get_cat_name(4); ?></div></a>
    <?php
    $catPost = get_posts('cat=4&posts_per_page=-1');
    foreach ($catPost as $post) : setup_postdata($post); ?>
        <?php get_template_part('content'); ?>
    <?php endforeach;?>
</div>
<div class="post clear">
    <a href="<?php echo get_category_link(1); ?>"><div class="date"><?php echo get_cat_name(1); ?></div></a>
    <?php
    $catPost = get_posts('cat=1&posts_per_page=-1');
    foreach ($catPost as $post) : setup_postdata($post); ?>
        <?php get_template_part('content'); ?>
    <?php endforeach;?>
</div>

And it's actually working. Well, almost. First of all I have to insert category by category everytime I create a new one, with most of the code being duplicated. The only thing that changes are the categories' IDs. It would be much better if with only one code I could retrieve all categories the way I want.

Also, another problem, is that pagination doesn't work. If I scroll down (or disable infinite-scroll and click on the second page) it'll just show repeated content over and over again.

I think you guys got the idea. I want users to be able to browse my gallery and everytime a new set of photos from a different year pops up, the date will appear on the first image. I came up with this solution of categories but it's becoming a little hard to make it work the way I want.

Any solutions/suggestions?

Thanks in advance!

EDIT @bestprogrammerintheworld

<?php
    $args=array(
      'posts_per_page' => -1,
      'orderby' => array( 'date' => 'DESC', 'title' => 'ASC' ),  //This works in WP4.0!!!     
      'caller_get_posts'=> 1
    );
    $my_query = new WP_Query($args);


    if( $my_query->have_posts() ) {
      $nr_datesyear = 0;
      while ($my_query->have_posts()) : $my_query->the_post();

        $year = get_the_date('Y');


        if ($year !== $yeargroup && $nr_datesyear <=5) {
            $nr_datesyear = 0;  
            echo '<div class="post clear">';
        }
        echo '<a href="'. $year .'"><span class="date">'. $year .'</span></a>';                  
        get_template_part('content');

        if ($year !== $yeargroup && $nr_datesyear <=5) {
            echo '<div>'; //end post year group
            $yeargroup = $year;   
        }

        $nr_datesyear++;  
            endwhile;            
        }

        wp_reset_query();
?>
  • 2
    I wouldn't use a new category for a year/date/month etc. Just put posts in there and filter out post based on year. – bestprogrammerintheworld Sep 15 '14 at 18:40
  • @bestprogrammerintheworld thanks, I'm also open for suggestions like that! Do you think you can assemble a working code for that purpose? Thanks. – coldpumpkin Sep 15 '14 at 18:51
  • This is not "do coding for me site". Please try and show your attempt, then I will help you further. – bestprogrammerintheworld Sep 15 '14 at 18:56
  • I'm not really a programmer, I'm just trying to assemble things here and there. I extracted something from wordpress.org/support/topic/… however I can't find a place to insert the <div class="post clear">. See my edit for the code I got. – coldpumpkin Sep 15 '14 at 19:12
  • PS: Also it doesn't work with pagination. Tried searching but didn't find anything... – coldpumpkin Sep 15 '14 at 19:28
0

To be able to order by category, you have to intercept the MySQL Query.

Start with pre_get_posts:

add_action('pre_get_posts','setup_my_query_interception');

function setup_my_query_interception($query){
    if(!is_admin() && $query->is_home() && $query->is_main_query()){
     add_filter( 'posts_clauses', 'order_by_category_intercept_query_clauses', 20, 1 );
  }
}

Now we enhance the MySQL Query to include the term and taxonomy tables:

function order_by_category_intercept_query_clauses($pieces){
        global $wpdb;
        $pieces['join'].=" LEFT JOIN $wpdb->term_relationships trt ON ($wpdb->posts.ID = trt.object_id) LEFT JOIN $wpdb->term_taxonomy ttt ON (trt.term_taxonomy_id = ttt.term_taxonomy_id) INNER JOIN $wpdb->terms termts ON (termts.term_id = ttt.term_id)";
        $pieces['where'].=" AND ttt.taxonomy = 'category'";
        $pieces['orderby']="termts.name ASC, ".$pieces['orderby'];
        //Remove the filter so it only runs once in our main query
        remove_filter('posts_clauses', 'order_by_category_intercept_query_clauses');
        return $pieces;
}

This should result in your main query being put out ordered first by your Category name (pagination should still work). When doing the loop, you can group your categories like this:

...before the loop:

global $activetax;
$activetax = "";
echo '<div class="mywrapper">

...in the loop:

$cats = get_the_terms(get_the_ID(),'category');
if(is_array($cats)){
    if(!($activetax==$cats[0]->name)){
            $activetax = $cats[0]->name;
            ?>
            </div>
            <h2 id="tax_id_<?php echo $cats[0]->term_id; ?>" class="cat-header"><?php echo $activetax; ?></h2>
            <div class="mywrapper">
            <?php
    }
}
get_template_part('content'); 

... after your loop

echo "</div>";

Didn't test it, but should work ;)

Happy Coding.

-1

You can also use this code for display category and post (if you are using default category and post of WordPress

<?php 
        $terms = get_terms( 'category', array(
        'hide_empty' => false,
        'order_by' => 'name',
        'order' => 'ASC',
        'number' => 100,
        'taxonomy' => 'category'
    ) );

         //print_r($terms);

         foreach($terms as  $termss)
        {
        //echo "<br/>";
         //echo $termss->term_id;
            //echo "<br/>";
            ?>
        <h1> <?php echo $termss->name; ?></h1>
         <?php 
            //echo "<br/>";
          //echo $termss->slug;


           $query =  query_posts("category_name='$termss->name'&showposts=5");
        //print_r($query);
        foreach($query as  $querys)
        {
        //$querys->ID;
        ?>
        <h3><?php echo $querys->post_title; ?></h3>

        <p><?php echo $querys->post_content; ?></p>

    <?php   //echo "<br/>";
        }


        }
         ?>
  • 2
    Is this working with pagination? – coldpumpkin Sep 26 '14 at 10:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.