Pagination not working on the Category Template - wordpress

I m trying to add the pagination for the category template in my application but when i click on the next option it redirect's me to the page and say no page found. The Same Code i Have written for the blog page too it is working but only in case of categories page it is not working. Can anyone please help me for this
<?php
/**
* A Simple Category Template
*/
get_header();
$current_page = get_queried_object();
$category = $current_page->name;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page'=> 1,
'category_name' => $category,
'paged' => $paged
)
);
?>
<div id="content" role="main">
<?php
// Check if there are any posts to display
if ( $query->have_posts() ) :
// The Loop
while ( $query->have_posts() ) : $query->the_post();
$image_id = get_post_thumbnail_id();
$image_src = wp_get_attachment_image_src($image_id, $size)[0];
?>
<br>
<section>
<article>
<img width="270" height="178" src="<?php echo $image_src; ?>">
</article>
<nav>
<ul>
<?php
foreach((get_the_category()) as $category) ?>
<li> | <?php echo get_the_date(); ?></li>
<li><a href="<?php the_permalink(); ?>/">
<?php the_title(); ?></a></li>
<li>
<img width="40" height="40" src="<?php echo $image_src ?>">
</li>
<li><?php echo get_the_author(); ?></li>
</ul>
</nav>
</section>
<?php endwhile; ?>
<?php //echo "<pre>"; ?>
<?php //var_dump($query); ?>
<?php //echo "</pre>"; ?>
<p><?php next_posts_link('Next',$query->max_num_pages); ?></p>
<p><?php previous_posts_link('Previous'); ?></p>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php
get_footer();
?>

Related

Wordpress CPT pagination returns 404 page

I want to add pagination to my CPT wordpress posts. I've seen some of the solutions, but neither I see no pagination or the pagination does not work.
I've added some pagination code, and it shows the correct number of pages but when I click on page number it returns 404 page. The url is also correct.
Here's my code:
<div class="container">
<div class="row">
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
<?php $args = array( 'post_type' => 'Realizacje', 'posts_per_page' => 6, 'category_name' => array('projekty-wnetrz', 'wykonczenia-pod-klucz'), 'paged' => $paged); ?>
<?php $loop = new WP_Query($args); ?>
<?php $count=0; ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
$num = $loop->post_count;
?>
<div class="col-md-4">
<div class="realisation">
<!-- <div class="gallery">
<?php
$images = get_field('galeria');
$size = 'medium';
if( $images ): ?>
<div id="lightgallery<?php echo $count; ?>">
<?php foreach( $images as $image ): ?>
<div class="gal">
<img src="<?php echo $image; ?>"/>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div> -->
<div class="galeria">
<div class="lightg" id="lightgallery<?php echo $count; ?>">
<a href="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="relative-box"><img class="first_image"src="<?php echo get_the_post_thumbnail_url(); ?>"/>
<div class="demo-gallery-poster">
<i class="fa fa-search" aria-hidden="true"></i>
</div></div>
<div class="gallery-title"><h5 class="gallery-t"><?php the_title(); ?></h5></div>
</a>
<?php
$count=$count+1;
$images = get_field('galeria');
$size = 'large';
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<img src="<?php echo $image; ?>"/>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<h5>Brak realizacji</h5>
<?php endif; ?>
<div class="pagination">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'prev_text' => '«',
'next_text' => '»'
) );
?>
</div>
<?php wp_reset_postdata(); ?>
</div>
</div>

Advanced custom fields repeater with post object not repeating content

