Posts pagination not working in front page - wordpress

This question has asked many time before and none has answers my question. I went through most of the answers before post this.
I have a pagination query in home page. Pagination is showing and I can going through the pages. But same posts are displaying for every paginate link.
This is my code so far. Can anybody elaborate the wrong here?
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type'=>'post',
'posts_per_page'=>10,
'orderby'=>'date',
'order'=>'DESC',
'offset'=>3,
'paged'=>$paged
);
$blogs = new WP_Query($args);
if($blogs->have_posts()):
while($blogs->have_posts()): $blogs->the_post();
?>
<div class="col-md-6">
<div class="single-post">
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url('blog-thumbnail'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
</a>
</div>
<div class="content">
<div class="category">
<?php
$categories = get_the_category();
foreach ($categories as $key => $category) {
echo ''. $category->name. '';
}
?>
</div>
<h3><?php the_title(); ?></h3>
<div class="metadata">
<span class="date"><?php echo get_the_date(); ?></span>
<span class="author"><?php echo get_the_author(); ?></span>
</div>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
echo '<div class="paginate-wrap">'. paginate_links() . '</div>';
endif;
wp_reset_query();
?>
Thanks in advance

Found the solution. Is anybody facing the same please check this.
My code is right, but because of the offset parameter pagination is not working. I found the solution here.
So, this is my final code:
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$per_page = 10;
$defualt_offset = $offset;
if ($paged == 1) {
$offset = $default_offset;
} else {
$offset = (($paged - 1) * $per_page) + $default_offset;
}
$args = array(
'post_type'=>'post',
'posts_per_page'=>$per_page,
'orderby'=>'date',
'order'=>'DESC',
'offset'=>3,
'paged'=>$paged
);
$blogs = new WP_Query($args);
if($blogs->have_posts()):
while($blogs->have_posts()): $blogs->the_post();
?>
<div class="col-md-6">
<div class="single-post">
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url('blog-thumbnail'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
</a>
</div>
<div class="content">
<div class="category">
<?php
$categories = get_the_category();
foreach ($categories as $key => $category) {
echo ''. $category->name. '';
}
?>
</div>
<h3><?php the_title(); ?></h3>
<div class="metadata">
<span class="date"><?php echo get_the_date(); ?></span>
<span class="author"><?php echo get_the_author(); ?></span>
</div>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
echo '<div class="paginate-wrap">'. paginate_links() . '</div>';
endif;
wp_reset_query();
?>

Related

Wordpress showing featured post card below each post, need to show featured on the featured page

I am working on a website that has a section below the blog post that shows a marked featured post, the issue I am having is that on the featured page the card section is showing the 1st post on the post list instead of its own featured post.
This is setup in a component-card.php
<?php
$post_id = (get_query_var('post_id')) ? get_query_var('post_id') : $post->ID;
$type = get_query_var('type');
$pages = get_query_var('pages');
$col = get_query_var('col');
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'single-post-thumbnail');
$show_featured = get_field("featured_story", $post_id);
$link = get_the_permalink($post_id);
$title = get_the_title($post_id);
$excerpt = get_the_excerpt($post_id);
$count = 6;
$switch = '';
if ($col === '12') {
$count = 12;
$switch = 'switch';
}
?>
<?php if ($type !== 'page'): ?>
<?php if ($show_featured): ?>
<article class="component-article-cards featured">
<div class="row align-middle align-center">
<div class="col-12 col-md-<?php echo $count ?> <?php echo $switch ?>">
<div class="article-inner">
<h3><?php echo $title; ?></h3>
<hr />
<?php echo $excerpt; ?>
Read full article
</div>
</div>
<div class="col-12 col-md-<?php echo $count ?>">
<?php if ($image) : ?>
<img src="<?php echo $image[0]; ?>">
<?php endif; ?>
</div>
</div>
</article>
<?php else: ?>
<article class="component-article-cards">
<?php if ($image) : ?>
<img src="<?php echo $image[0]; ?>">
<?php endif; ?>
<div class="article-inner">
<h3><?php echo $title; ?></h3>
<hr />
<?php echo $excerpt; ?>
Read full article
</div>
</article>
<?php endif; ?>
<?php endif; ?>
Any help would be gratefully appreciated

Recent posts with thumbnail, title, date and category - Wordpress

in Wordpress, I am trying to display my most recent posts in a list. I have being able to list the links, but cant do the same with post thumbnail, title, date and category. What am I doing wrong?
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo the_date();?></p>
<h3 class="article_title"><?php echo get_the_title( $post_id ); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Another way to do it, using WP_Query
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat-a, cat-b'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php echo the_category(); ?></p>
<h3 class="article_title"><?php echo get_the_title(); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>
Try below code its working for me.
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo get_the_category( $recent->ID );?></p>
<h3 class="article_title"><?php echo get_the_title( $recent->ID); ?></h3>
<p class="date"><?php echo get_the_date( $recent->ID );?></p>
</div>
<div class="mask">
<img src="<?php echo get_the_post_thumbnail_url($recent->ID,'full'); ?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>

Query related posts, Wrap in Row every 3 posts

I'm trying to query some related posts and wrap them in a row div every 3 posts. I've checked all the questions regarding this but still my query renders a mess.
This is what I have so far:
<?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 6,
'post__not_in' => array($post->ID) ) );
$counter=0;
if( $related ) foreach( $related as $post ) {
setup_postdata($post); $counter++;
?>
<div class="row">
<article class="third-width">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<?php the_post_thumbnail('post-parrilla'); ?>
</a>
<?php exclude_post_categories("8"); ?>
<div class="clear"></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<div class="post-title">
<span><?php the_title(); ?></span>
</div>
</a>
</article>
<?php if ($counter%3==0):?>
</div><div class="row">
<?php endif;?>
<?php } wp_reset_postdata(); ?>
Thank you in advance
Use below code
<?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 6,
'post__not_in' => array($post->ID) ) );
if( $related )
{
$counter = 0;
?>
<div class="row">
<?php
foreach( $related as $post ) {
setup_postdata($post);
if ($counter%3==0){
?>
</div><div class="row">
<?php
}
?>
<article class="third-width">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<?php the_post_thumbnail('post-parrilla'); ?>
</a>
<?php exclude_post_categories("8"); ?>
<div class="clear"></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<div class="post-title">
<span><?php the_title(); ?></span>
</div>
</a>
</article>
<?php
$counter++;
}
?>
</div>
<?php
}
wp_reset_postdata();
?>

