Navigating between pages of posts

Im rebuilding a site I was working on using roots (wish I had spotted it to begin with)
the ‘portfolio’ page displays 6 portfolio items of around 15, (fussy client doesnt want the page to scroll up :open_mouth:
im using the standard wp code

posts_nav_link();

at the bottom of the portfolio page, but nothing displays, even though the div is there.
i dont know if its a theme thing or bootstrap so apologies if I am asking in the wrong place.

thanks

There are a few paging options in WordPress. Read through the codex and make sure you are using the correct function for your template. For example posts_nav_link will not work on single templates.

by single do you mean a one off page template?
this exact code worked on the site before and but it just doesnt display anything at all, even though the code is there.
i have been through the codex and tried each of those options but i just dont get any links to previous or next, - they are there, just not visible if that makes sense. - i cant seem to post code here either it doesn’t display for me?

I borrowed and modified _s_content_nav() from _s.

Take a look at the function and see how they implemented it. _s is another great theme to learn from built by Automattic.

1 Like

The only important part of the code is the function that you are using, so I’ve edited the post to reflect that.

When I say single templates, I mean templates used to display a single post or page; as opposed to archives or indexes that loop through multiple entries.

If you use a single template to display an archive or index then you’ll need to make certain changes to the query or use a different function. The code from underscores is a good example of this; it uses the is_single conditional to check what kind of template is being used and alters the code accordingly.

With these kind of questions, it will usually help if you expand on how you have set up your data and/or provide your template code (with the loop).

Ive been at this a while, and this is as far as I have got. - this is now displaying a previous / next bar at the bottom, though i pinched this code from a previous site I made, its not the recommended way to display in columns as per the roots documentation ( I dont really know if its ok or not)

the only problem i have now, is its displaying the sidebar at the bottom. another issue again, but i have no idea where its getting it from.

all my problems are coming from the fact that my understanding of php and programming / scripting is very basic, something i am trying hard to rectify.
http://pastebin.com/GAQFsVg1

I feel like some of the code is missing. It might be a bug with discourse (noticed a recent update) but have you followed the guidelines for Github Flavored Fenced Code Blocks?

it is definitely skipping out loads of code, that is there when I post it, even when I go back in to edit the post its all there.
and then on refresh its gone. I will look at that now. Just half of what I am reading is still whooshing over my head unfortunately.

For now you can use https://gist.github.com or http://pastebin.com to paste your code

Could you send me a link to the offending page?

Ah, ok,

well I used the code you helped me with a while back here Horizontal Posts

you can see my page template below.
http://pastebin.com/S3WEEgmA

NOTHING I do paginates this page, - ive tried other code to display horizontal posts like so and its worked but it plonked a sidebar on the page, still haven’t grasped where that came from

thansk for looking!

so I changed my code to this:
http://pastebin.com/dfPnMYN5

this actually works fine, (almost)
Its displaying a sidebar. I cannot for the life of me work out how.
ive disabled sidebars both for that template and on the if_page in the config.php file as so:

 array(
  'is_404',
  'is_front_page',
  'is_page'
),
/**
 * Page template checks (via is_page_template())
 * Any of these page templates that return true won't show the sidebar
 */
array(
  'template-custom.php',
  'template-about.php',
  'template-portfolio.php'
)
  );

but its coming from somewhere :expressionless:

OK UPDATE
all my pagination is now lovely, but the sidebar is displaying, underneath my posts.
a bit of source inspection revealed the comment <!-- /.main --> which indicates its coming from base.php.

so how do I get it to stop using the sidebar on this particular page? - ive told config.php to ignore the sidebar on pages.

Try to avoid query_posts. The reasons are explained very well at the beginning of the codex page.

One thing I forgot to add to the WP_Query example I posted last week was wp_reset_postdata(). I’m editing the post to add that now.
Try putting that after your endwhile in your first version. I suspect that might be causing the problem with the sidebar.

edit: i’ve finally experienced the code display bug :slight_smile: take a look at the codex on wp_reset_postdata if you’re not sure how to use it

edit 2: I’ve updated your first pastebin with this tiny snippet: http://pastebin.com/vgKz8KNi

1 Like

Im stuck then - if i follow that code in the codex the posts still display nicely but none of the pagination codes work, they just don’t output anything. - oh and the sidebar isnt there either then, but no next and last either.

ive tried edit 2 and no pagination appearing with that either, - the only way i get it is with the ‘naughty’ code.

and tried the

php wp resetpostdata (sorry cant type that as code)

but I still get a sidebar.

im beginning to think things just do what they want to trial me :smiley: !

so im not sure which to do, try and stick with the one that works and try and get rid of the sidebar, or try to figure the one that doesn’t display any pagination.

ive also manually navigated to page two, and i do get a ‘newer’ link at the bottom of that page, but its still the same content, ie not an older set of posts.

Sorry, I forgot to take pagination into account.

Try this: https://gist.github.com/tamarazuk/a060d4fab09adba0105c

http://codex.wordpress.org/Function_Reference/next_posts_link explains how to use next_posts_link and previous_posts_link with WP_Query

2 Likes

Thank you,

http://pastebin.com/diff.php?i=8hGiYWrn

I added code to clear at the end of each row, and I added a line at the top that makes sure a new set of posts appears each time.

now this works except one small thing -
i only get the ‘newer link’ not the older, so i have no pagination on page one, only 2 and 3 to go back, if that makes any sense? weird!

On page 1 there is no ‘Newer’ posts so i don’t think it should appear.

Is everything else working as expected?

have you tried creating an archive-portfolio.php template instead to see if that works?

1 Like

sorry, i mean, on page 1 there is no link appearing to page 2. - as in ‘older’ isnt appearing. - in context it’s three pages of portfolio items. 15 items, but client only wants 6 per page.

so when you are on page 1, there is no link to view page 2 etc :slight_smile:

you can see here before the footer, there is no link, if I manually find page two it has a link to the previous page.

next_posts_link and previous_posts_link do not work on static pages: http://codex.wordpress.org/Function_Reference/next_posts_link#Notes

Is there a specific reason you are using a page template rather than the built in archive template for that post type?

I’ve updated my gist with an example of how the archive-portfolio.php file could look like in your case as well as updated your roots_display_sidebar function to show how to disable it for the portfolio post type archive

if your post type slug is ‘portfolio’ then you would find this archive at http://yoursite.com/portfolio

I’ve tested this (minus your ‘folio’ template part as I do not have it) and everything works

Update:

PS. All I did was duplicate index.php and rename it to archive-portfolio.php then add the .container and .col divs and changed the get_template_part line to include your ‘folio’ template part

2 Likes