WordPress.org

Ready to get started?Download WordPress

Forums

Showing and navigating posts by date/week... can it be done? (26 posts)

  1. nirurin
    Member
    Posted 1 year ago #

    I am attempting to design a blog section for my site, and the design I would *like* to have is causing me some issues...

    This is what I want it to end up being:

    My homepage will show the current weeks blog entries. The first "main" one will be the weekly update information, and will always be the first one on a week.
    Below that, posted at various times during the week, will be the occasional more "minor" updates, posted by me or by other authors.
    So the home page will have, for example, the Major News post, an Update Post from another author, and a Minor News post from later in the week.

    This is where it seems to get more complicated. Once the week has passed, and it is time for a new Major News post, I want all three of those previous posts to stay together, but go to a previous page.

    For example:
    Home page = week beginning 10th march - Major News, Author Update, Minor News
    Then, if you press "back" (or "previous") on a navigation button:
    Archive page = Week beginning 3rd MArch - Major News, Minor News
    Press "previous" again and the page should show all the posts for:
    Week beginning 26th Feb - Major news, Author Update, Author Update

    .... and so on, and so forth.

    The problem I'm having when searching for this solution, is that the custom navigation plugins seem to only allow for showing a single post at a time... I want to be able to navigate back and forth between pages of several posts, but to only show posts from a specific time frame.

    Is this actually possible? I'm hoping I have simply been searching the forums using the incorrect terminology...

    Thankyou to anyone who can help me!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Are "Major News" ,"Update Author Post" and "Minor News" categories?

  3. nirurin
    Member
    Posted 1 year ago #

    oh, no, thats just what I was calling the posts. "Major news" would be the first post every week, itd be the main one that I would write, and it would be there every week.
    The other two are things that wont necessarily be there every week, such as if someone else wanted to post up a piece of news, or if I posted up a small update.

    If using some sort of categories would make things easier, and could be implemented easily enough for others to use too, then I'm happy to use them.
    I did wonder if making each "week" of posts a category would help... so if another author wanted to post up some news, theyd just have to make sure it was connected to the right category... I'm not sure if this is easy though, or if it would even work the way im thinking...

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    There are no WordPress pagination functions that do that, so we have to create our own. Try it with this in your theme's functions.php: http://pastebin.com/gM5hBfKd

    Show the pagination functions in your index.php like this:

    <?php
    previous_week_link();
    next_week_link();
    ?>

    Or if you want to change the default labels "Previous Week", "Next Week":

    <?php
    previous_week_link('See previous week posts');
    next_week_link('See next week posts');
    ?>

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

  5. nirurin
    Member
    Posted 1 year ago #

    ahh thank you, I will definately look into this, it seems promising! I plan on using a child theme file for all the customisations I do, for the very reason that I want to keep them all safe!

    Just reading through that file now, trying to get my head around it. ill let you know if t worrks, I'm sure others will find it useful too!

    thank-you again

  6. nirurin
    Member
    Posted 1 year ago #

    OK... I believe I did this correctly, but it does not seem to have worked (at least as far as I can see).

    I made a funtions.php in my child theme folder, and pasted that entire page from pastebin into it.
    I then pasted the index.php from the main theme into my child theme, and added those echo lines into it.

    but when I load up the front page, it just shows the most recent posts, with no pagination links.

    I'm guessing ive posted the lines in incorrectly, but im not sure where else they should go

  7. nirurin
    Member
    Posted 1 year ago #

    Oh... I do apologise, it turns out it does show up properly, the only reason it *wasnt* was because I didnt have any previous weeks for it to navigate to.

    As soon as I backdated a post to a month ago, a "previous" link appeared on the front page!

    So thankyou, it seems to be working as it should.

    Now I am going to need to find a way to have the "next" and "previous" links permenantly on the page, just greyed out if they dont link to anything.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    If you change this:

    function next_week_link( $label = 'Next Week' ) {
    	echo get_week_link( $label, 'next' );
    }
    
    function previous_week_link( $label = 'Previous Week' ) {
    	echo get_week_link( $label, 'previous' );
    }

    To this:

    function next_week_link( $label = 'Next Week', $display = true ) {
    	if ( $display )
    		echo get_week_link( $label, 'next' );
    	else
    		return get_week_link( $label, 'next' );
    }
    
    function previous_week_link( $label = 'Previous Week', $display = true ) {
    	if ( $display )
    		echo get_week_link( $label, 'previous' );
    	else
    		return get_week_link( $label, 'previous' );
    }

    You can test if a link is returned. And if no link is returned echo the label:

    <?php
    $prev =  previous_week_link( 'Previous Week', false );
    $next =  next_week_link( 'Next Week', false );
    
    echo ( $prev != '' ) ? $prev : 'Previous Week'; // echo label if nothing is returned
    echo ( $next != '' ) ? $next : 'Next Week';
    ?>
  9. nirurin
    Member
    Posted 1 year ago #

    After a bit of a false start with this (for some reason, the first time i copied the code, a load of & symbols got included) I manage to get it all inserted properly, and it seems to be working wonderfully!

    Thankyou very much, all your help is really appreciated!

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

  11. nirurin
    Member
    Posted 1 year ago #

    I'm not sure if you are still keeping track of this thread, but an unfortunate flaw has come up with this solution.

    I have been trying to exclude certain categories from my home page loop, as I dont want every type of new post appearing on my home page. In order to do this, I installed the this plugin.

    However, even if I only exclude one category from the home feed, the posts from previous weeks which are fine... do not appear. In fact, the message "Ready to publish your first post? Start Here" appears instead.

    I would say this might be a plugin issue, but the plugin doesnt seem to have issues like that. Besides, I did recently have to attempt to work around another problem which I think this navigation caused.

    This second problem was, that I wanted to change the order of posts on the homepage from descending to ascending (or ascending to descending... whichever is the opposite of default). However, whichever method I used to change the order, worked on the current weeks posts fine.... but when I clicked on the "previous week" button, the previous week would appear blank. No posts from that week would show.

    Im not sure where the conflict is appearing, I am hoping someone will be able to help me with a fix as I do really prefer the weekly navigation over the wordpress system.

    Thankyou.

  12. Andrew
    Forum Moderator
    Posted 1 year ago #

    Bumps & duplicate threads will be deleted.

  13. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this in your theme's functions.php: http://pastebin.com/P469Sid2
    And change the category ids you want to exclude in your home page (and home page rss):

    $my_exclude_categories = array( 1, 3 );
    $my_exclude_categories_rss = array( 1, 3 );
  14. nirurin
    Member
    Posted 1 year ago #

    Well once again you have come up with the fix I needed, thankyou so much!

    I assume I was meant to delete the original changes to my funtions.php and replace them with the one you just posted? Thats what I did and it seems to be working anyway.

    Will this new version also allow me to change the posting order on the front page? I would prefer them to be listed oldest -> newest for each week... though if this is a chore, I can simply change the publishing dates retroactivly so that it shows up in the order I want.

    Thankyou again anyway, at least I'm now able to exclude certain categories!

  15. keesiemeijer
    moderator
    Posted 1 year ago #

    Try changing this:

    if ( is_home() ) {
                            $query->set( 'nopaging', true );
                            $query->set( 'category__not_in', $my_exclude_categories );
                            add_filter( 'posts_where', 'new_posts_where' );
                    }

    To this:

    if ( is_home() ) {
                            $query->set( 'nopaging', true );
                            $query->set( 'category__not_in', $my_exclude_categories );
                            $query->set( 'order ', 'ASC' );
                            add_filter( 'posts_where', 'new_posts_where' );
                    }
  16. nirurin
    Member
    Posted 1 year ago #

    didnt seem to make any difference... I tried changing ASC to DESC but btoh ways just formats the homepage to the default order of newest first.

  17. keesiemeijer
    moderator
    Posted 1 year ago #

    Try removing the space in 'order '

    $query->set( 'order', 'ASC' );
  18. nirurin
    Member
    Posted 1 year ago #

    Haha, typical, I hadnt even noticed there was an extra space. Thanks again, you sir are a legend.

  19. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome.

  20. nyubbie
    Member
    Posted 1 year ago #

    This post was exactly what I needed... thanks keesiemeijer!

    Also (to sidetrack a bit), I understand that this custom functions are to be used with the standard WordPress Loop. I tried WP_Query, but it isn't displaying anything.

    Is there a difference between the standard Loop and a WP_Query Loop?

  21. keesiemeijer
    moderator
    Posted 1 year ago #

    Is there a difference between the standard Loop and a WP_Query Loop?
    Yes, WP_Query is usually used for secondary loops.
    see:
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts
    http://www.billerickson.net/customize-the-wordpress-query/
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

    The functions use the pre_get_posts hook to query the loop.

    You could try using WP_Query like this [untested].

    global $my_exclude_categories;
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args =  array(
    'category__not_in' => $my_exclude_categories,
    'paged' => $paged,
    );
    add_filter( 'posts_where', 'new_posts_where' );
    $query = new WP_Query( $args );
    remove_filter( 'posts_where', 'new_posts_where' );
    // loop goes here

    And remove this line:

    add_action( 'pre_get_posts', 'weekly_pagination' );
  22. nyubbie
    Member
    Posted 1 year ago #

    Awesome information!

    I'm trying out the WP_Query you're providing, but it doesn't seem it work. Right now I'm tweaking the code to see if I can achieve anything with it, but while I'm actually trying to contain the weekly pagination to a single WP_Query itself, I have another WP_Query that displays 'featured' content on the same page.

    Pagination is giving me a problem as well, every time I go to /page/2, both content queries go to the next page. I just want one to change. Is it even possible?

    EDIT: I actually found a question on SE that answered whether its possible, I'm giving it a try now to see if it works.

    Link's here : http://wordpress.stackexchange.com/questions/47259/multiple-wp-query-loops-with-pagination

  23. nyubbie
    Member
    Posted 1 year ago #

    Right, right now I understand that because of this function,

    add_action( 'pre_get_posts', 'weekly_pagination' );
    function weekly_pagination( $query ) {
    	if ( !is_admin() && $query->is_main_query() ) {
    		if ( is_home() ) {
    			$query->set( 'nopaging', true );
    			add_filter( 'posts_where', 'new_posts_where' );
    		}
    	}
    }

    this actually modifies the entire loop for the index.php page. However, is there a way to limit if( is_home() ) to just a selected loop? Because right now I have 2 queries on my homepage, and this is changing the secondary loop (I'm using the original Loop for the weekly posts) into a weekly one as well.

  24. keesiemeijer
    moderator
    Posted 1 year ago #

    I've tested it and this seems to work:

    <?php
    global $my_exclude_categories;
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args =  array(
    'category__not_in' => $my_exclude_categories,
    'nopaging' => true
    );
    add_filter( 'posts_where', 'new_posts_where' );
    $query = new WP_Query( $args );
    remove_filter( 'posts_where', 'new_posts_where' );
    ?>

    Can you post the full code with both loops.

  25. nyubbie
    Member
    Posted 1 year ago #

    Your new loop works perfectly! With that fixed, my first loop is working normally now.

    All I need right now is for the first loop to display the pagination separately from the second loop.

    Here's the code with both loops, as you requested (if you want a reference).

    <div id="body" class="full-block">
        	<div id="main-content">
    			<?php $featured_query = new WP_Query(array( 'category_name' => 'featured', 'posts_per_page' => 3, 'paged' => ( get_query_var( 'paged' ) ))); while( $featured_query->have_posts() ): $featured_query->the_post(); ?>
                	<img class="featured-image" src="" />
                    <span class="featured-title"><?php the_title(); ?></span>
    			<?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
            </div>
            <br /><br /><br />
            <div id="sub-content">
                <?php
    		global $my_exclude_categories;
    		$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    		$args =  array(
    		'category__not_in' => $my_exclude_categories,
    		'nopaging' => true
    		);
    		add_filter( 'posts_where', 'new_posts_where' );
    		$query = new WP_Query( $args );
    		remove_filter( 'posts_where', 'new_posts_where' );
    	    ?>
                <?php while( $featured_query->have_posts() ): $featured_query->the_post(); ?>
                	<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <p><?php the_content(); ?></p>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
                <?php previous_week_link() . next_week_link(); ?>
            </div>
        </div>

    I can't thank you enough for helping me with this.

  26. Dan Hannigan
    Member
    Posted 1 year ago #

    Not sure if I should piggyback on this or move to a new thread but I'm trying to get similar functionality worked out but instead of weekly it would be daily. The navigation being next day, previous day, and so on.

    I've been searching for daily pagination for a few days and have come up short. I can get the pagination part to work - displaying links and such - but then it doesn't query for posts from that day. Just using a simple loop to run it:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $today = getdate();
    query_posts("year=$today[year]&monthnum=$today[mon]&day=$today[mday]&paged=$paged&orderby=date&order=asc");
    if (have_posts()) :
       while (have_posts()) : the_post();
    ?>

    It's a little daily events calendar type thing.

    Let me know if I need to move this to a new thread too...

Topic Closed

This topic has been closed to new replies.

About this Topic