• Resolved Bocq

    (@bocq)


    Bonjour,

    The result I need is:
    Category Name (as title of my page)
    Year 2010
    Author A
    Author B
    Author C
    Year 2009
    Author A
    Author B
    Author C

    Now it looks like this: https://valentine-meunier.de/wordpress/?cat=13
    (the articles are not alphabetically ordered).

    I’ve been searching for a long time but I can’t find the way to get back the year as a variable to use it within a “foreach”.
    I hope it will interest someone .
    Merci!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Can you show us the foreach loop?

    Thread Starter Bocq

    (@bocq)

    ok, but please don’t laugh (or cry)

    <h2 class="pagetitle"><?php echo category_description(); ?></h2>
    <?php $one_year = get_the_time('Y'); ?>
    <?php foreach($one_year as $year) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<?php echo get_year_link($one_year); ?>">Archive for the year <?php the_time('Y') ?>
    	<?php query_posts($one_year. 'orderby=title&order=ASC' );?>
    	<h2 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
    	<p><?php the_content() ?></p>
    	<?php endwhile; ?>
    <?php endforeach; ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    <?php $one_year = get_the_time('Y'); ?> Can only be used inside of the loop and doesn’t return an array so you can’t do a foreach on it. But from what I understand you retrieve the “year” from the post date.

    Thread Starter Bocq

    (@bocq)

    All right. The following code is still wrong, but could you tell me if it is the direction I should search in?

    <?php $one_year = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC"); ?>
    	<?php foreach($one_year as $year) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    			<?php query_posts($one_year. 'orderby=title&order=ASC' );?>
    			<p><?php the_date('Y') ?></p>
    			<h2 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
    			<p><?php the_content() ?></p>
    		<?php endwhile; ?>
    	<?php endforeach; ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    try this as your loop:

    <?php $my_query = new WP_Query('posts_per_page=-1orderby=title&order=ASC'); ?>
    <?php $temp_year = get_the_time( 'Y', $my_query->posts[0]->ID );
    $i=0;
     ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php
    if ($i == 0) : ?>
    <p><a href="<?php echo get_year_link($temp_year); ?>"><?php echo $temp_year; ?></a></p>
    <?php endif; ?>
    <?php if($temp_year != get_the_time('Y')) : ?>
    <p><a href="<?php echo get_year_link(get_the_time('Y')); ?>"><?php echo get_the_time('Y'); ?></a></p>
    <?php endif; ?>
    <h2 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
    <p><?php the_content() ?></p>
    <?php $temp_year = get_the_time('Y'); $i++; ?>
    <?php endwhile; ?>

    get_the_time outside of the loop with a post ID will work

    getting the years and the foreach is absolutely right.

    try to change this line:
    <?php query_posts($one_year. 'orderby=title&order=ASC' );?>
    into:
    <?php query_posts('year='. $year. '&orderby=title&order=ASC' );?>
    https://codex.www.ads-software.com/Function_Reference/query_posts#Time_Parameters

    and reshuffle the code a little bit (move the query out of the while loop, adding the year before the loop)

    <?php $one_year = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC"); ?>
    	<?php foreach($one_year as $year) : ?>
    	<?php query_posts('year=' .$year. '&orderby=title&order=ASC' );?>
    	<?php echo '<h2>'.$year.'</h2>'; ?>
    		<?php while (have_posts()) : the_post(); ?>
    			<!--<p><?php the_date('Y') ?></p>-->
    			<h2 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
    			<p><?php the_content() ?></p>
    		<?php endwhile; ?>
    	<?php endforeach; ?>
    Thread Starter Bocq

    (@bocq)

    Thanks both of you!
    I have only tried alchymyth’s solution for now.
    Now I only have to find my category again. I’m going ahead!

    Also another possibility https://www.ads-software.com/support/topic/337364
    –you’d have to add the category filter and eventually the sort by author logic in each year.

    Thread Starter Bocq

    (@bocq)

    Bonjour!
    I’ve tried the 3 solutions and finally used the one of Michael in the topic 337364
    To filter the actual category, I have included a variable
    <?php $currentCat = get_query_var(‘cat’); ?>
    and an argument
    ‘category__in’ => array($currentCat)

    It works!
    Sometime, a lonely title get lost and doesn’t appear properly alphabetically. I’ve search for forgotten spaces in the titles but could not find any real reason.

    Anyway, THANKS a lot (I will check If I can also bring a contribution by the translations into French).

    Here is the result https://valentine-meunier.de/wordpress/

    And here is the code

    <?php $currentCat = get_query_var('cat'); ?>
    <?php
    $args=array(
        'orderby' => 'date',
        'order' => 'ASC',
        'posts_per_page' => 1,
        'caller_get_posts'=>1,
    
    );
    $oldestpost =  get_posts($args);
    
    $args=array(
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 1,
        'caller_get_posts'=>1,
    
    );
    $newestpost =  get_posts($args);
    
    if ( !empty($oldestpost) && !empty($newestpost) ) {
      $oldest = mysql2date("Y", $oldestpost[0]->post_date);
      $newest = mysql2date("Y", $newestpost[0]->post_date);
    
      for ( $counter = intval($newest); $counter >= intval($oldest); $counter = $counter - 1)  {
    
        $args=array(
          'year'     => $counter,
          'posts_per_page' => -1,
          'orderby' => 'title',
          'order' => 'ASC',
          'caller_get_posts'=>1,
    	  'category__in' => array($currentCat)
    
    );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '<h2 class="jahr">' . $counter . '</h2>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	    <h2 class="author"><?php the_title(); ?></h2>
     				<div class="entry">
    					<?php the_content() ?>
    				</div>
          <?php
            //the_content('Read the rest of this entry &raquo;');
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
      }
    }
    ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to list articles of a category alphabetical within one year’ is closed to new replies.