WP-pagination and excluding category from page - php

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
);

Related

Pagination doesnt work in custom post type

Pagination template part includes common pagination function with style. The template part works for archive.php (it's for "single", you know default wp file) but doesn't work for custom post type.
Why not? How to solve it?
<?php get_header(); ?>
<main role="main">
<!-- section -->
<?php get_template_part( 'breadcrumb' );?>
<!-- Inner Pages Main Section -->
<section class="ulockd-service-details">
<div class="container">
<div class="col-md-12">
<div class="row">
<?php
/**
* Setup query to show the ‘services’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php //if (have_posts()): while (have_posts()) : the_post(); ?>
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
?>
<div class="col-md-12 ulockd-mrgn1210">
<div class="ulockd-project-sm-thumb">
<img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
</div>
</div>
<?php
}
?>
<div class="col-md-12 ulockd-mrgn1210">
<article class="ulockd-pd-content">
<div class="ulockd-bp-date">
<ul class="list-inline">
<li class="ulockd-bp-date-innner">On <span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></li>
</ul>
</div>
<h3><?php the_title(); ?> </h3>
<p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
<a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
</article>
</div>
<?php get_template_part('pagination'); ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>
</div></div></div></section>
<?php get_footer(); ?>
</main>
First of all, you don't need to include that template inside while loop. That's wrong.
Then, if you want to have an archive page for your team post type, you need to provide 'has_archive' => true within register_post_type() function args.
Also, consider changing archive page default slug if needed. If you do so, you need to open Settings > Permalinks for resetting your permalinks structure.
Then you could either use standard archive.php for the whole team archive page and standard template-parts/content.php for one post inside the loop or rewrite either of those by creating archive-team.php or content-team.php. And the_posts_pagination() function will work on proper archive page (either archive.php or archive-team.php).
I would try two things.
Add the argument paged to your wp_query args :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
);
Put your pagination template outside the loop :
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>

Use image as a Wordpress conditional

I'm trying to use a image as a conditional to show different post types. I have one post with images and another one without images. So I built a If else statement and a while loop that calls the post. At the end I have a page number code. But the loop is infinite, I'm not finding where to close it. Some one can help me?
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="texto-longo">
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
<div class="row-2 w-row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'cat' => '3',
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="_w-image container-novidades">
<?php if( get_field('imagem_do_evento') ): ?>
<div class="w-row">
<div class="column w-col w-col-4"><img class="<?php the_field('imagem_do_evento');?>">
</div>
<div class="colomn-text-novidades w-col w-col-8">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('imagem_do_evento');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('imagem_do_evento');?></p>
</div>
</div>
</div>
<?php wp_reset_query(); else : ?>
<p>Ainda não temos novidades :(</p>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="navegacao-paginas">
<div class="paginacao twisted w-inline-block">
<div class="seta-text"><?php echo get_previous_posts_link( '⟶' ); // display newer posts link ?></div>
</div>
<div class="paginacao w-inline-block">
<div class="seta-text"><?php echo get_next_posts_link( '⟶', $wp_query->max_num_pages ); // display older posts link ?></div>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_query(); else : ?>
<?php endif; ?>
Try to put the endwhile and edn if in the same row like this
<?php endwhile; wp_reset_query(); endif; ?>
I Figured it Out. It was a div opening outside the if statement. And I wasn't closing all if else properly. Check out the final code.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="texto-longo">
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
<div class="row-2 w-row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'cat' => '4',
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php if( get_field('foto_novidades') ): ?>
<div class="_w-image container-novidades">
<div class="w-row">
<div class="column w-col w-col-4"><img class="img-novidades" src="<?php the_field('foto_novidades');?>">
</div>
<div class="colomn-text-novidades w-col w-col-8">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('sub_titulo_novidades');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('texto_novidades');?></p>
</div>
</div>
</div>
<?php else:?>
<div class="container-novidades">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('sub_titulo_novidades');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('texto_novidades');?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="navegacao-paginas">
<div class="paginacao twisted w-inline-block">
<div class="seta-text"><?php echo get_previous_posts_link( '⟶' ); // display newer posts link ?></div>
</div>
<div class="paginacao w-inline-block">
<div class="seta-text"><?php echo get_next_posts_link( '⟶', $wp_query->max_num_pages ); // display older posts link ?></div>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_query(); else : ?>
<p>Ainda não temos novidades :(</p>
<?php endif; ?>

Can someone help me to add a category to this Post?

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

Have I made a mistake with my WordPress pagination?

The pagination on a site I am working on is not working; I really can't work out why as it is the same as the other templates within the site. I am wondering if there is a problem with the loop, perhaps where I have specified what category I want it to pull.
This is the page I am having trouble with:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php $catquery = new WP_Query( 'cat=388' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php get_template_part( 'content-archive', get_post_format() ); ?>
<?php endwhile; ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
</div>
<!-- end two_third -->
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Default Sidebar') ) : ?>
<?php endif; ?>
</div>
</div>
<!-- end container -->
<?php get_footer(); ?>
The template that does work correctly is here:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
<?php endif; ?>
</div>
<!-- end two_third -->
<aside>
<div class="one_third lastcolumn newssidebar">
<?php get_sidebar(); ?>
</div>
</aside>
</div>
<!-- end container -->
<?php get_footer(); ?>
I have tried copying this over into the template that doesn't work without success.
When you're using a custom query, you have to add the page number to your arguments.
<?php /* Start the Loop */ ?>
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$catquery = new WP_Query( array('cat' => 388, 'paged' => $paged) ); while($catquery->have_posts()) : $catquery->the_post(); ?>
You should also pass the number of pages to get_next_posts_link
next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ), $catquery->max_num_pages );

Next post link going to not found on search page template

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"

Categories