WordPress.org

Forums

[resolved] Latest post displays on 2nd page, pagination not working properly (11 posts)

  1. anionblu
    Member
    Posted 2 years ago #

    Hi

    I'm using a theme I bought. It uses a template that pulls in posts for a static home page. So the home page is not the blog.

    The post on my home page are limited to 8 per page, I have 10 posts for my home page. When I click on “Next” it displays the same posts as on the home page, the most recent posts and not the 2 older posts.

    Any help would be appreciated :-)

    This is my code:

    <?php
    /*
    Template Name: Template - Portfolio FullWidth
    */
    ?>
    <?php get_header(); // add header  ?>
    
    <div id="content">
    
              <ul class="filter">
                <li class="selected filterimg"><a href="#" rel="all" title="View all items" >All</a></li>
    
    				<?php
                      $catz =  get_terms('portfolio2', 'orderby=count&hide_empty=1');
                      foreach($catz as $cat1){
                      echo '<li><a href="#" rel="'.$cat1->slug.'" title="View all items under '.$cat1->name.'"><span>/</span>'.$cat1->name.'</a></li>';
                        }
                    ?>
              </ul><!-- end filter -->
              <div class="clear"></div>
    
    		         <?php
                      $nrprojects = of_get_option('iuliann_portfolio_number');
    
    					$args = array(
    								'post_type'         => 'portfolio',
    								'post_status'       => 'publish',
    								'posts_per_page'    => $nrprojects,
    								'ignore_sticky_posts'  => 1,
    								'paged'             => $paged,
    							   );
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		query_posts($args); while (have_posts()): the_post();
    
    		$catz = wp_get_object_terms($post->ID,'portfolio2');
                     foreach($catz as $cat1) { $itemcat = $cat1->slug;  break; }
    				 // Custom post type and get the category
                    ?>
    
         <ul class="item2">
            <li rel="<?php echo $itemcat; ?>"> 
    
          		<div class="post-portfolio">
           			<div class="entry-post">
    
    				 <?php if ( has_post_thumbnail()) { ?>
                      <a href="<?php the_permalink() ?>">
                      <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo get_image_path($post->ID); ?>&w=244&h=150"			                   alt="<?php the_title(); ?>" />
                      </a>
    
                      <?php } else { ?>
    
                    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                    <img src="<?php bloginfo('template_directory'); ?>/images/noimg-posts.png" width="244" height="150" alt="<?php the_title(); ?>" />
                    </a>
    
                    <?php } ?>
    
                     <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    				 <?php echo substr(the_title('', '', FALSE), 0, 40) . ' . .'; 	?></a></h2>
                     <p><?php truncate_post(70, true);?></p>
    
                     <div class="entry-meta">
                             <?php the_time('M d, Y'); ?> <span>·</span>
                             <?php comments_popup_link('No comments', '1 Comment', '% Comments'); ?>
                     </div><!-- end entry-meta -->
                     <div class="sright"><a href="<?php the_permalink() ?>" class="read_more"></a></div>
    
               </div><!-- end entry-post -->
             </div><!-- end #post -->
          </li>
        </ul><!-- end item -->
    
    <?php endwhile; ?>
    
    <div class="clear"></div>
    <div class="navigation">
    		<div class="previous" ><?php previous_posts_link(''); ?></div>
    		<div class="next"><?php next_posts_link(''); ?></div>
    </div><!-- end navigation -->       
    
    <div id="social-nav">
      <ul>
    	<?php if(of_get_option('iuliann_rss')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_rss'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/rss.png" width="16" height="16" alt="rss" /></a></li>
        <?php } else { ?>
    	<li><a href="<?php bloginfo('rss2_url'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/rss.png" width="16" height="16" alt="rss" /></a></li>
        <?php } // get rss icon ?>
    
        <?php if(of_get_option('iuliann_facebook')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_facebook'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/facebook.png" width="16" height="16" alt="facebook" /></a></li>
        <?php } // get facebook icon ?>
    
        <?php if(of_get_option('iuliann_twitter')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_twitter'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/twitter.png" width="16" height="16" alt="twitter" /></a></li>
        <?php } // get twitter icon ?>
    
        <?php if(of_get_option('iuliann_dribbble')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_dribbble'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/dribbble.png" width="16" height="16" alt="dribbble" /></a></li>
        <?php } // get dribbble icon ?>  
    
        <?php if(of_get_option('iuliann_flickr')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_flickr'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/flickr.png" width="16" height="16" alt="flickr" /></a></li>
        <?php } // get flickr icon ?>
    
        <?php if(of_get_option('iuliann_forrst')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_forrst'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/forrst.png" width="16" height="16" alt="forrst" /></a></li>
        <?php } // get forrst icon ?>    
    
        <?php if(of_get_option('iuliann_deviantart')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_deviantart'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/deviantart.png" width="16" height="16" alt="deviant" /></a></li>
        <?php } // get deviantart icon ?>    
    
        <?php if(of_get_option('iuliann_youtube')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_youtube'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/youtube.png" width="16" height="16" alt="youtube" /></a></li>
        <?php } // get youtube icon ?>    
    
        <?php if(of_get_option('iuliann_reddit')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_reddit'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/reddit.png" width="16" height="16" alt="reddit" /></a></li>
        <?php } // get reddit icon ?>    
    
        <?php if(of_get_option('iuliann_digg')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_digg'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/digg.png" width="16" height="16" alt="digg" /></a></li>
        <?php } // get digg icon ?> 
    
        <?php if(of_get_option('iuliann_behance')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_behance'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/behance.png" width="16" height="16" alt="behance" /></a></li>
        <?php } // get behance icon ?> 
    
        <?php if(of_get_option('iuliann_buzz')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_buzz'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/buzz.png" width="16" height="16" alt="buzz" /></a></li>
        <?php } // get buzz icon ?> 
    
        <?php if(of_get_option('iuliann_vimeo')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_vimeo'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/vimeo.png" width="16" height="16" alt="vimeo" /></a></li>
        <?php } // get vimeo icon ?> 
    
        <?php if(of_get_option('iuliann_linkedin')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_linkedin'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/linkedin.png" width="16" height="16" alt="linkedin" /></a></li>
        <?php } // get linkedin icon ?> 
    
        <?php if(of_get_option('iuliann_myspace')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_myspace'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/myspace.png" width="16" height="16" alt="myspace" /></a></li>
        <?php } // get myspace icon ?>     
    
        <?php if(of_get_option('iuliann_skype')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_skype'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/skype.png" width="16" height="16" alt="skype" /></a></li>
        <?php } // get skype icon ?>   
    
        <?php if(of_get_option('iuliann_stumbleupon')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_stumbleupon'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/stumbleupon.png" width="16" height="16" alt="stumbleupon" /></a></li>
        <?php } // get stumbleupon icon ?>   
    
        <?php if(of_get_option('iuliann_twitpic')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_twitpic'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/twitpic.png" width="16" height="16" alt="twitpic" /></a></li>
        <?php } // get twitpic icon ?>   
    
        <?php if(of_get_option('iuliann_xing')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_xing'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/xing.png" width="16" height="16" alt="xing" /></a></li>
        <?php } // get xing icon ?>   
    
        <?php if(of_get_option('iuliann_lastfm')!=""){ ?>
    	<li><a href="<?php echo of_get_option('iuliann_lastfm'); ?>" target="_blank">
        <img src="<?php bloginfo('template_directory'); ?>/images/social/lastfm.png" width="16" height="16" alt="lastfm" /></a></li>
        <?php } // get lastfm icon ?>
      </ul>
    </div><!-- end social -->     
    
    <?php wp_reset_query(); // reset query for navigation ?>
    
    <div class="clear"></div>
    </div><!-- end content -->
    <?php get_footer(); // add footer  ?>
  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try changing this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    to this:

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }

    http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

  3. anionblu
    Member
    Posted 2 years ago #

    Hi, thanks for the reply. It did not work.

    This template seems to work if it is applied to another page, other than the home page. The pagination works correctly if I apply it to say an events page, but when I target it to the home page it displays the recent posts on page two as well. The home page is set to a static page. There is a different template for the blog. Hope this makes sence :-)

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    Oh, also put it above the $args array like this:

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    
    $args = array(
    								'post_type'         => 'portfolio',
    								'post_status'       => 'publish',
    								'posts_per_page'    => $nrprojects,
    								'ignore_sticky_posts'  => 1,
    								'paged'             => $paged,
    							   );
    
    		query_posts($args); while (have_posts()): the_post();
  5. anionblu
    Member
    Posted 2 years ago #

    Awesome!! That worked perfectly! Thanks man :-)

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. I'm glad you got it resolved :-)

  7. Ahmed Taha
    Member
    Posted 2 years ago #

    hi keesiemeijer =)

    I wish you could help me with this problem:

    this is my blog: http://kbeacon.com

    when I try to go to kbeacon.com/page/2, a blank page appears instead of the second 10 posts.

    here is my code :

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser. Or use pastebin.com instead. ]

    <?php
    
    /**
     * Template tag: Boxed Style Paging
     *
     * @param array $args:
     *	'before': (string)
     *	'after': (string)
     *	'options': (string|array) Used to overwrite options set in WP-Admin -> Settings -> PageNavi
     *	'query': (object) A WP_Query instance
     */
    function wp_pagenavi( $args = array() ) {
    	if ( !is_array( $args ) ) {
    		$argv = func_get_args();
    
    		$args = array();
    		foreach ( array( 'before', 'after', 'options' ) as $i => $key )
    			$args[ $key ] = $argv[ $i ];
    	}
    
    	$args = wp_parse_args( $args, array(
    		'before' => '',
    		'after' => '',
    		'options' => array(),
    		'query' => $GLOBALS['wp_query'],
    		'type' => 'posts',
    		'echo' => true
    	) );
    
    	extract( $args, EXTR_SKIP );
    
    	$options = wp_parse_args( $options, PageNavi_Core::$options->get() );
    
    	$instance = new PageNavi_Call( $args );
    
    	list( $posts_per_page, $paged, $total_pages ) = $instance->get_pagination_args();
    
    	if ( 1 == $total_pages && !$options['always_show'] )
    		return;
    
    	$pages_to_show = absint( $options['num_pages'] );
    	$larger_page_to_show = absint( $options['num_larger_page_numbers'] );
    	$larger_page_multiple = absint( $options['larger_page_numbers_multiple'] );
    	$pages_to_show_minus_1 = $pages_to_show - 1;
    	$half_page_start = floor( $pages_to_show_minus_1/2 );
    	$half_page_end = ceil( $pages_to_show_minus_1/2 );
    	$start_page = $paged - $half_page_start;
    
    	if ( $start_page <= 0 )
    		$start_page = 1;
    
    	$end_page = $paged + $half_page_end;
    
    	if ( ( $end_page - $start_page ) != $pages_to_show_minus_1 )
    		$end_page = $start_page + $pages_to_show_minus_1;
    
    	if ( $end_page > $total_pages ) {
    		$start_page = $total_pages - $pages_to_show_minus_1;
    		$end_page = $total_pages;
    	}
    
    	if ( $start_page < 1 )
    		$start_page = 1;
    
    	$out = '';
    	switch ( intval( $options['style'] ) ) {
    		// Normal
    		case 1:
    			// Text
    			if ( !empty( $options['pages_text'] ) ) {
    				$pages_text = str_replace(
    					array( "%CURRENT_PAGE%", "%TOTAL_PAGES%" ),
    					array( number_format_i18n( $paged ), number_format_i18n( $total_pages ) ),
    				$options['pages_text'] );
    				$out .= "<span class='pages'>$pages_text</span>";
    			}
    
    			if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
    				// First
    				$first_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $total_pages ), $options['first_text'] );
    				$out .= $instance->get_single( 1, 'first', $first_text, '%TOTAL_PAGES%' );
    			}
    
    			// Previous
    			if ( $paged > 1 && !empty( $options['prev_text'] ) )
    				$out .= $instance->get_single( $paged - 1, 'previouspostslink', $options['prev_text'] );
    
    			if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
    				if ( !empty( $options['dotleft_text'] ) )
    					$out .= "<span class='extend'>{$options['dotleft_text']}</span>";
    			}
    
    			// Smaller pages
    			$larger_pages_array = array();
    			if ( $larger_page_multiple )
    				for ( $i = $larger_page_multiple; $i <= $total_pages; $i+= $larger_page_multiple )
    					$larger_pages_array[] = $i;
    
    			$larger_page_start = 0;
    			foreach ( $larger_pages_array as $larger_page ) {
    				if ( $larger_page < ($start_page - $half_page_start) && $larger_page_start < $larger_page_to_show ) {
    					$out .= $instance->get_single( $larger_page, 'smaller page', $options['page_text'] );
    					$larger_page_start++;
    				}
    			}
    
    			if ( $larger_page_start )
    				$out .= "<span class='extend'>{$options['dotleft_text']}</span>";
    
    			// Page numbers
    			$timeline = 'smaller';
    			foreach ( range( $start_page, $end_page ) as $i ) {
    				if ( $i == $paged && !empty( $options['current_text'] ) ) {
    					$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
    					$out .= "<span class='current'>$current_page_text</span>";
    					$timeline = 'larger';
    				} else {
    					$out .= $instance->get_single( $i, "page $timeline", $options['page_text'] );
    				}
    			}
    
    			// Large pages
    			$larger_page_end = 0;
    			$larger_page_out = '';
    			foreach ( $larger_pages_array as $larger_page ) {
    				if ( $larger_page > ($end_page + $half_page_end) && $larger_page_end < $larger_page_to_show ) {
    					$larger_page_out .= $instance->get_single( $larger_page, 'larger page', $options['page_text'] );
    					$larger_page_end++;
    				}
    			}
    
    			if ( $larger_page_out ) {
    				$out .= "<span class='extend'>{$options['dotright_text']}</span>";
    			}
    			$out .= $larger_page_out;
    
    			if ( $end_page < $total_pages ) {
    				if ( !empty( $options['dotright_text'] ) )
    					$out .= "<span class='extend'>{$options['dotright_text']}</span>";
    			}
    
    			// Next
    			if ( $paged < $total_pages && !empty( $options['next_text'] ) )
    				$out .= $instance->get_single( $paged + 1, 'nextpostslink', $options['next_text'] );
    
    			if ( $end_page < $total_pages ) {
    				// Last
    				$out .= $instance->get_single( $total_pages, 'last', $options['last_text'], '%TOTAL_PAGES%' );
    			}
    			break;
    
    		// Dropdown
    		case 2:
    			$out .= '<form action="" method="get">'."\n";
    			$out .= '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">'."\n";
    
    			foreach ( range( 1, $total_pages ) as $i ) {
    				$page_num = $i;
    				if ( $page_num == 1 )
    					$page_num = 0;
    
    				if ( $i == $paged ) {
    					$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
    					$out .= '<option value="'.esc_url( $instance->get_url( $page_num ) ).'" selected="selected" class="current">'.$current_page_text."</option>\n";
    				} else {
    					$page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
    					$out .= '<option value="'.esc_url( $instance->get_url( $page_num ) ).'">'.$page_text."</option>\n";
    				}
    			}
    
    			$out .= "</select>\n";
    			$out .= "</form>\n";
    			break;
    	}
    	$out = $before . "<div class='wp-pagenavi'>\n$out\n</div>" . $after;
    
    	$out = apply_filters( 'wp_pagenavi', $out );
    
    	if ( !$echo )
    		return $out;
    
    	echo $out;
    }
    
    class PageNavi_Call {
    
    	protected $args;
    
    	function __construct( $args ) {
    		$this->args = $args;
    	}
    
    	function __get( $key ) {
    		return $this->args[ $key ];
    	}
    
    	function get_pagination_args() {
    		global $numpages;
    
    		$query = $this->query;
    
    		switch( $this->type ) {
    		case 'multipart':
    			// Multipart page
    			$posts_per_page = 1;
    			$paged = max( 1, absint( get_query_var( 'page' ) ) );
    			$total_pages = max( 1, $numpages );
    			break;
    		case 'users':
    			// WP_User_Query
    			$posts_per_page = $query->query_vars['number'];
    			$paged = max( 1, floor( $query->query_vars['offset'] / $posts_per_page ) + 1 );
    			$total_pages = max( 1, ceil( $query->total_users / $posts_per_page ) );
    			break;
    		default:
    			// WP_Query
    			$posts_per_page = intval( $query->get( 'posts_per_page' ) );
    			$paged = max( 1, absint( $query->get( 'paged' ) ) );
    			$total_pages = max( 1, absint( $query->max_num_pages ) );
    			break;
    		}
    
    		return array( $posts_per_page, $paged, $total_pages );
    	}
    
    	function get_single( $page, $class, $raw_text, $format = '%PAGE_NUMBER%' ) {
    		if ( empty( $raw_text ) )
    			return '';
    
    		$text = str_replace( $format, number_format_i18n( $page ), $raw_text );
    
    		return "<a>get_url( $page ) ) . "' class='$class'>$text</a>";
    	}
    
    	function get_url( $page ) {
    		return ( 'multipart' == $this->type ) ? get_multipage_link( $page ) : get_pagenum_link( $page );
    	}
    }
    
    # http://core.trac.wordpress.org/ticket/16973
    if ( !function_exists( 'get_multipage_link' ) ) :
    function get_multipage_link( $page = 1 ) {
    	global $post, $wp_rewrite;
    
    	if ( 1 == $page ) {
    		$url = get_permalink();
    	} else {
    		if ( '' == get_option('permalink_structure') || in_array( $post->post_status, array( 'draft', 'pending') ) )
    			$url = add_query_arg( 'page', $page, get_permalink() );
    		elseif ( 'page' == get_option( 'show_on_front' ) && get_option('page_on_front') == $post->ID )
    			$url = trailingslashit( get_permalink() ) . user_trailingslashit( $wp_rewrite->pagination_base . "/$page", 'single_paged' );
    		else
    			$url = trailingslashit( get_permalink() ) . user_trailingslashit( $page, 'single_paged' );
    	}
    
    	return $url;
    }
    endif;
    
    // Template tag: Drop Down Menu (Deprecated)
    function wp_pagenavi_dropdown() {
    	wp_pagenavi();
    }
    
    class PageNavi_Core {
    	static $options;
    
    	static function init( $options ) {
    		self::$options = $options;
    
    		add_action( 'wp_print_styles', array( __CLASS__, 'stylesheets' ) );
    	}
    
    	static function stylesheets() {
    		if ( !self::$options->use_pagenavi_css )
    			return;
    
    		if ( @file_exists( get_stylesheet_directory() . '/pagenavi-css.css' ) )
    			$css_file = get_stylesheet_directory_uri() . '/pagenavi-css.css';
    		elseif ( @file_exists( get_template_directory() . '/pagenavi-css.css' ) )
    			$css_file = get_template_directory_uri() . '/pagenavi-css.css';
    		else
    			$css_file = plugins_url( 'pagenavi-css.css', __FILE__ );
    
    		wp_enqueue_style( 'wp-pagenavi', $css_file, false, '2.70' );
    	}
    }

    Thanks in advance

  8. @tahavaro, Please start your own topic.

  9. Ahmed Taha
    Member
    Posted 2 years ago #

    ok

  10. Ahmed Taha
    Member
    Posted 2 years ago #

    how to start a new thread????

  11. It can be hard to locate sometimes. ;)

    Use this link.

    http://wordpress.org/support/forum/how-to-and-troubleshooting#postform

Topic Closed

This topic has been closed to new replies.

About this Topic