WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Paginate with wp pagenavi on a Page (10 posts)

  1. necroob
    Member
    Posted 1 year ago #

    Hi! I’m using a page (not a post) to show the childs of that page in list and i wanna use wp_pagenavi to break the page because it has a lot of subpages. Let’s say i have 50 child and i wanna show only 25 and then to be a page2 where you can see the next 25 pages. I’m using this code to get the child pages, but i don’t know how to get the wp_pagenavi working with it. So.. can you help me in any way?

    <?php 
    
    /**
     * Template Name: pagina categorie
     */
    
     ?>
    <?php get_header(); ?>
    	<div class="content">
    		<?php tie_breadcrumbs() ?>
    
    		<?php if ( ! have_posts() ) : ?>
    			<div id="post-0" class="post not-found post-listing">
    				<h1 class="post-title"><?php _e( 'Not Found', 'tie' ); ?></h1>
    				<div class="entry">
    					<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'tie' ); ?></p>
    					<?php get_search_form(); ?>
    				</div>
    			</div>
    		<?php endif; ?>
    
    		<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    		<?php $get_meta = get_post_custom($post->ID);  ?>
    		<?php //Above Post Banner
    		if( empty( $get_meta["tie_hide_above"][0] ) ){
    			if( !empty( $get_meta["tie_banner_above"][0] ) ) echo '<div class="ads-post">' .htmlspecialchars_decode($get_meta["tie_banner_above"][0]) .'</div>';
    			else tie_banner('banner_above' , '<div class="ads-post">' , '</div>' );
    		}
    		?>
    
    		<article class="post-listing post">
    			<?php tie_include( 'post-head' ); // Get Post Head template ?>
    			<div class="post-inner">
    				<h1 class="post-title"><?php the_title(); ?></h1>
    				<p class="post-meta"></p>
    				<div class="clear"></div>
    				<div class="entry">
    					<?php the_content(); ?>
    
    <!--Incepe cod pentru afiliere-->
    <center>
    <ul class="product_list">
    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_title');
    	$count = 0;
    	foreach($pages as $page)
    	{?>
    
    <li>
            <div class="product_layout">
      <div class="thumb1"><a href="<?php echo get_page_link($page->ID) ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></a>
      <a href="<?php echo get_page_link($page->ID) ?>" title="<?php echo $page->post_title ?>"><?php $thetitle=$page->post_title; $getlength = strlen($thetitle);
    $thelength = 18;
    echo substr($thetitle, 0, $thelength);
    if ($getlength > $thelength) echo "..."; ?></a></div>
        <div class="bottom"></div> 
    
    </li>
    
    	<?php }
    
    ?>
     </ul></center>
     <div id="clear"></div>
    
    <!--Se termina cod afiliere-->
    		<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'tie' ), 'after' => '</div>' ) ); ?>
    
    					<?php edit_post_link( __( 'Edit', 'tie' ), '<span class="edit-link">', '</span>' ); ?>
    				</div><!-- .entry /-->	
    
    			</div><!-- .post-inner -->
    		</article><!-- .post-listing -->
    		<?php endwhile; ?>
    
    		<?php //Below Post Banner
    		if( empty( $get_meta["tie_hide_below"][0] ) ){
    			if( !empty( $get_meta["tie_banner_below"][0] ) ) echo '<div class="ads-post">' .htmlspecialchars_decode($get_meta["tie_banner_below"][0]) .'</div>';
    			else tie_banner('banner_below' , '<div class="ads-post">' , '</div>' );
    		}
    		?>
    
    		<?php comments_template( '', true ); ?>
    	</div><!-- .content -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    a 'foreach' loop with 'get_pages()' is virtually impossible to paginate;

    - try to rewrite the code to use 'WP_Query()'

    http://codex.wordpress.org/Class_Reference/WP_Query

  3. necroob
    Member
    Posted 1 year ago #

    can you please help me do that? or someone else? I really need it done and I don't know how.

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Try reviewing the page at the link posted above. If you still cannot manage it yourself, you may want to consider hiring someone to create the new loop/query for you.

  5. alchymyth
    Forum Moderator
    Posted 1 year ago #

    turned out to be more complicated than I thought (and there might be something simpler);

    replace this section:

    <center>
    <ul class="product_list">
    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_title');
    	$count = 0;
    	foreach($pages as $page)
    	{?>
    
    <li>
            <div class="product_layout">
      <div class="thumb1"><a href="<?php echo get_page_link($page->ID) ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></a>
      <a href="<?php echo get_page_link($page->ID) ?>" title="<?php echo $page->post_title ?>"><?php $thetitle=$page->post_title; $getlength = strlen($thetitle);
    $thelength = 18;
    echo substr($thetitle, 0, $thelength);
    if ($getlength > $thelength) echo "..."; ?></a></div>
        <div class="bottom"></div> 
    
    </li>
    
    	<?php }
    
    ?>
     </ul></center>

    with:

    <center>
    <?php
    	$page_ids = array();
    	$subpages = get_pages('child_of='.$post->ID.'&sort_column=post_title');
    	if($subpages)foreach($subpages as $subpage) { $page_ids[] = $subpage->ID; } 
    
    	$subpages = new WP_Query( array(
    		'post_type' => 'page',
    		'post__in' => $page_ids,
    		'orderby' => 'title',
    		'paged' => get_query_var('paged')
    		));
    	$count = 0;
    	if($page_ids && $subpages->have_posts()) : ?>
    <ul class="product_list">
    <?php while($subpages->have_posts()) : $subpages->the_post(); ?>
    
    <li>
            <div class="product_layout">
      <div class="thumb1"><a href="<?php echo get_page_link($post->ID) ?>"><?php echo get_the_post_thumbnail($post->ID, 'thumbnail'); ?></a>
      <a href="<?php echo get_page_link($post->ID) ?>" title="<?php echo $post->post_title ?>"><?php $thetitle=$post->post_title; $getlength = strlen($thetitle);
    $thelength = 18;
    echo substr($thetitle, 0, $thelength);
    if ($getlength > $thelength) echo "..."; ?></a></div>
        <div class="bottom"></div> 
    
    </li>
    
    	<?php endwhile; ?>
     </ul>
    <?php previous_posts_link('&laquo; newer posts',$subpages->max_num_pages);
    next_posts_link('older posts &raquo;',$subpages->max_num_pages); ?>
    
    <?php endif; wp_reset_postdata(); ?>
    </center>

    untested

  6. necroob
    Member
    Posted 1 year ago #

    This works perfectly! You are a life saver. I was going crazy this past days and I'm not that good at this.
    I don't wanna be a jerk (I'm just asking), can it use WP_Pagenavi instead of "older" and "newer"?

  7. alchymyth
    Forum Moderator
    Posted 1 year ago #

    you can use wp-pagenavi, but read the instructions:
    http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html

    i.e. replace:

    <?php previous_posts_link('&laquo; newer posts',$subpages->max_num_pages);
    next_posts_link('older posts &raquo;',$subpages->max_num_pages); ?>

    with:

    <?php wp_pagenavi( array( 'query' => $subpages ) ); ?>
  8. necroob
    Member
    Posted 1 year ago #

    Thank you alchymyth! I hope to learn more in the future and stop bothering people :)
    Greate fix up for my page.

  9. necroob
    Member
    Posted 1 year ago #

    And if I have
    Page 1 with SubPage 1 whom also havem a SubSubPage 1, SubSubPage2, 3, 4, etc. and at the bottom of the tree I have SubSubSubPage 1,2,3
    And I want to display the SubSubSubPages instead of all the Subpages?
    I tried adding depth=3 like that:

    <?php
    	$page_ids = array();
    	$subpages = get_pages('depth=4&child_of='.$post->ID.'&sort_column=post_title');
    	if($subpages)foreach($subpages as $subpage) { $page_ids[] = $subpage->ID; } 
    
    	$subpages = new WP_Query( array(
    		'post_type' => 'page',
    		'post__in' => $page_ids,
    		'orderby' => 'title',
    		'paged' => get_query_var('paged')
    		));
    	$count = 0;
    	if($page_ids && $subpages->have_posts()) : ?>

    But it doesn't do anything.

  10. alchymyth
    Forum Moderator
    Posted 1 year ago #

    please start a new topic -

    this problem is new and different from the currently resolved pagination problem.

Topic Closed

This topic has been closed to new replies.

About this Topic