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.
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 would like to display post from only one category. How should I change this function?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post','paged' => $paged);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'post-format/content', get_post_format() );
endwhile;
else:
get_template_part( 'post-format/content', 'none' );
endif;
?>
<?php
/*
Template Name: Boats'n'Hoes
*/
?>
<?php get_header(); ?>
<div id="main">
<div id="content" class="narrowcolumn">
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
reference :- https://wordpress.org/support/topic/display-the-posts-of-one-category-in-a-page-solved
Something I created for a client was a template that lists posts from a category that has the same name as the page.
So, create a Page called "funnies" if you want to list all posts in the "technology" category.
Oh, and the original content of the Page is displayed too, if you need an introductory text.
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Pass this with $args
'category_name' => 'cat-name',
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 want to exclude category from shoowing my blog posts. My category id is 62. category name is perfect_work
Here is my wordpress blog template code:
<div id="left" class="eleven columns">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="title">
<h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?></a></h2>
<div class="postmeta"> <span>by <?php the_author_posts_link(); ?></span> | <span><?php the_time('l, F jS, Y') ?></span> | <span><?php the_category(', '); ?></span> </div>
</div>
<div class="entry">
<?php $image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'top_feature'); ?>
<img src="<?php echo $image_attr[0]; ?>" class="postim scale-with-grid" id="blog-thumb" >
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
I already tried using
$wp_query = new WP_Query('cat=-62');
its not work. I also put
<?php query_posts('cat=-62'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Its work but page navigation not work, and also not showing others post. only 1st 5 post show.
Any Solution?
Get the page number
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
Then you may use
$wp_query = new WP_Query('cat=-62&paged=' . $paged);
Or use
$cat_id = get_cat_ID('perfect_work');
$wp_query = new WP_Query('cat=-' . $cat_id . '&paged=' . $paged);
Then loop
if($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
// ...
endwhile;
endif;
Try this one you have to specify the showposts to limit the posts
<?php $wp_query->set( 'cat', '-62' ); ?>
<?php query_posts( 'showposts=10' ); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
.
.
.
<?php endwhile; ?>
<?php endif; ?>
Note : The minus sign indicates the exclusion of all Posts which
belong to that category from being retrieved from the database. In
turn, the Loop will never have Posts of that category id and only
process the specified number of Posts of other category ids.
Please read the codex on WP_Query, it is imo very detailed, look at the category params part
Just add a minus sign - in front of the categories you dont want, so the below code would mean show posts with category 10 and 11, but exclude category 62
$recent = new WP_Query("showposts=3&cat=10,11,-62")
You don't need to use the $temp variable before or after the query. You should use something like this:
//This should do the trick
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => -62,
'paged' => $paged
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
//The real trick!
<?php wp_reset_postdata(); ?>
Two things to note:
The paged query parameter
To reset the query use wp_reset_postdata()
I have this code to get the latest 3 posts from "novels" category and display the content inside html tags.
<? $novels = get_option('of_novels') ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('category_name=$novels&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="sliderunit">
<?php the_post_thumbnail(); ?>
<div class="novelsslidertitle">
<div class="arrow-left"></div>
<img class="cross" src="<?php bloginfo('stylesheet_directory'); ?>/images/cross.png"/>
<?php the_title(); ?>
<h3>رواية</h3>
</div>
</div>
<?php endwhile;?>
What I need to know is how to add default next post in wordpress to this code?