WordPress.org

Support

Support » How-To and Troubleshooting » One page per year. Category

One page per year. Category

  • DaniSainz
    Member

    @danisainz

    Hi!
    I want one page in category to each year.
    I have category.php and only category (“All”).

    First Page (category.php):
    Year 2014:
    Februar
    – 9 Post
    Januar
    – 8 Post
    – 7 Post
    – 6 Post

    Second Page (category.php):
    Year 2013
    December
    – 5 Post
    November
    – 4 Post
    -3 Post
    …..

    I´ve readed this post but i want one page-one year

    Thanks!

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

    @keesiemeijer

    Try it with a plugin:
    http://keesiemeijer.wordpress.com/date-pagination/

    Activate the plugin and put this in your theme’s functions.php to paginate categories by year:

    add_action( 'pre_get_posts', 'yearly_paginated_home_query' );
    function yearly_paginated_home_query( $query ) {
    
        // not a wp-admin page and the query is for the main query
        if ( !is_admin() && $query->is_main_query() ) {
    
            //  paginate by year on category pages
    
            if ( is_category() ) {
    
                // 'yearly', 'monthly', 'daily'
                $query->set('date_pagination_type', 'yearly');
    
                // set other parameters here
            }
    
        }
    }

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    DaniSainz
    Member

    @danisainz

    First, thanks to your answer!

    Have you tested this plugin?

    I can´t see anything.
    I put your code in function.php and active the plugin.
    Also include in my category.php only “echo km_dp_get_current_date_label()” to see the actual date, but this don´t work for me.

    keesiemeijer
    Moderator

    @keesiemeijer

    I’ve got this working on my testsite.

    Can you post a link to a category page?

    DaniSainz
    Member

    @danisainz

    Yes, i can use a normal category pagination:

    <?php
    global $wp_query;
    $args = array_merge( $wp_query->query, array('posts_per_page'=> 2 ) );
    query_posts( $args );
     if (have_posts()): ?>
        <h2><?php echo "DATE: ".km_dp_get_current_date_label(); ?></h2>
              <?php while (have_posts()): the_post(); ?>
                <div id="post">
                  <div class="post-title">
                    <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
                  </div>
                  <div class="post-content"><?php the_excerpt(); ?></div>
                </div>
              <?php endwhile; ?>
    
            <div class="navigation">
              <div class="alignleft static-scroll"><?php next_posts_link('&laquo; Alte Beiträge') ?></div>
              <div class="alignright static-scroll"><?php previous_posts_link('Neue Beiträge &raquo;') ?></div>
            </div>
    <?php else: ?>
            <p><?echo __("Keine Nachricht"); ?></p>
    <?php endif; ?>

    but when i use not working:

    <?php
    global $wp_query;
    $args = array_merge( $wp_query->query, array('posts_per_page'=> 2 ) );
    query_posts( $args );
     if (have_posts()): ?>
        <h2><?php echo "DATE: ".km_dp_get_current_date_label(); ?></h2>
              <?php while (have_posts()): the_post(); ?>
                <div id="post">
                  <div class="post-title">
                    <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
                  </div>
                  <div class="post-content"><?php the_excerpt(); ?></div>
                </div>
              <?php endwhile; ?>
              <?php
    
              $next_label = 'Previous Month';
              $prev_label = 'Next Month';
    
              if ( function_exists( 'km_dp_get_next_date_label' ) ) {
                  $next_label = km_dp_get_next_date_label( 'F Y' );
              }
    
              if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
                  $prev_label = km_dp_get_previous_date_label( 'F Y' );
              }
              ?>
    
            <div class="navigation">
              <div class="alignleft static-scroll"><?php next_posts_link($next_label) ?></div>
              <div class="alignright static-scroll"><?php previous_posts_link($prev_label) ?></div>
            </div>
    <?php else: ?>
            <p><?echo __("Keine Nachricht"); ?></p>
    <?php endif; ?>

    I´ve checked the if(function_exists(‘km_dp_get_nex_date_label’)) and work.

    keesiemeijer
    Moderator

    @keesiemeijer

    Try it without the query, remove this

    global $wp_query;
    $args = array_merge( $wp_query->query, array('posts_per_page'=> 2 ) );
    query_posts( $args );

    DaniSainz
    Member

    @danisainz

    I have checked this option but it doesn´t work.

    I think this line <?php echo "DATE: ".km_dp_get_current_date_label(); ?> should be enough to checked…

    I don´t know what is the problem, I have the code in category.php and a normal pagination ist right.
    The plugin is installed and the function

    add_action( ‘pre_get_posts’, ‘yearly_paginated_home_query’ );
    function yearly_paginated_home_query( $query ) {

    // not a wp-admin page and the query is for the main query
    if ( !is_admin() && $query->is_main_query() ) {

    // paginate by year on category pages

    if ( is_category() ) {

    // ‘yearly’, ‘monthly’, ‘daily’
    $query->set(‘date_pagination_type’, ‘yearly’);

    // set other parameters here
    }

    }
    }
    is in function.php

    keesiemeijer
    Moderator

    @keesiemeijer

    Is it in function.php or functions.php? It needs to go in functions.php.

    DaniSainz
    Member

    @danisainz

    Really is in global.php wich is included in functions.php.
    I have checked in functions.php too, but not.

    I’m totally frustated with this problem.

    keesiemeijer
    Moderator

    @keesiemeijer

    What theme are you using? And where did you download it from?

    DaniSainz
    Member

    @danisainz

    The theme is mine, i’ve made it totally.

    keesiemeijer
    Moderator

    @keesiemeijer

    Ok, can you remove the code from global.php/functions.php and use this for your category.php template:

    <?php
    // set the paged variable (see: http://codex.wordpress.org/Pagination )
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    // example args
    $args = array(
        // 'yearly', 'monthly', 'daily'
        'date_pagination_type' => 'yearly',
        'paged' => $paged,
    );
    
    // the custom query
    $the_query = new WP_Query( $args );
    ?>
    
    <?php
    
     if ( $the_query->have_posts() ) : ?>
        <h2><?php echo "DATE: " . km_dp_get_current_date_label('Y', $the_query); ?></h2>
              <?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
                <div id="post">
                  <div class="post-title">
                    <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
                  </div>
                  <div class="post-content"><?php the_excerpt(); ?></div>
                </div>
              <?php endwhile; ?>
              <?php
    
              $next_label = 'Previous Month';
              $prev_label = 'Next Month';
    
              if ( function_exists( 'km_dp_get_next_date_label' ) ) {
                  $next_label = km_dp_get_next_date_label( 'F Y' );
              }
    
              if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
                  $prev_label = km_dp_get_previous_date_label( 'F Y' );
              }
              ?>
    
            <div class="navigation">
              <div class="alignleft static-scroll"><?php next_posts_link($next_label, $the_query->max_num_pages ); ?></div>
              <div class="alignright static-scroll"><?php previous_posts_link($prev_label); ?></div>
            </div>
            <?php wp_reset_postdata(); ?>
    <?php else: ?>
            <p><?echo __("Keine Nachricht"); ?></p>
    <?php endif; ?>

    DaniSainz
    Member

    @danisainz

    thanks but neither serves me, I don’t know the conflict.
    Hopefully get fix… is really necesary

    DaniSainz
    Member

    @danisainz

    Hi again keesiemeijer!

    I’m trying run once again yours plugin, i have tried desactivating plugins, changing my code, but i don’t get.
    Maybe, do you know other solution with your plugin or other method?

    Thanks!

    DaniSainz
    Member

    @danisainz

    Some Opinion?

    keesiemeijer
    Moderator

    @keesiemeijer

    I have no idea why it shouldn’t work. Can you post the full code from your theme template.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘One page per year. Category’ is closed to new replies.