I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
Here is the code I'm using, simplified of course:
How to do pagination for this?
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; endif; ?>
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
First of all, you must setup properly your query:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
And second, you need to add pagination links out of the loop
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
Whole code can looks like this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
Problem has been solved:
<?php
global $paged, $wp_query, $wp;
$args1 = array('cat' => '3, 7, 10, 12');
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=3&cat=3,7,10,12');
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
Related
I created custom post type and child post , i want to add pagination to child post , I've had a basic attempt at this using $paged = (get_query_var('page')) ? get_query_var('page') : 1; and the paged argument in the WP_Query, but my next_posts_link doesn't work.
my code to show child post
<?php
$args = array(
'post_type' => 'programs',
'posts_per_page' => 60,
'post_parent' => $post->ID,
'order' => 'ASC',
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="penci-border-arrow penci-homepage-title penci-magazine-title style-5 pcalign-right pciconp-right pcicon-right" style="border-color: #3E68B0">
<h3 class="inner-arrow" style="background:#3E68B0;border-color: #3E68B0">
الحلقات </h3>
</div>
<ul class="penci-wrapper-data penci-grid">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
$episode_number = get_field('episode_number', get_the_ID());
?>
<li class="list-post pclist-layout">
<article id="post-<?php the_ID(); ?>" class="item hentry">
<div class="thumbnail child-thumbnail">
<a class="penci-image-holder penci-lazy" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" style="background-image: url(<?php echo $featured_img_url?>);">
</a>
</div>
<div class="content-list-right content-list-center child-content">
<div class="header-list-style">
<div class="episode-details">
<span class=""> <a class="url fn n" href="<?php the_permalink(); ?>"><?php echo $episode_number ?></a></span>
<span style="margin:0 20px"><a>|</a></span>
<span class="featc-date"><time class="entry-date published" datetime="2021-10-31T13:26:29+00:00">2021-10-31</time>
</span>
<span class='program-viewcn'><?php pvc_stats_update( get_the_ID(), 1 ); ?></span>
</div>
<h2 class="grid-title entry-title">
<?php the_title(); ?>
</h2>
</div>
<div class="item-content entry-content">
<p>
<?php the_excerpt();?>
</p>
</div>
</div>
</article>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
I have coded as below for Recent post by same author. Code is working fine and giving me recent post by same author, but it show more than one post (but I need 1 post to show)
<div class="popular-post-wrapper">
<h4 class="blog-post-sub-title">Most Recent</h4>
<div class="col-md-3 popular-post-thumbnail">
<?php
$query_posts=array(
'orderby' => 'display_name',
'order' => 'ASC',
'who' => 'authors',
'posts_per_page' => '1' );
if (have_posts()) : while (have_posts()) : the_post();
?>
<?php //if ( has_post_thumbnail() ) : ?>
<a href="<?php //the_permalink(); ?>" title="<?php //the_title_attribute(); ?>">
<?php //the_post_thumbnail(); ?>
</a>
<?php //endif; ?>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="https://kubalubra.is/wp-content/uploads/2017/11/default-thumbnail.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<div class="col-md-9 post-title-wrapper">
<h3> <a href="<?php the_permalink(); ?>">
<?php the_title();?>
</a></h3>
</div>
<?php
endwhile; endif;
wp_reset_query();
?>
</div>
I am trying to paginate archived post in WordPress when I click "next button" but it doesn't work. Please guide me to do it in a simple way. My code is given below:
<div class="grid-wrapper ">
<?php $args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner-blog">
<div class="row">
<div class="col-lg-4 col-md-2">
<div class="post-thumbnail">
<img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
</div>
</div>
<div class="col-lg-8 col-md-9">
<h2 class="post-title"><?php the_title();?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php wp_reset_postdata(); ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?> »</li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
</div>
</div>
I am trying to have the layout of my posts on my search results be 2 posts per line. Right now there is 1 post, so I am trying to add in 'col-sm-6 col-md-6' to my search.php file. However, every time I try to add it in nothing changes. Does anyone have any solutions?
search.php
<?php get_header(); ?>
<div id="page">
<div class="column span-9 first" id="maincontent">
<div class="content">
<?php if (have_posts()) : ?>
<form role="search" method="get" class="search-form-search form-inline-search" action="">
<div class="input-group-search">
<input type="search" value="" name="s" class="input-sm-search search-field-search form-control-search" placeholder="<?php echo $s ?>">
</div>
</form>
<div class="clear"></div>
<?php while (have_posts()) : the_post(); ?>
<div class="search-post" id="post-<?php the_ID(); ?>">
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?>
<div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<div class="front-page-post-info">
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<h2 class="center">No posts found. Try a different search?</h2>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
<?php endif; ?>
</div> <!-- /content -->
</div> <!-- /maincontent-->
</div> <!-- /page -->
<?php get_footer(); ?>
Here is another version of a search.php I have been playing around with. However, with this one the web page doesn't show up.
<?php
get_header();
$the_query = new WP_Query( [
'posts_per_page' => 15,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<div id="ajax">
<?php
while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<div class="row">
<article <?php post_class( 'col-sm-6 col-md-6' ); ?>>
<div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
</div>
<div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<div class="front-page-post-info">
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</div>
</article>
</div>
<?php if(get_query_var('paged') < $the_query->max_num_pages) {
}
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();
hi I have a problem to put featured post without pagination and featured post with pagination in one page:
Here is the code the first one I use this:
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
if (have_posts()) : while (have_posts()) : the_post();
$postImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<div class="slider" style="background:url(<?php echo $postImage[0];?>) no-repeat center center">
<div class="slider-shadowbox"></div>
<a class="slider-hyperlink" href="<?php the_permalink(); ?>">
<div class="slider-title">
<div class="featured-post-author">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '100'); ?>
<?php the_author_posts_link(); ?>
</div>
<span class="slider-tagline"><?php the_title()?></span>
<em class="slider-author">By <?php the_author_posts_link(); ?></em>
<em class="slider-date"><i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></em>
</div>
</a>
</div>
<?php
endwhile;
endif;
?>
/and the second one I use this to create featured post with pagination, but somehow the pagination shows the numbers but the hyperlink for each number doesn't work properly./
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="featured-post">
<div class="zitem">
<?php the_post_thumbnail( array(360, 240));?>
</div>
<span class="featured-post-summary">
<h3><?php the_title(); ?></h3>
<?php echo get_excerpt(); ?>
</span>
<div class="featured-post-author-wrap">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '35'); ?>
<span class="featured-post-author">By <?php the_author_posts_link(); ?></span>
<span class="featured-post-date"><?php the_time('F j, Y'); ?><span>
</div>
</div>
<?php endwhile; ?>
<div class="featured-post-pagination pagination">
<div class="pagination-outer"><?php my_pagination(); ?></div>
</div>
<?php else : ?>
<strong>Sorry no post available.</strong>
<?php endif; ?>