WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need code rewrite for my Previous/next post links to work for query posts (9 posts)

  1. jalacom
    Member
    Posted 2 years ago #

    Hi,
    I've got a main index page that uses a query post to show just category 4. This is the top of my loop currently:

    <?php $my_query = "showposts=10&cat=4"; $my_query = new WP_Query($my_query); ?><!-- Calls the main blogroll category -->
    	<?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?><!-- Starts the loop -->
    
    	<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    How would I rewrite it so my next and previous button links work?
    I've seen a lot of post on how query's don't act nice with the previous and next because of how it's pulling the content but I don't know enough about php to rewrite it to function correctly.
    Thanks in advance.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try and use the 'paged' parameter in the query:

    $my_query = "showposts=10&cat=4&paged=".get_query_var('paged');

  3. jalacom
    Member
    Posted 2 years ago #

    ah, ok I was just finding a post you had left on another forum question about the paged parameter. I'll try that out. Thanks!

  4. jalacom
    Member
    Posted 2 years ago #

    So this would go in the first line I wouldn't have to change the if and while statement?

  5. jalacom
    Member
    Posted 2 years ago #

    This is my full code:

    <?php $my_query = "showposts=10&cat=4"; $my_query = new WP_Query($my_query); ?>
    	<?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- CONTENT -->
    <?php endwhile; ?>
    
    <?php else : ?>
    <p class="no-posts"><?php _e('Sorry, no posts matched your criteria', 'example'); ?></p>
    <?php endif; ?>
  6. esmi
    Forum Moderator
    Posted 2 years ago #

    <?php$my_query = "showposts=10&cat=4&paged=".get_query_var('paged');
    $my_query = new WP_Query($my_query); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
    [...]
  7. jalacom
    Member
    Posted 2 years ago #

    Esmi,
    Thanks for the reply. I tried applying it that way, but it gives me an unexpected T_variable error on line 6. Which would be that line: <?php$my_query = "showposts=10&cat=4&paged=".get_query_var('paged'); $my_query = new WP_Query($my_query); ?>

    oh I wish I knew what I was doing :)

  8. jalacom
    Member
    Posted 2 years ago #

    Oh looks like this was the problem <?php$my_query needs a space <?php$ my_query Think its working. Thanks so much!

  9. esmi
    Forum Moderator
    Posted 2 years ago #

    Sorry - my bad:
    <?php $my_query = "showposts=10&cat=4&paged=".get_query_var('paged');

Topic Closed

This topic has been closed to new replies.

About this Topic