I have already searched several topics and questions regarding this problem, but I havenĀ“t found any answer that suits my code.
The pagination for my custom posts is displayed, but when I click in Show next posts >> the same posts from the previous page are shown, even though the URL shows ?paged=2.
My code is the following:
<div class="podcast-entries">
<?php
$args = array( 'post_type' => 'strn5_podcasts',
'posts_per_page' => 3,
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1 );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
?>
<div <?php post_class(); ?> >
<h4 class="podcast-title"><?php the_title(); ?></h4>
<div class="podcast-content">
<?php the_content(); ?>
</div>
<h6><?php the_time('F j, Y'); ?></h6>
<div class="post-separator col-lg-12"></div>
</div>
<?php endwhile; endif; ?>
<div class="navigation-links">
<div class="next-post">
<?php next_posts_link( '>> Siguiente Entrada' ); ?>
</div>
<div class="previous-post">
<?php previous_posts_link( 'Anterior Entrada >>' ); ?>
</div>
</div>
<?php $wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
</div> <!-- .podcast-entries -->
Usually what I do when something doesn't work is that I strip it from everything and start rebuilding it with bare basics to see what the problem might be.
Why don't you try a simpler form of loop to see if you can get it to prin.t
<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'strn5_podcasts', 'paged' => get_query_var('page'))); if (have_posts()) : while (have_posts()) : the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; endif;?>
<div class="navigation-links">
<div class="next-post"><?php next_posts_link( '>> Siguiente Entrada' ); ?></div>
<div class="previous-post"><?php previous_posts_link( 'Anterior Entrada >>' ); ?></div>
</div>
It's pretty much the same thing you are doing (almost) but without any variables or any other thing that "could" go wrong.
Edit
In the end it turned out that the paged parameter can be used with paged or page and in this case it worked out for you with page instead of paged
So it ended up looking like so:
'paged' => get_query_var('page')
On a final note, using get_query_var('page') is needed if you're using query posts in a custom page template that you've set as your homepage.
I agree with sulfureous. I think more specifically though, these three lines are the problem:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
Based off of your description it seems like this would cause the issue if $wp_query is being caught as not null. I'm not sure why you'd even have that in there but you must have your reason.
At the very least take it out the first two lines of that to help you troubleshoot.
Related
I'm trying to output all posts under custom post type "philosophy" on a page in a list. Alternating between categories "img-left" and "img-right".
I can get the posts to display ALL "philosophy" posts however i want to lay out the posts in two layouts depending on their custom category.
If the category is "img-right" i want the post to be shown with the text on the left and image on the right and vice-versa for "img-left".
I have tried the below code which doesn't work at all.
<?php
$args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
if( in_category( 'img-right' ) ):
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="col-md-12"><h2>';
the_title();
echo '</h2></div><div class="row content"><div class="col-md-6"';
the_content();
echo '</div><div class="col-md-5 offset-1 float-right">';
the_post_thumbnail('array(100,100)');
echo '</div></div>';
endwhile;
endif;
?>
by removing the "if" and "endif" i have the code that lists all the posts in one layout. What I need is conditionals that can output both "img-right" and "img-left" layouts based on the post's category. The only layout shown in my example above is "img-right".
Any help would be greatly appreciated. This PHP is making my head spin!
So...with all the help from the guys answering i figured it out using the CSS approach touched on by #Mohsin.
Here is my code:
<div id="content" class="col-12" role="main">
<?php get_template_part('loops/page-content'); ?>
</div>
<div class="row">
<?php
$args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row content"><div class="col-md-12"><h2>';
the_title();
echo '</div><div class="col-md-6">';
the_content();
echo '</div><div class="col-md-6">';
the_post_thumbnail();
echo '</div></div>';
endwhile;
?>
I then applied this:
.row.content:nth-child(even) {
flex-direction: row-reverse;
}
and we're golden.
Thank you to everyone who helped.
If I understand correctly:
<?php $args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 ); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-12"><h2>
<?php the_title(); ?>
</h2></div>
<div class="row content">
<?php if( in_category( 'img-left' ) ): ?>
<div class="col-md-5 offset-1 float-left">
<?php the_post_thumbnail('array(100,100)'); ?>
</div>
<?php endif; ?>
<div class="col-md-6">
<?php the_content(); ?>
</div>
<?php if( in_category( 'img-right' ) ): ?>
<div class="col-md-5 offset-1 float-right">
<?php the_post_thumbnail('array(100,100)'); ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
The above code will output all of the posts, with a thumbnail to the left if it is category img-left and thumbnail to the right if in img-right (and outputting both if it is categorized as both, and neither if it is neither category. You may want different behavior, but it should be simple to adjust the conditionals).
If your template becomes anymore complicated, I would recommend moving sections out to template-parts/using functions like get_sidebar().
I am trying to get Pagination working on my static homepage which I have integrated with wordpress. The problem I am having is when I click the "Older Entries" button on the page it goes to the ?paged=2 page but displays the first 10 posts still. Just like on the first page.
I know the code somewhat works because I changed ($the_query->max_num_pages > 1) to 2 and went to the page and it displayed the contents of the second page with a "Newer Entries" button.
I just cannot get it to do it automatically.
I used this persons tutorial on how to set it up
http://callmenick.com/post/custom-wordpress-loop-with-pagination
and here is my code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h1><?php echo the_title(); ?></h1>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
The website (temporary) can be seen here http://auroraservers.org/MainSite/Index.php
So you can see what it is doing.
Thank you for any help! I've been trying to fix this for over a day now so I hope it isn't too much trouble.
This line should be paged instead of page
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
I try to build an option for the user to highlight "related posts" to be shown at the bottom of a single post, so I put this code at the bottom of "content-single.php":
<div class="related-post">
<?php
$args = array( 'post__in' => array(38328, 38359, 21321) );//it's going to be the user choice, for example, I put three id's
$related_query = new WP_Query($args);
if ($related_query):
echo '<div class="row">';
while ($related_query->have_posts()): $related_query->the_post();?>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()):
the_post_thumbnail();
endif;
?>
<?php the_title() . '<br/>'; ?>
</div>
<?php endwhile;
echo '</div>';
wp_reset_postdata();
?>
</div>
But in some posts, I get 8 diefferent posts, in some posts I don't get any posts (though I put only three id's in post__in).
I guess the problem is that I try to loop inside the main loop.
I tried with query_posts and with get_posts, doesn't work.
Any help will be appreciate!
I think that is related to the missing post_type argument. Yet you need some corrections in your loop structure:
<div class="related-post">
<?php
$args = array( 'post_type' => 'your_post_type', 'post__in' => array(38328, 38359, 21321) );
$related_query = new WP_Query($args);
if( $related_query->have_posts() ){ ?>
<div class="row"><?php
while( $related_query->have_posts() ){ $related_query->the_post();?>
<div class="col-sm-4"><?php
if( has_post_thumbnail() ) the_post_thumbnail();
the_title();?>
</div><?php
}
wp_reset_postdata();?>
</div><?php
}?>
</div>
UPDATE
Please try to add 'posts_per_page' => 3 to the args array.
I have a solution to this post. Hope so you are wrong with the query post.
<div class="related-post">
<?php
$args = array(
'post_type' => 'your_post_type',
'post__in' => array(38328, 38359, 21321),
'post_status'=>'publish'
);
$related_query = new WP_Query( $args );
?>
<?php if ( $related_query->have_posts() ) : ?>
<div class="row">
<?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
<div class="col-sm-4">
<!-- do stuff ... -->
the_post_thumbnail();
the_title();
</div>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
</div>
Hope so it helps you to fetch out your Output Required.
Make sure you have post in the ID that you have mentioned in the post_in section.
Well, I manage to solve this by adding this 'ignore_sticky_posts' => 1 to the arguments. thanks #Pieter Goosen
I'm stuck at something that should be a really simple task.. But I don't know how to do it because it involves looping out the default "posts" of the global $wp_query.
Consider this code:
<section id="blog-wrapper">
<ul id="blog-list" class="list-reset">
<?php
$args = array(
'posts_per_page' => 5,
'paged' => $paged
);
global $wp_query;
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<li class="clear-fix blog-post">
<h1 class="blog-post-title"><?php the_title(); ?></h1>
<article class="blog-post-content"><?php the_content(); ?></article>
<span class="blog-post-info">Posted by <span class="author">
<?php the_author(); ?></span> on <?php the_time('F j, Y'); ?></span>
<div class="blog-post-border"></div>
</li>
<?php endwhile; ?>
</ul>
<?php
if ( function_exists( 'pagination' ) ) {
pagination( $wp_query->max_num_pages );
}
?>
<?php endif; wp_reset_postdata(); ?>
</section>
Now what I need is being able to pass the $args into the global $wp_query and loop out as many posts that I have posted. Then I need to pass the max_num_pages of the global $wp_query to a pagination function which creates the pagination.
How do I do this? All my attempts has failed and I'm going completely mad. This is my best attempt so far, but the pages after the first one are just completely empty instead of containing more posts..
Is this what you're looking for? http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
I created a single.php Wordpress template file to show standard WP Posts.
This is the truncated version of the code:
<?php
// First (Main Content) Loop
if (have_posts()) :
while (have_posts()):the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
// Second (Related Posts) Loop
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'category__in' => 3,
'posts_per_page'=> 4,
'paged' => $paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new wp_query($args);
if( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
echo '<div id="navigation">'.get_next_posts_link('Load More').'</div>';
endif;
}
$wp_query = null;
$wp_query = $temp;
?>
The 4 related posts are generated as expected and the get_next_posts_link generates a link to /post-name/page/2.
I'm using Paul Irish's Infinite Scroll plugin to load page/2's posts into the container (Which is working fine on the archive- and index.php pages on the site).
Unfortunately when you click get_next_posts_link the same 4 posts are appended to the container.
Any ideas how to get this to work?
I've tried a lot of other solutions on StackOverflow but so far none have worked.
Any help appreciated, cheers.