I am attempting to use a repeater with a post object inside as seen here:
Picture of ACF repeater with post object
It is only showing one out of two Reps:
Rep map
Here is the code I am using to attempt to accomplish this:
<?php
$args = array(
'post_type' => 'find-your-rep',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
$i = 0;
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); $i++ ?>
<span id="div<?php echo $i ?>" class="targetDiv">
<div class="repCard">
<?php if ( have_rows( 'reps' ) ) : ?>
<?php while ( have_rows( 'reps' ) ) : the_row(); ?>
<h3><?php the_sub_field( 'location' ); ?></h3>
<?php $rep = get_sub_field( 'rep' ); ?>
<?php if ( $rep ) : ?>
<?php $post = $rep; ?>
<?php setup_postdata( $post ); ?>
<div class="fullname"><?php the_title() ?></div>
<?php if ( get_field( 'company' ) ) : ?><div class="company">
<?php the_field( 'company' ); ?></div>
<?php endif; ?>
<?php if ( get_field( 'phone' ) ) : ?>
<div class="phone"><i class="phone icon"></i>
<a href="tel:<?php the_field( 'phone' ); ?>">
<?php the_field( 'phone' ); ?></a>.
</div>.
<?php endif; ?>
<?php if ( get_field( 'email' ) ) : ?>
<div class="email">
<i class="envelope icon"></i> <a href="mailto:<?php the_field( 'email' ); ?>">
<?php the_field( 'email' ); ?></a></div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
Sorry, there's no rep assigned to this location.
<?php endif; ?>
</div>
</span>
<?php endwhile; wp_reset_postdata(); ?>
I'd appreciate any help/direction to resolve this.

Two custom loops on same page in wordpress?