Custom post type pagination not working

I know this has been asked a thousand times, and I have tried to replicate almost all the solutions I found either here, or into the Wordpress forums (where I posted a question too) but nothing seems to solve my problem.
I have created a theme from scratch, I downloaded a blank theme template from _underscores and created everything else from there.
The final goal would be to create an infinite scroll, but I cannot even make pagination work. If anyone could help me, it would be greatly appreciated.
This is the code I have:
<?php
/*
Template Name: zvideos
*/
/**
* #package zseventyfour
*/
get_header(); ?>
<?php
$temp = $zvideos;
$zvideos = null;
$zvideos = new WP_Query();
$zvideos ->query( 'showposts=2&post_type=zvideo' . '&paged=' . $paged );
if( $zvideos->have_posts() ) {
while( $zvideos->have_posts() ) {
$zvideos->the_post();
$zvideo_date = get_post_meta($post->ID, 'zvideo_date', true);
$zvideo_client = get_post_meta($post->ID, 'zvideo_client', true);
$zvideo_location = get_post_meta($post->ID, 'zvideo_location', true);
$zvideo_vimeo = get_post_meta($post->ID, 'zvideo_vimeo', true);
$zvideo_screenshot01 = get_post_meta($post->ID, 'zvideo_screenshot01');
$zvideo_job01 = get_post_meta($post->ID, 'zvideo_job01', true);
$zvideo_job02 = get_post_meta($post->ID, 'zvideo_job02', true);
$zvideo_job03 = get_post_meta($post->ID, 'zvideo_job03', true);
$zvideo_name01 = get_post_meta($post->ID, 'zvideo_name01', true);
$zvideo_name02 = get_post_meta($post->ID, 'zvideo_name02', true);
$zvideo_name03 = get_post_meta($post->ID, 'zvideo_name03', true);
?>
<section id="content">
<div class="wrapper">
<article>
<div class="vimeo">
<h1><?php the_title(); ?></h1>
<ul class="details">
<li><?php echo $zvideo_date?></li>
<li><?php echo $zvideo_client?></li>
<li><?php echo $zvideo_location?></li>
</ul>
<div class="vimeo-wrapper">
<iframe src="//player.vimeo.com/video/<?php echo $zvideo_vimeo?>" width="100%" height="100%" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
</div>
<div class="footage">
<ul class="images">
<li><img src="<?php echo $zvideo_screenshot01[0]?>" alt="<?php the_title(); ?>_01"/></li>
<li><img src="<?php echo $zvideo_screenshot01[1]?>" alt="<?php the_title(); ?>_02"/></li>
<li><img src="<?php echo $zvideo_screenshot01[2]?>" alt="<?php the_title(); ?>_03"/></li>
<li><img src="<?php echo $zvideo_screenshot01[3]?>" alt="<?php the_title(); ?>_04"/></li>
<li><img src="<?php echo $zvideo_screenshot01[4]?>" alt="<?php the_title(); ?>_05"/></li>
<li><img src="<?php echo $zvideo_screenshot01[5]?>" alt="<?php the_title(); ?>_06"/></li>
</ul>
</div>
<div class="clearfix"></div>
<div class="info">
<ul class="credits">
<li><p class="title"><?php echo $zvideo_job01?></p><p class="name"><?php echo $zvideo_name01?></p></li>
<li><p class="title"><?php echo $zvideo_job02?></p><p class="name"><?php echo $zvideo_name02?></p></li>
<li><p class="title"><?php echo $zvideo_job03?></p><p class="name"><?php echo $zvideo_name03?></p></li>
<li><p class="title"><?php echo $zvideo_job?></p><p class="name"><?php echo $zvideo_name?></p></li>
<li><p class="title"><?php echo $zvideo_job?></p><p class="name"><?php echo $zvideo_name?></p></li>
<li><p class="title"><?php echo $zvideo_job?></p><p class="name"><?php echo $zvideo_name?></p></li>
</ul>
</div>
</article>
</div><!-- END O WRAPPER -->
</section>
<?php
}
}
else {
echo '<div style="width=100%;text-align:center;font-size:180%;">Ooops! Something went <strong>terribly</strong> wrong...</br> Or there are no videos to show. Who knows, this is obscure stuff.</div>';
}
?>
<section>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
</section>
<?php get_footer(); ?>
I don't know how to solve this problem.
Thanks in advance.
Edit: problem fixed with the below code -
<?php
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page'); // Display posts from current page on a static front page
} else {
$paged = 1;
}
$args = array(
'paged' => $paged,
'posts_per_page' => 1,
'post_type' => 'zvideos'
);
query_posts($args);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<nav>
<?php previous_posts_link(); ?>
<?php next_posts_link(); ?>
</nav>
<?php endif; ?>
<?php wp_reset_query(); ?>
Try this:
<?php echo paginate_links(array('base' => str_replace(999999999,'%#%', esc_url(get_pagenum_link(999999999))), 'format'=>'?paged=%#%', 'current'=>max(1, get_query_var('paged')), 'total' => $wp_query->max_num_pages, 'type' => 'list')); ?>
Put this code where you want to display the navigation bar.

