WordPress.org

Ready to get started?Download WordPress

Forums

Custom Query Paginate (Page Navigation) (9 posts)

  1. JusHerb
    Member
    Posted 1 year ago #

    Below I have code that I have pulled from several sources

    How can I tie my custom query into the page navigation? All I want to do is have a custom query that allows me to have 4 post per page. With a page nav at the bottom to get to each page containing the post.

    Here is a link to my page: http://gods-image.com/news-2

    <?php query_posts('cat=1&showposts=4'); ?>
    
      <div id="content">
    
    <?php if (have_posts()) : ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<p class="post-date">
    				<span class="date-day"><?php the_time('j') ?></span>
    				<span class="date-month"><?php the_time('M') ?></span>
    			</p>
    			<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    			<div class="entry">
    				<?php the_excerpt(); ?>
    
         </div>
           <p class="metadata">Posted by <?php the_author() ?>.<?php edit_post_link('Edit', ' | ', ''); ?></p>
    		</div>
    
      <?php endwhile; endif; ?>
    
              <?php
    global $wp_query;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
    	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format' => '?paged=%#%',
    	'current' => max( 1, get_query_var('paged') ),
    	'total' => $wp_query->max_num_pages
    ) );
    ?>
  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Is this the main query or a secondary query?.

  3. JusHerb
    Member
    Posted 1 year ago #

    @esmi somehow I need to tie both queries into one... I know it may sound kind of crazy. But I am new to php. It will more than likely end up being the second query which displays page Navigation. See the issue is I tweaked this custom query sometime back and here recently I needed to add navigation because of how the page is built. I need to be able to call 4 post per page. I need to some how pull from the first query to finish the second query. Basically I just need the navigation to work with the post category. Make sense?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    It will more than likely end up being the second query which displays page Navigation

    Then you shouldn't be using query_posts() but WP_Query(). However, it will always be the primary query & Loop that controls pagination. You cannot have separate pagination with a second custom query & Loop.

  5. JusHerb
    Member
    Posted 1 year ago #

    Right, Thats my problem I have no clue on how to use WP_Query() to produce what I need. 4 post per page coming from the category with navigation.... Didnt think it would ruff me up this bad... Any suggestions on how I can accomplish this? I am clueless at this point. Its there in the code but I have no clue on what to do to get started and finish it

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Do you intend to have 1 or 2 loops on the page?

  7. JusHerb
    Member
    Posted 1 year ago #

    At this point I have no real preference on what code is on the page. I'm just trying to accomplish my goal of having my post show as they are but just 4 to a page in that category of "News" but with Page Navigation (Prev 1 2 3 4 Next)... So to answer your question I dont care how i get there as long as i figure it out... What do you suggest?

  8. JusHerb
    Member
    Posted 1 year ago #

    Check out this page http://gods-image.com/news-2

    [No bumping. This is a volunteer-staffed forum - not online chat. If it's that urgent, consider hiring someone.]

  9. JusHerb
    Member
    Posted 1 year ago #

    WHAT!?

    Anyways....

    Here my solution to my problem if there is anybody searching for the same solution

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php query_posts('posts_per_page=3&cat=6&paged='.$paged); ?> 
    
      <div id="content">
    
    <?php if (have_posts()) : ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<p class="post-date">
    				<span class="date-day"><?php the_time('j') ?></span>
    				<span class="date-month"><?php the_time('M') ?></span>
    			</p>
    			<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    			<div class="entry">
    				<?php the_excerpt(); ?>
    
         </div>
           <p class="metadata">Posted by <?php the_author() ?>.<?php edit_post_link('Edit', ' | ', ''); ?></p>
    		</div>
    
      <?php endwhile; endif; ?>
    
    	<?php global $wp_query;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
    	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format' => '?paged=%#%',
    	'current' => max( 1, get_query_var('paged') ),
    	'total' => $wp_query->max_num_pages
    ) );
    ?>

    This will give you a list of post for an category on your site with working page Navigation

Topic Closed

This topic has been closed to new replies.

About this Topic