I m using wordpress 4.0 and I have create a custom search page template successfully but the problem is that the pagination link next is going to not found what should i do Please Guide Me....
here is my code
<div class="blog-post">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 3, 'ignore_sticky_posts' => 1, 'paged' => $paged );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog-span"><?php /* Start the Loop */ ?>
<?php get_template_part( 'content-search', get_post_format() ); ?>
<div class="space-sep20"></div>
</div>
<?php endwhile; ?>
<div class="nav-previous alignleft my-link"><?php previous_posts_link('« Previous');?></div>
<div class="nav-next alignright my-link"><?php next_posts_link( 'Next »', $the_query->max_num_pages ); ?></div>
<?php else : ?>
<div class="blog-span">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'weblizar' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'weblizar' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
<?php endif; ?>
</div>
Preserving Search Page Results and Pagination
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
?>
for more help please refer this link
Create search template
and serch for this title "Preserving Search Page Results and Pagination"
Related
I'm displaying blog posts on a page and have enabled pagination. However, the pagination doesn't seem to quite work.
When you navigate to the second page it displays the same posts as on the first page.
This is my code:
<div class="container-max">
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1'; ?>
<?php
$args = array( 'numberposts' => 5, 'offset' => 2, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date",'cat'=>'-8, -9, -7, -6, -5, -4','paged'=> $paged);
$postslist = get_posts( $args );
echo '<div class="latest_new_posts main-news">';
?>
<?php
$wp_query = new WP_Query($args);
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : the_post(); ?>
<?php foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="position:relative; margin:45px 0;" >
<div class="blog-date-news main-news-date"><span><?php the_date('d/m/Y'); ?></span></div>
<div class="blog-container main-news-container">
<div class="news-blog-title"><span><?php the_title(); ?></span></div>
<div class="news-blog-excerpt"> <?php echo excerpt(500); ?> </div>
<div class="news-blog-more main-news-blog-more"> <img src="http://www.mariadev.co.uk/wp-content/uploads/2017/05/read-more.png"/></div>
</div>
</div>
<?php endforeach; ?>
<?php endwhile; ?>
<div class="blog-page-next"><?php next_posts_link( 'Next', $wp_query->max_num_pages );
?> </div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
Any ideas what I'm doing wrong?
Replace 'numberposts' => 5 with 'posts_per_page' => 5
Can you use query_posts() ?
$postslist = get_posts( $args );
replace
$postslist = query_posts( $args );
I have set a special category for posts that I want to show on the front page here: http://aquadiva.it/en/
some of them won't show though. there should be four.
I suspect the problem lies in the fact that two of the four posts are custom posts. The category itself works, because, if I search for category, what it gives is those four posts: http://aquadiva.it/en/category/frontpage/
while on the front page I have only the ones belonging to "regular" posts (articles) and not to custom posts (products).
this is my content-frontpage.php:
<?php
$nirvanas = nirvana_get_theme_options();
foreach ($nirvanas as $key => $value) { ${"$key"} = $value; } ?>
<section id="container" class="one-column <?php //echo nirvana_get_layout_class(); ?>">
<div id="content" role="main">
<?php //cryout_before_content_hook();
$nirvana_old_posts_per_page = get_option( 'posts_per_page' );
if ( have_posts() ) :
/* Start the Loop */
update_option( 'posts_per_page', $nirvanas['nirvana_frontpostscount']);
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged) );
while ( $the_query->have_posts() ) : $the_query->the_post();
global $more; $more=0;
get_template_part( 'content/content', get_post_format() );
endwhile;
if($nirvana_pagination=="Enable") nirvana_pagination($the_query->max_num_pages); else nirvana_content_nav( 'nav-below' );
else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No Posts to Display', 'nirvana' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php printf(
__( 'You currently have no published posts. To hide this message either add some posts or disable displaying posts on the Presentation Page in theme settings.', 'nirvana' ),
esc_url( admin_url()."post-new.php"),
esc_url( admin_url()."themes.php?page=nirvana-page") ); ?>
</p>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php
endif;
update_option( 'posts_per_page', $nirvana_old_posts_per_page);
//cryout_after_content_hook();
?>
</div><!-- #content -->
<?php //nirvana_get_sidebar(); ?>
</section><!-- #container -->
what can I do?
Exchange this:
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged) );
with this:
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged, 'category_name'=>'frontpage', 'post_type' => array('post','product')) );
Let me know if this works. If not, let me know what the name of your custom post type is.
I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category.
Can someone please help em to add a category there one is "surf" , I've tried everything.
Or maybe a different way to do it?
Thank you.
<div class="posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title section small-padding">
<h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div> <!-- /post -->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="archive-nav">
<?php echo get_next_posts_link( '« ' . __('Posts Antigos', 'radcliffe')); ?>
<?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' »'); ?>
<div class="clear"></div>
</div> <!-- /post-nav archive-nav -->
<?php endif; ?>
<?php endif; ?>
</div> </div> <!-- /posts -->
If you are trying to display category by ID , Then
global $post;
$args = array( 'category' => '12' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Note: In $args = array( 'category' => '12' ); 12 is the ID of
the category
But if you want to display category by Name, Then
global $post;
$args = array( 'category_name' => 'uncatogerized' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Here, uncategorized is a category name
I have problem to change content on page when for ex. url change from
/?page_id=55&paged=1 to /?page_id=55&paged=2...
So I have problem to display next/previous page content, it just show first 4 post on page and when I click next nothing hapend with content just url change.
Here is my blog template:
<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
<div class="row main-part">
<?php
$args = array(
'category_name' => 'blog',
'paged' => $paged,
'posts_per_page' => 4
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
?>
<div class="col-xs-12 col-md-3">
<h1><?php the_title(); ?></h1>
<p>
<?php the_excerpt(); ?>
</p>
<p><a class="btn btn-primary" href="<?php the_permalink() ?>" role="button">View details »</a></p>
</div>
<?php endwhile;endif; ?>
<?php
wp_reset_postdata();
wp_reset_query();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
<?php get_footer(); ?>
I'm trying to use WP pagination on post archive page but exclude posts from one category to be shown there.
When I add this to my code the page2,3,4... of the archive display the same first 10 posts:
<?php query_posts('cat=-4');?>
This is the whole code of my page template so I would be grateful for all your help:
<?php
/*
Template Name: Post archive
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="content col-md-9">
<div class="home-content">
<!-- Show posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php query_posts('cat=-4');?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<div style="float:left; margin:1%;">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'thumbnail', array( 'class' => 'img-post')); // show featured image
}
?>
</div>
<h1 class="post-thumb"><?php the_title(); ?></h1>
<h4>Category: <?php the_category(', '); ?></h4>
<p><?php the_excerpt(); ?></p>
<hr style="margin-bottom:5%">
<?php endwhile; ?>
<!-- pagination -->
<div class="nav-previous alignleft" style="margin-top:-1%"><?php next_posts_link( 'See older posts' ); ?></div>
<div class="nav-next alignright" style="margin-top:-1%"><?php previous_posts_link( 'See newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-3 sidebar unstyled">
<?php dynamic_sidebar( 'home1' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'home2' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'articles1' ); ?>
</div>
</div>
</div>
</body>
</html>
<?php get_footer(); ?>
This code fixed my page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat'=> -4,
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
Modifying to reflect OP's solution for benefit of future readers
Change query() slightly as shown below
$args = array(
'cat'=> -4,
'posts_per_page'=> 10,
'paged'=> $paged
);