Multiple Wordpress loops, hide first loop on second page - php

I've construced two Wordpress loops with numeric pagination as follows:
<div class="container" style="background:#ccc">
<?
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => false,
'category_name' => 'latest',
'posts_per_page' => 1,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
?>
<!-- second -->
<?
$args2 = array(
'paged' => $paged2,
'category_name' => 'uncategorized',
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
<!-- container -->
Everything works fine, but the paginatin page also shows the 'latest' post loop, although I've set the pagination to false.
How could I hide the first loop on the pagination pages?
Advice appreciated.

use wp_reset_postdata() function after your loop end
follow this code, may be it'll help you
<?php
// example args
$args = array( 'posts_per_page' => 3 );
// the query
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?><!-- end of the loop -->
<!-- put pagination functions here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Related

WordPress Pagination Failing To Display

I'm working on adding pagination to a page and I am following along on the official codex but I am not getting results. Right now I want to populate pagination after 1 post per page (for testing purposes). No pagination shows, the h3 tags return empty. I have tried multiple queries other than the one below to no avail. Help is sorely needed and appreciated.
<?php
$paged = (get_query_var ('paged')) ? get_query_var ('paged') : 1;
$args = array(
'post_type' => array('post'),
'posts_per_page' => 1,
'paged' => $paged,
'cat' => 5,
'tag__not_in' => 22
);
$the_query = new WP_Query ( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- THE CONTENT -->
<?php endwhile; ?>
<h3><?php next_posts_link('Next'); ?></h3>
<h3><?php previous_posts_link('Previous');?></h3>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

WP Query paging not generating

I have a seemingly simple WP query looking for posts on my site, and want these posts to be 'paged' accordingly. Below is my code, trying to achieve this, however all I am currently seeing is an empty .pager nav.
What is incorrect here? Help much appreciated.
Here is the live example.
PHP
<?php get_header(); ?>
<?php get_sidebar(); ?>
<main>
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'journal',
'posts_per_page' => 2,
'paged' => $paged,
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date'
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article>
/* Article content here */
</article>
<?php endwhile; ?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<nav class="pager">
<?php
$args = array(
'prev_next' => true,
'show_all' => true,
'type' => 'list'
);
echo paginate_links( $args );
?>
<ul class="page-prev-next">
<?php if( get_previous_posts_link() ) :
echo '<li class="prev-page">' . get_previous_posts_link( 'Prev' ) . '</li>';
endif; ?>
<?php if( get_next_posts_link() ) :
echo '<li class="next-page">' . get_next_posts_link( 'Next' ) . '</li>';
endif; ?>
</ul>
</nav>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>

Why wp_query loop still considers hidden categories and 'post__not_in' in pagination?

I have three loops on same page.
The first loop displays the most recent post of category "highlight".
The second loop displays others posts of this same category in chronological order.
The third loop displays all the posts, except the posts in others loops.
All works fine, but in pagination (pagenavi), the max_num_pages considers all the posts, ignoring criterias like 'post__not_in' or 'cat'.
If I use this loop, the last page of pagenavi stills in blank (counts hidden posts yet, but don't shows them):
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
And If I use this loop (loop 3), the last page of pagenavi shows the "hidden" posts:
if (have_posts()) : while (have_posts()) : the_post();
How to force the wp_query loop to exclude the hidden posts of the max_num_pages count?
//loop 1
<?php
$loop1 = new WP_query(array(
'category_name' => 'highlight',
'posts_per_page' => 1,
));
if($loop1->have_posts()) : $firstPosts = array(); while($loop1->have_posts()) : $loop1->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 2
<?php
$loop2 = new WP_query(array(
'post__not_in' => $firstPosts,
'category_name' => 'highlight',
'posts_per_page' => 2,
));
if($loop2->have_posts()) : while($loop2->have_posts()) : $loop2->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 3
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
//other stuff
//pagenavi
<?php wp_pagenavi("", "", array(
'query' => $loop3,
'first_text' => 'lorem ipsum',
'last_text' => 'lorem ipsum',
)); ?>
If you're using WP-PageNavi, there is a special way to use it with custom queries. For example:
$my_query = new WP_Query(
array(
'tag' => 'foo',
'paged' => get_query_var('paged')
)
);
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
So make sure you are passing your custom query object into the wp_pagenavi() function call.
~Edit~
Here's the link to the documentation: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
~Edit 2~
Try this code that has been customized for your particular application:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
...
wp_pagenavi( array( 'query' => $loop3 ) );
wp_reset_postdata();

Get taxonomy from one loop and use it in another

This code in the first loop:
<?php
$terms_as_text = get_the_term_list( $post->ID, 'produktkategori', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?>
Results in "Spanskt". I want "Spanskt" to be entered in the second loop where it says "Spanskt". Is that possible?
Thanks in advance.
// First loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php
$terms_as_text = get_the_term_list( $post->ID, 'produktkategori', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> // The value of this...</h1>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
Some content
</article>
<?php endwhile; endif; ?>
// Second loop
<?php $loop = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 4, 'produktkategori' => 'Spanskt // ...should be entered here', 'orderby' => 'rand', ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Some content
<?php endwhile; ?>
This is my solution:
// First loop
<?php $loop = new WP_Query( array( 'post_type' => 'forfattare', 'posts_per_page' => 10, 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $string = get_the_title() ;
$forfattare = createAlias( $string );
?>
// Second loop
<?php $loop_2 = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 10, 'forfattare' => ''.$forfattare.'', 'orderby' => 'rand', 'order' => 'ASC', ) ); ?>
<?php while ( $loop_2->have_posts() ) : $loop_2->the_post(); ?>
<div class="italic"><?php the_title(); ?></div>
<?php endwhile; ?>
<?php endwhile; ?>

Wordpress, if no results from loop, don't show header

I am using WP_Query to loop through a custom post type in wordpress. My loop looks like this:
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
As you can see, before the loop there is a header that says "Available Now!". I want to reformat the loop so if there are no posts returned, then the div containing the title (div class bigRedStrip) will not be displayed. I have tried a number of potential solutions, but the problem I keep running into, is that all of these "solutions" require putting the <div class="bigRedStrip"> inside the loop, which results in the header repeating for every returned post. The idea is to have the header only displayed once. Any ideas how I can accomplish this?
You only need to pull the things a bit apart. First of all run the query:
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
Then check if there is something:
<?php if ($loop->have_posts()) { ?>
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
...
And if so, just iterate over the posts:
...
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
<?php } ?>
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
<?php if ($loop->have_posts()){
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
<?php } ?>

Categories