Wordpress CPT pagination returns 404 page - wordpress

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>

Related

Pagination in Wordpress after the 5 latest posts

How do I add pagination to this code? It currently shows latest articles.
Pagination can be after every 3 or 5 or 10 posts.
<?php
$the_query = new WP_Query( array(
'category_name' => $category_name,
'offset' => 1, //ignore the first 4 posts and show remaining
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="home__full">
<div class="row">
<div class="col-md-3">
<small><?php the_time('F jS, Y') ?> • <?php echo reading_time(); ?> • <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></small>
</div>
<div class="col-md-6">
<h3><?php echo substr(get_the_title(),0,95); ?></h3>
<div class="home__latest--excerpt"><p><?php echo substr(get_the_excerpt(),0,140); ?></p></div>
</div>
<div class="col-md-3">
<a href="<?php the_permalink() ?>">
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/blog-thumbnail.png" alt="<?php echo the_title(); ?>" class="wp-post-image" />
<?php } ?>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>

Pagination not working on the Category Template

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

Displaying short product description of woocommerce product via ACF Repeater field

My code is below
It’s essentially bringing out a woocommerce product as a post object inside a repeater field.
It’s pretty much working fine with two exceptions.
It’s giving an error for using get_attribute for the $scent variable
The $description variable should pull the Product Short Description, but its not bringing anything back.
Any help would be very much appreciated!
<?php if ( have_rows( 'products' ) ) : ?>
<?php while ( have_rows( 'products' ) ) : the_row(); ?>
<?php $post_object = get_sub_field( 'product' ); ?>
<?php if ( $post_object ): ?>
<?php $post = $post_object; ?>
<?php setup_postdata( $post );
global $post;
$logoImageHtml = '';
$subCollectionHtml = '';
$imageAlt = '';
$hoverImage = '';
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'parent',
'order' => 'ASC',
'menu_order' => 'ASC',
'pad_counts' => 0,
'hierarchical' => 1,
'hide_empty' => 1
);
$terms = wc_get_product_terms(get_the_ID(), 'product_cat', $args);
if (!empty($terms) && is_array($terms)) {
$terms = array_reverse($terms);
$term = (!empty($terms[1]) ? $terms[1] : null);
if (!empty($term->name)) {
$imageAlt = $term->name;
}
$term = (!empty($terms[2]) ? $terms[2] : null);
if (!empty($term->name)) {
$subCollectionHtml = "{$term->name}
";
}
}
$scent = $post->get_attribute('scent');
$scent = (!empty($scent) ? explode(', ', $scent) : null);
$description = get_short_description ( $post->post->ID );
// $description = $post->post->post_excerpt;
$thumbImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->post->ID ), 'shop_catalog' );
if (get_field('product_logo_image')) {
$logoImage = get_field('product_logo_image');
if (!empty($logoImage)) {
$logoImageHtml = "<h3><img class='product-logo' src='$logoImage' alt='$imageAlt'/></h3>";
}
}
if (get_field('product_grid_scent_image')) {
$hoverImage = get_field('product_grid_scent_image');
}
?>
<li class="product">
<div class="front">
<div class="image">
<?php if (!empty($thumbImage)): ?>
<img src="<?php echo $thumbImage[0]; ?>" data-id="<?php echo $post->post->ID; ?>">
<?php endif; ?>
</div>
<?php if ($logoImageHtml || $subCollectionHtml || $scent): ?>
<div class="text">
<?php echo $logoImageHtml; ?>
<?php echo $subCollectionHtml; ?>
<?php
if (!empty($scent) && is_array($scent)): ?>
<div class="scent">
<ul>
<?php foreach ($scent as $key => $value): ?>
<li><?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<div class="back">
<div class="frame">
<div class="content">
<?php if (!empty($hoverImage)): ?>
<div class="image">
<img src="<?php echo $hoverImage; ?>" alt="Scent"/>
</div>
<?php endif; ?>
<div class="text">
<?php
if (!empty($description)): ?>
<?php echo $description; ?>ssss
<?php endif; ?>
View details
</div>
</div>
</div>
</div>
</li>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>

pagination within wp_query / wordpress

I have written this code for a custom loop in Wordpress, but I cannot seem to get pagination working.
<?php //Template Name: Acapellas ?>
<?php get_header(); ?>
<?php if (is_user_logged_in()) { ?>
<div id="main-content">
<div class="container">
<div class="row">
<div class="col-md-3">
<?php get_sidebar('primary-left'); ?>
</div>
<div class="col-md-9">
<div class="post-<?php the_ID(); ?> post-content">
<h1>Acapellas</h1>
<?php $loop = new WP_Query( array( 'post_type' => 'acapella', 'posts_per_page' => 100 ) ); ?>
<ul class="acapellas row">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="post-<?php the_ID(); ?> col-md-6">
<div class="wrap">
<h2><?php the_title() ?></h2>
<?php the_content(); ?>
<span class="download left"><?php get_attachment_icons($echo=true); ?></span>
<span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>
<?php
global $post;
$post_type = get_post_type(get_the_ID());
$post_type_taxonomies = get_object_taxonomies($post_type);
if (!empty($post_type_taxonomies)) {
echo '<ul class="details">';
foreach ($post_type_taxonomies as $taxonomy) {
$terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');
if ($terms) {
echo '<li>' . $terms . '</li>';
}
}
echo '</ul>';
}
?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php
} else {
wp_redirect( 'http://www.voclr.it/signup' ); exit;
}
?>
<?php get_footer(); ?>
Move your $paged variable outside the loop and include it as a parameter of WP_Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 100,
'paged' => $paged,
));

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