I wasn't able to find anything on here, so I am bringing this question to you. The following code does mainly what I want, only the page-cycle doesn't work: When I want to navigate to older posts, the page that is seen displays the same new posts again. How do I do this properly?
So index.php has two loops:
<?php
$args = array( 'numberposts' => 1,
'post_status'=>"publish",
'post_type'=>"post",
'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
// regular post html with wp tags
<?php endforeach; ?>
<?php
query_posts('posts_per_page=12&offset=1');
if (have_posts()) : ?>
<h3>Headline</h3>
<?php while (have_posts()) : the_post(); ?>
// regular html with wp tags for these posts (teasers, with the_excerpt)
<?php endwhile; ?>
<div class="navigation">
<span class="nextlink"><?php next_posts_link( 'Older', 0 ); ?></span>
<span class="previouslink"><?php previous_posts_link( 'Newer' ); ?></span>
</div>
<?php else : ?>
<h3><?php _e('Nothing found'); ?></h3>
<?php endif; ?>
Here is the explanation: One visits the blog: The most recent blog post is displayed in full length in one block, the 12 older posts (excluding the first one with offset, so 2–13) are displayed in another block with content teasers and a next-previous-navigation at the bottom to display post teasers 14–25 and so on. (The problem: it's always 2–13)
// Edit: Code in response to the top answer, now the full code of the index.php. Notice, that the HTML has changed, since I have used normalized HTML, since it should usually not matter. This times it's the actual code.
<?php get_header(); ?>
<div class="box blog-block">
<?php
$args = array( 'numberposts' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<span class="head"><?php the_time('j. F Y') ?></span>
<h3><?php the_title(); ?></h3>
<div class="entry">
<?php global $more;
$more = 1;
the_content();
?>
<p class="author"><?php the_author(); ?></p>
<p><a class="more-link" href="<?php the_permalink(); ?>#respond" title="<?php the_title();?>">Kommentieren</a>
</div>
<?php endforeach;
wp_reset_postdata(); /* 1. reset post data */
?>
</div>
<div class="box" id="recentposts">
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$display_count = 12;
$excluded_count = 1;
$offset = ( ($paged-1) * $display_count ) + $excluded_count;
$args = array(
'post_status'=>"publish",
'post_type'=>"post",
'orderby'=>"post_date",
'posts_per_page' => $display_count,
'offset' => $offset,
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<h3>Bisherige Artikel</h3>
<div class="recentpostsmasonry">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div <?php post_class('halb'); ?> id="post-<?php the_ID(); ?>">
<span class="head"><?php the_time('j. F Y') ?></span>
<h3><?php the_title(); ?></h3>
<div class="entry">
<?php the_excerpt(); ?>
<p class="more-link">Weiterlesen ...</p>
<p class="comments"><?php edit_post_link(__('Bearbeiten'), '', ' | '); ?> <?php comments_popup_link(__('Schreib den ersten Kommentar'), __('Ein Kommentar'), __('% Kommentare'), '', __('') ); ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="navigation">
<span class="nextlink" title="Ältere Einträge"><?php next_posts_link( '«', 0 ); ?></span>
<span class="previouslink" title="Jüngere Einträge"><?php previous_posts_link( '»' ); ?></span>
</div>
<?php else : ?>
<h3><?php _e('Nichts gefunden :('); ?></h3>
<?php endif; ?>
</div>
I am trying out this code now (14.10.17), but it doesn't add functioning page navigation...:
<?php get_header(); ?>
<div class="box blog-block">
<?php
$args = array( 'numberposts' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<span class="head"><?php the_time('j. F Y') ?></span>
<h3><?php the_title(); ?></h3>
<div class="entry">
<?php global $more;
$more = 1;
the_content();
?>
<p class="author"><?php the_author(); ?></p>
<p><a class="more-link" href="<?php the_permalink(); ?>#respond" title="<?php the_title();?>">Kommentieren</a>
</div>
<?php endforeach;
wp_reset_postdata(); /* 1. reset post data */
?>
</div>
<div class="box" id="recentposts">
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$display_count = 12;
$excluded_count = 1;
$offset = ( ($paged-1) * $display_count ) + $excluded_count;
$args = array(
'post_status'=>"publish",
'post_type'=>"post",
'orderby'=>"post_date",
'posts_per_page' => $display_count,
'offset' => $offset,
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<h3>Bisherige Artikel</h3>
<div class="recentpostsmasonry">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div <?php post_class('halb'); ?> id="post-<?php the_ID(); ?>">
<span class="head"><?php the_time('j. F Y') ?></span>
<h3><?php the_title(); ?></h3>
<div class="entry">
<?php the_excerpt(); ?>
<p class="more-link">Weiterlesen ...</p>
<p class="comments"><?php edit_post_link(__('Bearbeiten'), '', ' | '); ?> <?php comments_popup_link(__('Schreib den ersten Kommentar'), __('Ein Kommentar'), __('% Kommentare'), '', __('') ); ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="navigation">
<span class="nextlink" title="Ältere Einträge"><?php next_posts_link( '«', 0 ); ?></span>
<span class="previouslink" title="Jüngere Einträge"><?php previous_posts_link( '»' ); ?></span>
</div>
<?php else : ?>
<h3><?php _e('Nichts gefunden :('); ?></h3>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
New:
<?php get_header(); ?>
<div class="box blog-block">
<?php
$args = array( 'numberposts' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<span class="head"><?php the_time('j. F Y') ?></span>
<h3><?php the_title(); ?></h3>
<div class="entry">
<?php global $more;
$more = 1;
the_content();
?>
<p class="author"><?php the_author(); ?></p>
<p><a class="more-link" href="<?php the_permalink(); ?>#respond" title="<?php the_title();?>">Kommentieren</a>
</div>
<?php endforeach;
wp_reset_postdata(); /* 1. reset post data */
?>
</div>
<div class="box" id="recentposts">
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$display_count = 12;
$excluded_count = 1;
$offset = ( ($paged-1) * $display_count ) + $excluded_count;
$args = array(
'post_status'=>"publish",
'post_type'=>"post",
'orderby'=>"post_date",
'posts_per_page' => $display_count,
'offset' => $offset,
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<h3>Bisherige Artikel</h3>
<div class="recentpostsmasonry">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div <?php post_class('halb'); ?> id="post-<?php the_ID(); ?>">
<span class="head"><?php the_time('j. F Y') ?></span>
<h3><?php the_title(); ?></h3>
<div class="entry">
<?php the_excerpt(); ?>
<p class="more-link">Weiterlesen ...</p>
<p class="comments"><?php edit_post_link(__('Bearbeiten'), '', ' | '); ?> <?php comments_popup_link(__('Schreib den ersten Kommentar'), __('Ein Kommentar'), __('% Kommentare'), '', __('') ); ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="navigation">
<span class="nextlink" title="Ältere Einträge"><?php next_posts_link( '«', $the_query->max_num_pages ); ?></span>
<span class="previouslink" title="Jüngere Einträge"><?php previous_posts_link( '»' ); ?></span>
</div>
<?php else : ?>
<h3><?php _e('Nichts gefunden :('); ?></h3>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You haven't set up paging, so your query always thinks its on page 1.
Also according to the WP Developer Resources , you shouldn't use query_posts- use WP_Query or pre_get_posts instead, so I'm using WP_Query below to do your 2nd query.
Note that I've assumed you are using this on a static homepage, but I've added the change required for a custom page in comments.
<?php
$args = array( 'numberposts' => 1,
'post_status'=>"publish",
'post_type'=>"post",
'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
// regular post html with wp tags
<?php endforeach;
wp_reset_postdata(); /* 1. reset post data */
?>
<?php
/* Set up your pagination - $paged will contain the current page, telling WP_Query which post to start with (e.g. #13 on page 2) */
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
/* NOTE: if this is NOT on a static page
get the 'paged' query var instead of 'page', i.e.:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
*/
$display_count = 12; /* variable for the number of posts per page */
$excluded_count = 1; /* the number of posts excluded from the start position */
/* Calculate the offset, i.e. the post number to start the page with
Normally this is calculated by: ($paged-1) * $display_count
so we just need to add 1 for the one we excluded */
$offset = ( ($paged-1) * $display_count ) + $excluded_count;
/* set up your new query passing in the page so WP_Query knows what to return */
$args = array(
'post_status'=>"publish",
'post_type'=>"post",
'orderby'=>"post_date",
'posts_per_page' => $display_count,
'offset' => $offset,
'paged' => $paged
);
/* set up your new query */
$the_query = new WP_Query( $args );
<?php if ( $the_query->have_posts() ) : ?>
<h3>Headline</h3>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// regular html with wp tags for these posts (teasers, with the_excerpt)
<?php endwhile; ?>
<div class="navigation">
<span class="nextlink"><?php next_posts_link( 'Older', $the_query->max_num_pages ); ?></span>
<span class="previouslink"><?php previous_posts_link( 'Newer' ); ?></span>
</div>
<?php else : ?>
<h3><?php _e('Nothing found'); ?></h3>
<?php endif; ?>
The above code is untested, but the basic logic should be right.
UPDATE:
next_posts_link and
previous_posts_link use the global variable $wp_query by default, so to make is use your custom query, pass $custom_query->max_num_pages into next_posts_link, e.g.:
<span class="nextlink"><?php next_posts_link( 'Older', $the_query->max_num_pages ); ?></span>
<span class="previouslink"><?php previous_posts_link( 'Newer' ); ?></span>

woocommerce products by category repeated first 10 products on each page so how to display all product?

I created custom code for woocommerce products show as per selected category, first 10 product display as per the category but on the second page of pagination same 10 products are displayed, also at all page the same product are display, when products are order by rand then result is correct but i want result by title so please give me the solution for that my code is as below.
<ul class="products">
<?php $cat_slug=get_queried_object()->slug; ?>
<?php
$args = array( 'post_type' => 'product', 'product_cat' => $cat_slug, 'orderby' => 'title' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product_cust ">
<div class="woo_100">
<div id="woo_c">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<div class="woo_thumb">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
</div>
</a>
</div>
<div id="woo_d">
<div class="woo_desc">
<div class="nil_desc">
<a href="<?php echo get_permalink( $loop1->post->ID ) ?>" title="<?php echo esc_attr($loop1->post->post_title ? $loop1->post->post_title : $loop1->post->ID); ?>">
<h3><?php the_title(); ?></h3>
</a>
<span class="price"><?php echo $product->get_price_html(); ?></span><br>
</div>
<div class="nil_cart"><?php woocommerce_template_loop_add_to_cart( $loop1->post, $product ); ?>
</div>
</div>
</div>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
You have not passed any variable to paginate it.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'product_cat' => $cat_slug, 'orderby' => 'title', 'paged'=>$paged);
Also after your loop use nav.php to get pagination control links.

All posts on page of one category

I really can't figure this out. I'm trying to have posts all posts on a page from one category, but I still get all categories. This is the code I'm using now. I thought I could manage with WP_Query( array( 'posts_per_page' => -1, 'category_name' => 'resep' ) );, but it totally drives me nuts.
<?php /* Template Name: Blog */ ?>
<?php get_header(); ?>
<div id="content-wrap">
<div id="content">
<div class="post_content">
<h1 class="archive_title"><?php the_title(); ?></h1>
<?php
$query['post_type'] = 'post';
// WP 3.0 PAGED BUG FIX
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query['paged'] = $paged;
$recipe_posts = new WP_Query( array( 'posts_per_page' => -1, 'category_name' => 'resep' ) );
while ( $recipe_posts->have_posts() ) { $recipe_posts->the_post(); }
query_posts($query);
if (have_posts()) : ?>
<?php $more = 0; ?>
<div class="posts">
<?php while (have_posts()) : the_post();
$is_recipe = in_category('Resep'); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>
<?php if (option::get('index_thumb') == 'on') {
get_the_image( array( 'size' => 'loop', 'width' => option::get('thumb_width'), 'height' => option::get('thumb_height'), 'before' => '<div class="post-thumb">', 'after' => '</div>' ) );
} ?>
<div class="details">
<h2 class="title"><?php the_title(); ?></h2>
<?php if ( option::get('display_meta') == 'on' ) { ?>
<div class="meta">
<?php
if ( $is_recipe ) {
$fields = get_fields();
if ( !empty( $fields ) ) echo $fields;
} else { ?>
<p><strong><img src="<?php echo get_template_directory_uri() . '/images/person.png'; ?>" /><?php _e('Author', 'wpzoom'); ?>:</strong><?php the_author_posts_link(); ?></p>
<p><strong><img src="<?php echo get_template_directory_uri() . '/images/clock.png'; ?>" />
<?php _e('Posted', 'wpzoom'); ?>
:</strong> <?php echo get_the_date(); ?></p>
<?php } ?>
</div>
<?php } ?>
<div class="entry">
<?php the_content('<span>'.__('Read More', 'wpzoom').' ›</span>'); ?>
</div>
<p>
<?php if ( option::get('display_readmore') == 'on' && (option::get('display_content') == 'Excerpt') ) { ?>
<a href="<?php the_permalink(); ?>" class=" clean more-link">
<?php _e( ( $is_recipe ? 'Lihat Resep' : 'Read More' ), 'wpzoom' ); ?>
</a>
<?php } ?>
<?php edit_post_link( __('Edit', 'wpzoom'), ' <small>', '</small>' ); ?>
</p>
</div>
<div class="cleaner"> </div>
</div>
<!-- /.post -->
<?php endwhile; ?>
</div>
<div class="cleaner"> </div>
<?php get_template_part( 'pagination' ); ?>
<?php wp_reset_query(); ?>
<div class="cleaner"> </div>
<?php endif; ?>
</div><!-- / .post_content -->
</div><!-- / #content -->
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
You're making it far too complex, just specificy the stuff you need inside the while loop:
$recipe_posts = new WP_Query( array( 'posts_per_page' => -1, 'category_name' => 'resep' ) );
while ( $recipe_posts->have_posts() ) {
$recipe_posts->the_post();
echo '<li>' . get_the_title() . '</li>';
}
wp_reset_query();
You have entered loop inside loop, try this following with cleaning your code:
$args = array('posts_per_page' => -1 , 'category_name' => 'resep');
$recipe_posts = new WP_Query($args);
if($recipe_posts->have_posts()) :
while($recipe_posts->have_posts()) :
$recipe_posts->the_post();
?>
<h1><?php the_title() ?></h1>
<div class='post-content'><?php the_content() ?></div>
<?php
endwhile;
else:
?>
Oops, there are no posts.
<?php
endif;
?>
I cleaned the whole template, but posts from other categories were still there. In the end I managed with these lines
global $wp_query;
$args = array_merge( $wp_query->query, array( 'category_name' => 'resep' ) );
query_posts( $args );
$recipe_posts = new WP_Query($args);
$more = 0;
if($recipe_posts->have_posts()) :
while ($recipe_posts->have_posts()) : $recipe_posts->the_post();?>

Resources