WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP_Query, Pagination, static & URL problems (6 posts)

  1. alexopalexo
    Member
    Posted 3 months ago #

    First of all, hello!

    So I spent all day trying to fix this and cannot... and it gets more confusing because I am not even sure what the issue is (after finding an undesirable solution).

    Problem:

    I am using a theme that I downloaded, and to easily get the desired home page I have to use their custom page template as a static home page. After some modifying and testing, I realized that there was no pagination on the static home page. The original also does not contain any pagination, so I did not mess any of their stuff up... I don't think!

    My problem is that I have successfully added pagination, but it fails to link properly. When I click on the various pagination links I tried, it just refreshes the page and shows the same posts. But, there is more! I can get to the other pages containing older posts if I manually type the url http://www.jovialjoystick.com/?paged=2), so I know the pages exist. I just cannot get my pagination to correctly link and when I hover over the pagination, my browser tells me it links to http://www.jovialjoystick.com/page/2/ which DOESN'T exist/work. So that makes me wonder if it is just some url output error that I hope is easily fixed? The undesirable solution is to switch WordPress' Permalink settings to default, and then the pagination links work, but I do not want that url/permalink structure.

    Here is my page's template code, which the theme developers made (I only removed 2 other loops and added the sidebar) :

    <?php
    $do_not_duplicate = array();
    get_header(); // Loads the header.php template. ?>
    
     <!-- Begin featured area. --->
     <?php
     $sticky = get_option( 'sticky_posts' );
     rsort( $sticky );
    
      $loop = new WP_Query(
      array( 'post__in' => array_slice( $sticky, 0, 5 ),
    	 'posts_per_page' => 5,
    	 'tax_query' => array(
    			array(
    	   		'taxonomy' => 'post_format',
    			'field' => 'slug',
    			'terms' => array(
    						'post-format-aside',
    						'post-format-audio',
    						'post-format-chat',
    						'post-format-link',
    						'post-format-quote',
    						'post-format-status',
    						'post-format-video'
    					),
    					'operator' => 'NOT IN'
    				)
    			)
    		)
    	); ?>
    
    	<?php if ( $loop->have_posts() ) : ?>
    
    		<div class="flexslider">
    
    			<div class="slides">
    
    				<?php while ( $loop->have_posts() ) : $loop->the_post(); $do_not_duplicate[] = get_the_ID(); ?>
    
    					<figure class="slide">
    
    						<?php get_the_image( array( 'meta_key' => true, 'size' => 'unique-slider' ) ); ?>
    
    						<figcaption class="slide-caption">
    
    							<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '">', '</a></h2>' ); ?>
    
    							<div class="entry-summary">
    								<?php the_excerpt(); ?>
    							</div><!-- .entry-summary -->
    
    						</figcaption><!-- .slide-caption -->
    
    					</figure><!-- .slide -->
    				<?php endwhile; ?>
    
    			</div><!-- .slides -->
    
    		</div><!-- .flexslider -->
    
    	<?php endif; ?>
    
    	<!-- End featured area. -->
    <div id="content" class="hfeed">
    
    <!-- Begin excerpts area. -->
    		<?php if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    $loop = new WP_Query(
    			array(
    				'post_type' => 'post',
    				'posts_per_page' => 3,
    				'paged' => $paged,
    				'tax_query' => array(
    					array(
    						'taxonomy' => 'post_format',
    						'field' => 'slug',
    						'terms' => array(
    							'post-format-aside',
    							'post-format-audio',
    							'post-format-chat',
    							'post-format-gallery',
    							'post-format-image',
    							'post-format-link',
    							'post-format-quote',
    							'post-format-status',
    							'post-format-video'
    						),
    						'operator' => 'NOT IN'
    					)
    				),
    				'post__not_in' => $do_not_duplicate
    			)
    		); ?>
    
    		<?php if ( $loop->have_posts() ) : ?>
    
    			<div class="content-secondary">
    
    				<?php while ( $loop->have_posts() ) : $loop->the_post(); $do_not_duplicate[] = get_the_ID();  ?>
    
    					<article id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
    
    							<?php if ( current_theme_supports( 'get-the-image' ) ) get_the_image( array( 'meta_key' => 'Thumbnail', 'size' => 'thumbnail' ) ); ?>
    
    							<header class="entry-header">
    								<?php echo apply_atomic_shortcode( 'entry_title', '[entry-title tag="h2"]' ); ?>
    								<?php echo apply_atomic_shortcode( 'entry_byline', '<div class="entry-byline">' . __( 'Published on [entry-published] [entry-edit-link before=" | "]', 'unique' ) . '</div>' ); ?>
    							</header><!-- .entry-header -->
    
    							<div class="entry-summary">
    								<?php the_excerpt(); ?>
    							</div><!-- .entry-summary -->
    
    					</article><!-- .hentry -->
    
    				<?php endwhile; ?>
    
    			</div><!-- .content-secondary -->
    <?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('page') ),
    	'total' => $loop->max_num_pages
    ) );
    ?>
    
    		<?php endif; ?>
    
    		<?php wp_reset_query(); ?>
    
    	</div><!-- #content -->
    
    <?php get_footer(); // Loads the footer.php template. ?>

    I specifically added:

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

    And this (which I think can solve my url structure in the format, but I am not sure how to do it.)

    <?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('page') ),
    	'total' => $loop->max_num_pages
    ) );
    ?>

    I researched for a long time and just could not get it to work because I am not sure if my urls, code and/or structure is the problem.

    Also, I am not using the custom page template how it came. The original contained 2 MORE loops to display gallery archives, so maybe there is a better way of doing what I want? Which is: the sticky post controlled slider with the latest blog posts under it.

    I did try to modify the index.php file, so I could just use the main loop... but I could not get the slider to show up properly, which I figured required a lot of theme modifying and dissecting, which I will attempt if I must.

  2. alexopalexo
    Member
    Posted 3 months ago #

    Also, I hope the page functions fine for people, it is just mocked up and not done yet... Thank you in advance!

  3. alexopalexo
    Member
    Posted 3 months ago #

    I resolved this! Not sure if it is the proper way... but I removed this line of code from the pagination at the bottom:

    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format' => '?paged=%#%',

    and now it properly paginates as far as I can tell... I will do some further testing.

  4. alexopalexo
    Member
    Posted 3 months ago #

    Update
    Hmm now my problem is that I can go to any page, but trying to get back to page 1 through clicking "1" or "Previous" does not work. Although, the "Previous" button works when going from "3" to "2."

  5. alexopalexo
    Member
    Posted 3 months ago #

    Update
    Hmm, I am still having problems. I think it is a simple fix now after doing proper research. As stated in the last update, my pagination shows up and works... but for some reason I cannot properly get back to page 1 (home/front page). This is a static page with the Permalink settings set to "Post Name" so they show up as: http://www.jovialjoystick.com/sample-post/ but for some reason my pagination urls are being returned as "/?paged=%#%" and working this way when moving to any other page that isn't page 1 (home/front page). When I hover over the pagination link to page 1 it shows me other page numbers depending on what page I am. So for example if I am on page 3, which works and I hover over the page 1 link, it displays "http://www.jovialjoystick.com/?paged=3" as the destination.

  6. alexopalexo
    Member
    Posted 3 months ago #

    Ok, so I never resolved the actual issue, but I got my home page functioning the way I want with out it being a static home page.

Reply

You must log in to post.

About this Topic