Wordpress category template is showing posts from all categories instead of specific category posts

I have an archive page that's set up to display category specific posts. However, instead of showing only the posts from a given category on the category page, it's showing all posts. For an example, see here.
Here's the code I'm using on my archive.php page. I know it's improper use of the loop, but I'm not sure how to fix it. Thanks for the help.
<?php get_header(); ?>
<div id="content">
<div id="inner-content" class="wrap clearfix">
<h1 class="blogTitle" style="margin:10px 0 3px 0;">Blog Title</h1>
<nav class="blogNav" role="navigation">
<?php bones_blog_nav(); // Adjust using Menus in Wordpress Admin ?>
</nav>
<div id="main" class="eightcol first clearfix" role="main">
<div class="catwhiteBg">
<?php if (is_category()) { ?>
<h1 class="catTitle">
<span><?php _e("", "bonestheme"); ?></span> <?php single_cat_title(); ?>
</h1>
<?php echo category_description( $category_id ); ?>
<?php } elseif (is_author()) { ?>
<div class="authorTop">
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
<div class="author-pic"><?php echo get_avatar( $curauth->user_email, '80' ); ?></div>
<div class="author-name"><span style="font-weight: 200; color: #575757;">POSTS BY:</span>
<?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?>
</div>
<div class="author-bio"><?php echo $curauth->description; ?></div>
<div class="author-twitter"><span>twitter</span></div>
</div>
<?php } elseif (is_day()) { ?>
<h1 class="archive-title h2">
<span><?php _e("Daily Archives:", "bonestheme"); ?></span> <?php the_time('l, F j, Y'); ?>
</h1>
<?php } elseif (is_month()) { ?>
<h1 class="archive-title h2">
<span><?php _e("Monthly Archives:", "bonestheme"); ?></span> <?php the_time('F Y'); ?>
</h1>
<?php } elseif (is_year()) { ?>
<h1 class="archive-title h2">
<span><?php _e("Yearly Archives:", "bonestheme"); ?></span> <?php the_time('Y'); ?>
</h1>
<?php } ?>
</div>
<div class="psts">
<?php
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
query_posts(array('posts_per_page' => '8','paged'=>$paged));
?>
<?php
$count = 1;
while (have_posts()) : the_post(); ?>
<div class="sixcol small pst<?php if ((isset($count)) && ($count % 2 == 0 )) { echo ' last';} // same logic to add class of last to last item in row of two ?>" id="post-<?php the_ID(); ?>">
<article id="post-<?php the_ID(); ?>" role="article">
<div class="thumb-wrapper mobile">
<?php if(has_post_thumbnail()) { $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'post-thumb' ); echo '<img src="' . $image_src[0] . '" width="100%" class="post-thumb" />'; } ?>
<header class="post-thumb-header">
<h2 class="post-title"><?php the_title(); ?></h2>
</header> <!-- end article header -->
<p class="meta"><?php the_category(', '); ?></p>
</div>
<section class="mobile-content">
<?php the_excerpt(); ?>
</section>
</article> <!-- end article -->
</div>
<?php $count++; ?>
<?php endwhile; ?>
<nav class="wp-prev-next">
<?php echo rb_pagination(); ?>
</nav>
</div> <!-- end .psts -->
</div> <!-- end #main -->
<?php get_sidebar(); // sidebar 1 ?>
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
<?php get_footer(); ?>
I think you're overriding the original query with this line:
query_posts(array('posts_per_page' => '8','paged'=>$paged));
Can you swap this line with this, and let me know the results?
global $query_string;
query_posts( $query_string . "&posts_per_page=8&paged=$paged" );
if that fails try this:
query_posts(array('posts_per_page' => '8','paged'=>$paged, 'cat' => $category_id ));

Resources