views:

33

answers:

1

Hi all,

I'm trying to link posts in wordpress. Let me elaborate. In wordpress, you can set how many posts you want to show on a page. If, for example, you set that number to 1, and you have 10 posts, then you'll have 10 pages.

I have the following code that WORKS on the index.php (main posts page). It shows a link to older posts. Below is the code:

    <ul id="postNavigation">
    <li class="floatLeft"><?php next_posts_link('Old posts &raquo;') ?></li>
    <li class="floatRight"><?php previous_posts_link('&laquo; New posts') ?></li>
</ul>

Now, the way I have my wordpress set up, I have another page where I show posts with a certain category using this code (this code is in my page.php file):

   <?php if ( is_page('newspaper') ) { // BEGINNING of newspaper page
    ?>
<?php $my_query = new WP_Query('category_name=newspaper'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post">
    <!--<h3><?php the_title(); ?></h3>-->
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    <div class="clear"></div>
    </div>
    <?php endwhile; ?>

    <ul id="postNavigation">
        <li class="floatLeft"><?php next_posts_link('Old Posts &raquo;') ?></li>
        <li class="floatRight"><?php previous_posts_link('&laquo; New Posts') ?></li>
    </ul>

    <?php } //END of newspaper page
    ?>

However, it doesn't show the "Older posts" link there. Does anyone have any idea if and how this is possible to accomplish?

Thanks, Amit

A: 

I assume that the older next post link is the only one missing. That would mean that your newer posts link is visible. How are you sorting your posts? Maybe you are looking at the oldest posts, so there are no older posts to show?

try

<?php $my_query = 
     new WP_Query('category_name=newspaper&orderby=date&order=desc');?>

This will organize posts from newest to oldest, and should enable the link to older posts. you could also try orderby=ID to use post id's instead of dates

kevtrout
Hmm.. I think both links are missing. I'm not sure. Howcome you assumed that only the older one is missing?
Amit
When I put the post navigation links, there's just nothing generated. If I set wordpress to show 1 post per page and there are 10 posts, it means that 9 of them will not be shown (without a chance to navigate to them). I'm not sure if this is fixable since this is not dealing with the index.php file, but rather with the page.php file (since this is not the blog's main post page, aka is_home(), this is a is_page() if that makes any sense to you...) Soo..for now, I just set it so that the posts_per_page=-1, so that ALL posts are shown on that page.
Amit
If you have any suggestions, I'd love to hear them. For now, I'll give your code a try as well.
Amit
Hey...I tried your code. It didn't work :(
Amit
I assumed the older posts link was the only one missing because that's the only one you mentioned in your question. What theme are you using? Does it have a category.php file?
kevtrout
One more question...you stated you're using page.php to show the posts from the 'newspaper' category. Why not use a category.php file to show the posts from this category? Wordpress is designed to work this way, and may have something to do with your next posts link not working. WordPress treats Pages as areas for static content so I'm not sure it allows next-post and prev-post navigation. I believe you can set up a loop just as your are on your page, on a category page and the navigation might work, magically.
kevtrout
Well. It's a slightly complicated problem. The thing is, I have a page that is supposed to show posts with category of 'newspaper'. The thing is, the page is actually a page, not a category. It would be awesome if it were a category page, but it cant be, since it's built into the navigation menu. If it were to be a category page, I would have to hard-code the navigation menu, because this page is under a dropdown menu. Ugh, I don't think this problem can have a fix. And I think you're right in that the previous_post_link() function doesn't work on the page.php file.
Amit
Therefore, I've marked your answer as the correct one. In the meantime, I've just set it so that it shows all posts in that page. I'll have to stay that way I guess. For those interested, to show all posts, this is the code I've modified:<?php $my_query = new WP_Query('category_name=newspaper ?>
Amit
If you are comfortable changing the site navigation from pages to categories, you can use wp_list_categories()( http://codex.wordpress.org/Template_Tags/wp_list_categories) to show categories as a list, then style the list to match your current page-based navigation design. On the sites I create with WordPress, I've learned to stick with category based navigation because I think it works better than page based navi.
kevtrout