I have one custom loop in a custom homepage in WordPress that is pulling 4 posts with some banners in middle. All is working fine, however pagination always shows same posts. Is there a way for me to add pagination to this custom loop?
My index.php code:
<?php
query_posts(array(
'post_type' => 'post',
'showposts' => 4,
) );
?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 3) : ?>
<!-- banners -->
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php else : ?>
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<!-- banners -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<p align="center"><?php posts_nav_link(); ?></p>
Inside your query_post array add this line:
'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
and in settings -> reading set 4 posts per blog page.
Related
Hi all I am a new of wordpress I have problame with pagination, I used plugin Wp-pagnavi ,When I click link to page 2 it same as page 1,How to to fix it.
You can see it at
http://westecmedia.com/?page_id=758
And this my code in page-event.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; endif; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please :(
Include paged, documentation: https://codex.wordpress.org/Pagination
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
endwhile;
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
Also don't use query_posts to fetch data in WordPress, consider using https://codex.wordpress.org/Class_Reference/WP_Query.
Please ask WordPress related question here: http://wordpress.stackexchange.com
I hope this helps.
HI I have edited and make a template page of responsive theme to make some thumbnails of the features pics of the posts.
ALl it's okay I can see them, but even if the posts are 9 I could see only 5. If I add one ony I see the new one, there is something like "show only latest 5 posts" but I can't understand WHERE!
get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( 'loop-header' ); ?>
<?php responsive_entry_before(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php responsive_entry_top(); ?>
<?php get_template_part( 'post-meta-page' ); ?>
<div class="post-entry">
<?php the_content(__('Read more ›', 'responsive')); ?>
<?php wp_link_pages(array('before' => '<div class="pagination">' . __('Pages:', 'responsive'), 'after' => '</div>')); ?>
</div><!-- end of .post-entry -->
(this is my added code)
<ul>
<?php
$posts = get_posts();
foreach($posts as $post) : setup_postdata($post);
?>
<li><div class="fotoBoxContent"><a class="fotoBox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); the_title(); ?></a></div></li>
<?php endforeach; ?>
</ul>
<?php responsive_entry_bottom(); ?>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php responsive_entry_after(); ?>
<?php responsive_comments_before(); ?>
<?php comments_template( '', true ); ?>
<?php responsive_comments_after(); ?>
<?php
endwhile;
get_template_part( 'loop-nav' );
else :
get_template_part( 'loop-no-posts' );
endif;
?>
Try adding query_posts( 'posts_per_page=NUMBER_GOES_HERE' ); immediately before the <?php while (have_posts()) : the_post(); ?>
Replacing NUMBER_GOES_HERE with the amount of posts you want displayed. Use -1 to show all posts
Also within Wordpress itself Settings->Reading has a field where you can set Blog pages show at most
I'm still not sure what exactly you want to achieve but if you just want to have thumbnails of posts that are in the main loop, then you do not need to do additional query.
All you need to do is something like this:
1.) Before <?php if (have_posts()) : ?> you initialize some variable:
$thumb_data='';
2.) After <?php if (have_posts()) : ?>
$thumb_data='<ul>';
3.) replace your "added code" with this:
$thumb_data.='<li><div class="fotoBoxContent"><a class="fotoBox" href="'.get_the_permalink().'">'.get_the_post_thumbnail()." ".get_the_title().'</a></div></li>';
4.) After the main while loop, add:
$thumb_data='</ul>';
5.) Mow all the HTML code for the list of thumbnails will be in $thumb_data, so just echo this variable in the template where you want the HTML code to appear.
Now I've done this a couple other times with no problem, I have a main page using home.php and the blog as another page using "blog-home.php" blog template with all the right code to grab the posts but it's not displaying. The only difference is I've added a custom portfolio post field to the functions, would this be effecting it or could it be something else? I can access a post from the home page under latest post putting the code below but that's it.
<?php query_posts("post_per_page=1"); the_post(); ?>
<p><?php the_excerpt(); ?></p>
<?php wp_reset_query(); ?></div>
*UPDATE: I've tried another code but now it is only displaying the blog page as a post. *
<?php
/*
Template Name: Blog Home
*/
?>
<?php get_header(); ?>
<div id="contentwrapper">
<?php query_posts( array ( 'category_name' => 'Blog', 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="blogentry">
<h4><?php the_title(); ?> </h4>
<?php the_content(); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Maybe if you use
$posts = get_posts(array('numberposts' => 1));
global $post;
$post = $posts[0];
the_excerpt();
instead
query_posts();
is never a good idea change the global query if get_posts dosn't work for you try with WP_Query()
So I have a custom template that display posts from a particular category (depending on the get_post_meta('Category')).
The page works flawlessly for displaying posts from a 'News' Category. However, using the exact same code (minus how its displayed), it has problems with the Pagination for posts from the Calendar Category. Basically, the first page of Calendar posts display correctly, and then when you hit 'next page', the url updates to page/2, but the same posts are on the page.
Here's the code:
<?php $category = get_post_meta($post->ID, 'Category', true); ?>
<?php $cat = get_cat_ID($category); ?>
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?>
<?php $args = array(
'cat' => $cat,
'paged' => $paged
);
?>
<?php query_posts($args); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- If its a Calendar page -->
<?php if ($cat == 1): ?>
<div class='entry'>
<!-- List all Calendar info and Custom Fields -->
<ul>
<li><h3><?php the_title(); ?></h3></li>
<li><?php the_content(); ?></li>
<!-- ...And displays other data, etc..... -->
</ul>
</div>
<?php else: ?>
<div class='entry'>
<ul>
<li><?php the_title(); ?></a></h3></li>
<li><?php the_time('F jS, Y'); ?></li>
<li><?php the_content(); ?></li>
<!-- And display other data, etc .... -->
</ul>
</div>
<?php endif; ?>
<?php endwhile; ?>
<!-- Posts Nav Links -->
<?php posts_nav_link(' | ', '« Newer Entries', 'Older Entries »'); ?>
<?php endif; ?>
More than likely this has to do with how you are setting the paged parameter for query_posts() in a custom template.
Read more about this here: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html
In a nutshell you may need to use get_query_var('paged').
Can anybody look at the following codes? After I excluded the category 15, the next_posts_link does not work, nor does the page navigation plugin. When I click the page 2, 3,4, it only display the posts in page 1.
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; else: ?>
<?php next_posts_link() ?>
<?php endif; ?>
How about...
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; ?>
<?php next_posts_link() ?>
<?php endif; ?>
??
Try it like this:
<?php query_posts(array( 'cat' => -15, 'paged' => get_query_var('paged') ) ); ?>
This should work.