I have a custom static frontpage that show recent blogg updates.
But I cant seem to figure out why the pagination doesn't work? I gives me only the link to previous page but when I click the link it just shows the same page but the url changes from
www.homepage.com
to www.homepage.com/page/2
this is my loop.
<?php query_posts('posts_per_page=2&category=blogg'); ?> <?php while (have_posts()) : the_post(); ?>
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : /* if post has post thumbnail */ ?>
<div class="image"><?php the_post_thumbnail('archive-preview'); ?></div>
<?php endif; ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<br/>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } else { ?>
<?php next_posts_link(__('← Older Entries', 'framework')) ?>
<?php previous_posts_link(__('Newer Entries →', 'framework')) ?>
<?php } ?>
lease use the code it will help you:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts('posts_per_page=3&paged=' . $paged);
?>
wordpress codex pagination
Related
I have this Page Template, developed by someone else a long long time ago, which shows all my Posts in a grid based on the category they're in.
Now, I want to have such a page for my Custom Post Type but I can't get it to work. I tried replacing the
<?php if (have_posts()) : while (have_posts()) : the_post();?>
with
<?php $args = array('post_type'=>array('shopitems')); query_posts($args); if (
have_posts() ) : while ( have_posts() ) : the_post();?>`
which seems to be the go-to answer everywhere I look. but for me, it turns out empty.
Below the original Page Template, I want to change this so it loads the 'shopitems' Custom Post Type.
Any help or nod in the right direction is greatly appreciated!
<?php get_header(); ?>
<div class="content-sidebar-wrap">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php $category_list= get_the_content(); ?>
<?php endwhile; endif; ?>
<div id="content" class="post-category page">
<?php $category_name=explode(',', $category_list);
$catIDs="";
foreach ($category_name as $cat_name) {
$catIDs.= get_cat_ID(trim($cat_name)).",";
}
$catIDs=trim($catIDs,",");
$i=0;
?>
<?php $thePosts= query_posts('cat='.$catIDs.'&post_status=publish&posts_per_page=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ) { ?>
<?php // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?>
<?php if($i%4==0) { ?>
<div class="clear"></div>
<?php } $i++; ?>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img alt="" src="<?php echo $image[0]; ?>" class="thumb-img">
<span class="thumb-title">
<?php
$second_name = get_field('second_name');
if( $second_name && !empty($second_name))
echo $second_name;
else
the_title();
?>
</span>
</a>
</div>
<?php } endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Just you need to modify your query like below.
<?php
$args = array(
'post_type'=>'shopitems'
);
$query = new WP_Query($args);
if ($query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
//do your code here
endwhile;
else:
// No posts found
endif;
?>`
I try to display a message if there are no posts for a certain category
<?php $recent = new WP_Query("cat=9&showposts=10");
while($recent->have_posts()) : $recent->the_post();?>
<?php the_title(); ?>
<?php the_content();?>
<?php endwhile; ?>
But when I add an 'else', I get the blank screen
<?php else: ?>
message ////
<?php endif; ?>
you can do something like this
<?php
if ( have_posts() )
{
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
}
else {
echo 'Sorry, no posts were found';
}
?>
I created a page template at http://www.durgeshsound.com/gallery/
Here my pagination buttons are not working. this issue arises when permalink format is http://www.testsite.com/sample-post/
But when I set permalink format to default for example http://testsite.com/?p=123 then it starts working
and creates a working pagination link like this http://www.durgeshsound.com/?page_id=81&paged=2.
I want this format http://www.testsite.com/sample-post/ in the links.
here is what i tried for custom page template
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=gallery&posts_per_page=9&paged=' . $paged); ?>
<?php if (have_posts()) : while (have_posts()) : the_post();
if ( get_post_gallery() ) :
$gallery = get_post_gallery( get_the_ID(), false );
?>
<?php the_post_thumbnail('large'); ?>
<?php the_title( ); ?>
<?php
endif;
endwhile; endif;
?>
<?php kriesi_pagination(); ?>
<?php get_sidebar('gallery'); ?>
<?php get_footer(); ?>
Please help.
Below is custom Page : Replace $cat with your resp. ID
<ul class="clearfix">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$recentPosts = new WP_Query();
$recentPosts->query("cat=$cat&showposts=10&paged=".$paged); ?>
<?php if($recentPosts){ ?>
<?php if ( $recentPosts->have_posts() ): while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class('interview-list'); ?>>
<div class="video-thumb">
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php if(has_post_thumbnail()) {
the_post_thumbnail(array(500,393));
}else {
echo '<img src="'.get_bloginfo("template_url").'/images/no-image.jpg" width="500px" height="393px"/>';
}
?> </a>
</div>
<div class="image-con">
<div class="int-title">
<?php the_title(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
<?php else: ?>
<h2 class="post-title">No Photos</h2>
<?php endif; ?>
<div class="older-link"><?php next_posts_link('« Older Entries', $recentPosts->max_num_pages) ?></div>
<div class="newer-link"><?php previous_posts_link('Newer Entries »') ?></div>
<?php } ?>
</ul>
I'm having a problem with my Wordpress theme. My next_posts_link() only reloads the same page. Here's my code.
<div id="main">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="utdrag">
<h2><?php the_title(); ?></h2>
<div class="crop"><a href="<?php the_permalink(); ?>">
<?php
if ( get_the_post_thumbnail($post_id) != '' ) {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="';
echo catch_that_image();
echo '" alt="Image unavailable" class="crop" />';
echo '</a>';
}
?></a>
</div>
<?php the_excerpt(); ?>
</div>
<?php endwhile; else: endif; ?>
<?php next_posts_link();?><?php previous_posts_link();?>
</div>
I'm using a static page as my front page. As you can see it's only displaying posts that have the same category as the page title: query_posts('category_name='.get_the_title().'&post_status=publish,future');
This is something I want to keep. So does anyone know why it just reloads? Why it isn't changing to the next page?
I figured it out! I'm posting it here in case someone else is having the same problem!
I added this code at the top:
<?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 replaced this:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
with this:
<?php query_posts('catery_name='.get_the_title().'&showposts=2'.'&paged='.$paged); ?>
For more information, check out this link:
http://wordpress.org/support/topic/front-page-wp-query-pagination-issue-repeating-posts-next_posts_link-help
These two functions does not work on static pages.
From the documentation for wp_next_post_link():
This function does not work with static pages.
However, take a look at this article. It talks about how to create a static front page with dynamic content.
On this page here, I'm using the following php code to show the posts:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($args);
if( have_posts() ) :?>
<?php twentyeleven_content_nav( 'nav-above' );?>
<?php
$withThumb = 5;
while ( have_posts() ) : the_post();
if ($withThumb-- > 0) { ?>
<div class="post-thumb-title">
<?php the_post_thumbnail(array(632,305));?>
<p class="thumb-title2"><?php the_title(); ?></p>
<p class="news-date"><?php the_time('F jS, Y') ?></p>
<div id="post-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php } else { ?>
<div class="post-title">
<p class="thumb-title2">
<?php the_title(); ?>
</p>
</div>
<?php } ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
The issue is that if you click on "Older Posts", it shows the same posts and not the older ones.
It does the same when you open the 3rd page, 4th page and so on.
How do I fix this?
Thanks
You need to add the page number to the arguments of the query_posts() call.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
query_posts($args);
See the documentation.
Note: If you've already defined $args and as a string not an array, you'll want to concatenate &paged=page_number_here, instead of adding a new key-value pair.