I am using this code to show 5 posts (any post's would do)
<div id="featured-post-section">
<div id="post-list">1
<?php query_posts( 'posts_per_page=5' ); ?>2
As you can see, I've added a 1 and 2 numbers.... One before and one after ... I only get 1 2 ... No posts are showing at all.
What am I doing wrong?
That function only tells Wordpress how many posts to output. You still have to use the WP loop to actually perform the output.
<?php
// The Query
query_posts( 'posts_per_page=5' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
Related
I have spent some time reading through the WordPress Codex, as well as various themes, and can see that some developers insert <?php wp_reset_postdata(); ?> after the endif; in a Blog Loop whilst others insert the code between the endwhile; and endif; of a Blog Loop. I have tried both locations but have yet to see a difference. Is there a correct location?
This function supposed to reset the secondery query that you run.. the function the_postis letting you to use all the functions that you can run in the loop like the_title() the_content and so on..
So you reset the the_post function and after the endwhile; you can reset it already. and use your main query inside the if statment too if you like.
<?php
// this is the main query check if there is posts
if ( have_posts() ) :
// loop the main query post and run the_post() function on each post that you can use the function the_title() and so on..
while ( have_posts() ) : the_post();
the_title(); // title of the main query
// the is second query
$args = array( 'posts_per_page' => 3 );
$the_query = new WP_Query( $args );
// check if there is a posts in the second query
if ( $the_query->have_posts() ) :
// run the_post on the second query now you can use the functions..
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
endwhile;
// reset the second query
wp_reset_postdata();
/* here we can use the main query function already if we want..
its in case that you want to do something only if the second query have posts.
You can run here third query too and so on...
*/
the_title(); // title of the main query for example
endif;
endwhile;
endif;
?>
wp_reset_postdata() is only required if you have a secondary loop (you're running additional queries on a page). The purpose of the function is to restore the global post variable back to the current post in the main query.
Example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
OUTPUT MAIN POSTS HERE
<?php endwhile; endif; ?>
In the code above I'm simply using the main loop to display posts. There's no reason to include wp_reset_postdata(). The global post variable is exactly what it's supposed to be.
If somewhere else on the page I decide to add a secondary loop, then I'll need wp_reset_postdata().
// This query is just an example. It works but isn't particularly useful.
$secondary_query = new WP_Query( array(
'post_type' => 'page'
) );
if ( $secondary_query->have_posts() ) :
// Separating if and while to make answer clearer.
while ( $secondary_query->have_posts() ) : $secondary_query->the_post();
// OUTPUT MORE STUFF HERE.
endwhile;
wp_reset_postdata();
endif;
To answer your original question: it usually comes after endwhile and before endif. It's the call to the_post() which actually changes the global post variable. If there are no posts then the post variable remain the same and there's no reason to use the reset function.
I have two custom post types from the archive.php One is archive-slug.php and the other is category-slug.php. The pagination works on the archive-slug.php but the same code on the category-slug.php won't even show. I'm somewhat new to Wordpress and php so i'm sure i'm missing something here, I just don't know what?
<?php
// Custom Post Type
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'available_dogs',
'category_name' => 'adopted-dogs',
'posts_per_page'=> 9,
'paged'=> $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="dog-info-box col-lg-4 col-sm-6 col-xs-12">...</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php
// Get the pagination
fusion_pagination( $pages = '', $range = 2 );
?>
<?php if( $sidebar_exists == true ): ?>
<?php wp_reset_query(); ?>
As I want to inform you that when you create a file "category-slug.php : it's an default template.No need to add the query posts.
It will display the posts of the category whose slug is added in file like category-slug.php.Here is the modifying code.
if (have_posts() ) :while ( have_posts() ) : the_post();
the_content();//content is going on.
endwhile;//pagination functionfusion_pagination( $pages = '', $range = 2 );else : _e( 'Sorry, no posts matched your criteria.' ); endif;
Precaution :1:Pagination code should be placed just before of the while loop.2:In your code pagination code have been place after the endif that's why it is not working.
Thanking you.
I used this code and got it working. It's not pretty but it will do for now. For what ever reason when I try to use it after the loop it doesn't work. When I use it after the endif it works.
<?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 } ?>
Please follow this steps.
1: Please make sure, your category have the posts.
2: If your category slug is "test" then your file name should be " category-test.php . if any confusion then please let me know.
3: See the code and write in file.
if(have_posts()) :while(have_posts()): the_post();the_title();the_content();endwhile;//pagination of wp
previous_posts_link( '« Newer Entries' );
next_posts_link( 'Older Entries »', 0 );endif;
Note:1:Please place this code in file and run if it will running successfully, then we will add the designing part.
2: If you can share the website URL then I can also give suitable solution because then I can seen the category slug etc .
Please let me know if still needs problem.
Thanking.
I finally fixed a variation of this same issue.
This issue was occurring with category archive pagination.
Initially thought it was within the Loop - turns out the theme I was working on had these filters in a file, custom.php, which manually rewrote the permalink structure.
add_filter('category_link', 'themename_change_archive_link', 100, 2);
add_action('init', 'themename_archive_rewrite', 50);
Pagination didn't like that due to what looks like a conflict with a recent WooCommerce update, so I removed these filters, used the Custom Permalink plugin to rewrite the category permalinks, and it worked! Now I have a custom permalink structure AND functioning category pagination.
Im using advanced custom fields and have setup a custom post type for testimonials, on the testimonial page there is a relationship field (display_on_page) where you would select which page to display the testimonial on.
The issue is that the testimonial displays on everypage when a page is chosen
Does anyone know what I am doing wrong on the below please?
<?php query_posts( 'post_type=Testimonial'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $posts = get_field('display_on_page');
if( $posts ): ?>
<?php the_field('the_testimonial'); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
If I understood correctly, you should check if the value that you get from get_field('display_on_page') matches the current page ID.
(I'm assuming that the page ID is what is stored by the custom field you created).
If that's the case, you may query your posts filtering by custom field values, like such:
<?php
// Fetches current page ID, assuming that this is page.php or similar
$current_page_id = get_the_ID();
query_posts(
'post_type=Testimonial',
'meta_key' => 'display_on_page',
'meta_value' => $current_page_id // Only gets Testimonials that are set for this page
);
while ( have_posts() ) : the_post();
the_field('the_testimonial');
endwhile;
// Resets default WP Post Data *AFTER* the loop is complete
wp_reset_postdata();
?>
With the code below, I show 5 posts ordered randomly at the end of a post. As you can see, if the current post (ID) is one of the random chosen posts then it doesn't show up.
That means instead of the 5 posts I want to show there are 4. In other case, there will be 5 posts.
My question is how to edit the code below to show only 5 posts even if the current post is one of the randomly chosen.
<?php
query_posts( 'posts_per_page=5&orderby=rand' );
while (have_posts()) : the_post();
if ( $post->ID == $ID ) continue;
the_title();
endwhile;
wp_reset_query();
?>
Select 1 extra post anticipated and only show it if you had to skip one.
-edit-
This is very ugly but it should work:
<?php
query_posts( 'posts_per_page=6&orderby=rand' );
$counter = 0;
while (have_posts()) : the_post();
if ( $post->ID == $ID || $counter == 5 ) continue;
$counter++;
the_title();
endwhile;
wp_reset_query();
?>
I created a new template in Wordpress to show the latest articles. After, I will sort them in other ways. My problem is I can not use the pagination correctly.
The next page and previous page are working ok, but the posts are not changed. The first three are the only that are shown.
Can you give me a clue about this ?
<?php
/*
Template Name: Latest Prizes
*/
get_header();
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
posts_nav_link(' · ', 'previous page', 'next page');
// Reset Query
wp_reset_query();
get_footer(); ?>
From the Wordpress Codex on query_posts():
...
For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:
query_posts( 'posts_per_page=5' );
Change your query_posts() to this:
query_posts(array('posts_per_page' => 3, 'paged' => get_query_var('page')));