Pagination is not working in archive page in WordPress - wordpress

Every pagination link take me to the first page every time. I have tried with or without a plugin, but nothing works. It doesn't matter if I click on the Next button or the Prev button. It always takes me to the first page.
<?php
/*
Template Name: Notices & Circulars
*/
get_header(); ?>
<div class="banner-box">
<div class="wrap">
<div class="main-top">
<div class="main">
<h1><div class="titlepage"><?php the_title();?></div></h1>
<section class="content">
<?php
$args=array(
'cat'=> 14,
'posts_per_page' => 10,
'offset' => 5,
'paged' => get_query_var('page')
);
if ( have_posts() ) :
query_posts($args);
?>
<?php while(have_posts()):the_post();?>
<li style="list-style:none;">
<h3><font style="color:#666666;"><?php the_title(); ?></h3>
<?php
/***** Thumbnail ******/
the_post_thumbnail(
array(120, 90),
array(
'class' => 'thumbnail',
'alt' => 'post thumbnail',
'title' => 'my custom title'
)
);
/******* Thumbnail Ends ********/
the_content(__('Continue Reading'));?></font>
</li><hr /><?php
endwhile;
wp_pagenavi();
endif;
?>
</div>
</div>
</div>
<?php get_footer(); ?>
</div>

Please try this:
<?php
/*
Template Name: Notices & Circulars
*/
get_header(); ?>
<div class="banner-box">
<div class="wrap"><div class="main-top"><div class="main">
<h1><div class="titlepage"><?php the_title();?></div></h1>
<section class="content">
<?php
// Example for adding WP PageNavi to a new WP_Query call
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array('post_type' => 'post','cat'=> 14,'posts_per_page' => 10, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li style="list-style:none;">
<h3><font style="color:#666666;"><?php the_title(); ?></h3>
<?php
/***** Thumbnail ******/
the_post_thumbnail(
array(120, 90),
array(
'class' => 'thumbnail',
'alt' => 'post thumbnail',
'title' => 'my custom title'
)
);
/******* Thumbnail Ends ********/
the_content(__('Continue Reading'));?></font>
</li><hr />
<?php
endwhile; ?>
<?php wp_pagenavi( array( 'query' => $loop ) ); ?>
</div></div></div>
<?php get_footer();?></div>

Related

Pagenation in single agent page not working wordpress

I have created a single agent page in that page i am trying to list all the property by the agent in that property area i have kept an bootstrap pagination that was not working it was showing the page numbers but when you click that was not taking to page1 or page 2 etc. But it was redirecting to the same page. Here is my code.
get_header();
<div class="container-fluid"><div class="row bkg-white bkg-pd-top">
<div class="container">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<div class="col-md-9"><div class="row">
<div class="single-details">
<div class="single-sec-prop-title">Agent Details</div>
<div class="single-prop-detail">
<div class="col-md-4 col-sm-4 col-xs-4"><div class="row"><div class="agent-single-image">
<?php the_post_thumbnail('small-img-featured', array('class' => 'img-responsive')); ?>
</div></div></div>
</div>
</div></div>
</div></div>
<div class="single-details">
<div class="single-sec-prop-title">Description</div>
<div class="single-prop-detail">
<?php the_content(); ?>
</div>
</div>
<div class="col-xs-12"><div class="row">
<div class="single-sec-prop-title agent-single-title">Listed Properties</div>
<?php
endwhile;
endif;
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => array('forsale', 'forrent'),
'numberposts' => -1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => $paged, //very important
'meta_key' => 'select-agent',
'meta_value' => get_the_id(),
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
get_template_part( 'template-parts/property', 'agent' );
endwhile; ?>
<?php
if ($custom_query->max_num_pages > 1) :
$orig_query = $wp_query;
$wp_query = $custom_query;
?><nav class="prev-next-posts">
<?php
if (function_exists('wp_bootstrap_pagination')){
wp_bootstrap_pagination();
}
?>
</nav>
<?php endif;
wp_reset_postdata();
else:
get_template_part( 'template-parts/no', 'post' );
endif; //ends loop
?>
<?php
get_footer();
Here is the link for bootstrap pagenation code Bootstrap pagenatio
Here is a answer for your question. If you need to add an pagenation to your single page for example your single agent page you need to set a template redirect function in wordpress for that you need to add the following code to your active themes function.php In this code please change the agent to your custom post type if your custom post is not agent.
add_filter('redirect_canonical','redirect_single_page');
function redirect_single_page($redirect_url) {
if (is_singular('agent')) $redirect_url = false; // change 'agent' to your custom post type, obviously
return $redirect_url;
}
you can try this with wordpress.
<?php
get_header(); ?>
<div class="container-fluid"><div class="row bkg-white bkg-pd-top">
<div class="container">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<div class="col-md-9"><div class="row">
<div class="single-details">
<div class="single-sec-prop-title">Agent Details</div>
<div class="single-prop-detail">
<div class="col-md-4 col-sm-4 col-xs-4"><div class="row"><div class="agent-single-image">
<?php the_post_thumbnail('small-img-featured', array('class' => 'img-responsive')); ?>
</div></div></div>
</div>
</div></div>
</div></div>
<div class="single-details">
<div class="single-sec-prop-title">Description</div>
<div class="single-prop-detail">
<?php the_content(); ?>
</div>
</div>
<div class="col-xs-12"><div class="row">
<div class="single-sec-prop-title agent-single-title">Listed Properties</div>
<?php
endwhile;
endif;
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => array('forsale', 'forrent'),
'numberposts' => -1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => $paged, //very important
'meta_key' => 'select-agent',
'meta_value' => get_the_id(),
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
get_template_part( 'template-parts/property', 'agent' );
endwhile;
customPagination($paged,$custom_query);
endif;
wp_reset_postdata();
else:
get_template_part( 'template-parts/no', 'post' );
endif; //ends loop
?>
<?php
get_footer();
?>
<?php
function customPagination($paged,$probj){
$data = '';
$data .='<nav aria-label="Page navigation example">';
$pager = 999999999;
$pargs = array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $probj->max_num_pages,
'prev_next'=> false,
'type'=> 'array'
);
$links = paginate_links( $pargs );
if ( $links ) :
$data .= '<ul class="pagination justify-content-center">';
// get_previous_posts_link will return a string or void if no link is set.
if ( $prev_posts_link = get_previous_posts_link( __( 'Previous Page' ) ) ) :
$data .= '<li class="prev-list-item">';
$data .= $prev_posts_link;
$data .= '</li>';
endif;
$data .= '<li class="page-item">';
$data .= join( '</li><li class="page-item">', $links );
$data .= '</li>';
// get_next_posts_link will return a string or void if no link is set.
if ( $next_posts_link = get_next_posts_link( __( 'Next Page' ) ) ) :
$data .= '<li class="next-list-item">';
$data .= $next_posts_link;
$data .= '</li>';
endif;
$data .= '</ul>';
endif;
$data .='</nav>';
echo $data;
}
?>
I have added one function at your code please refere this.

Custom blog page in wordpress navigation Next and previous page not working

I have tried to create custom blog page with navigation. Everything works fine without navigation. I have tried fix the pagenation issue but it was not working below is the code for my custom page template.
<?php /*
Template Name: My Blog Page
*/
get_header();
?>
<div class="container-fluid"><div class="row bkg-grey"><div class="container"><div class="row">
<div class="col-md-8"><div class="row">
<?php
$args = array( 'post_type' => 'post');
if ( have_posts() ) :
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12"><div class="row lwp-posts">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-md-4"><div class="row">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('homepage-thumb',array('class' => 'img-responsive' ) );
} ?></div></div>
<div class="col-md-8"><div class="row">
<div class="rjs-post-title"><h2><?php the_title(); ?></h2></div>
<div class="rjs-post-meta"><span>Posted on :<?php the_time( 'j M Y' ); ?></span><span>Posted by : <?php the_author(); ?></span><span itemprop="interactionCount" content='UserComments: <?php comments_popup_link( '0', '1', '%' ); ?> '>Comments : <?php comments_popup_link( '0', '1', '%' ); ?></span><span>Category : <?php the_category(', '); ?></span></div>
<?php the_excerpt(); ?>
</div></div>
<?php edit_post_link(); ?>
<?php wp_link_pages(); ?>
</article></div></div>
<?php endwhile; ?>
<nav class="navigation index">
<div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
<div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
</nav>
</div></div>
<?php else : endif; ?>
<div class="col-md-4"><div class="row">
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
</div></div></div></div></div></div>
<?php get_footer(); ?>
You need to add some parameters to your query args:
$args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged
);
And if you want to customize navigation links, use this pattern:
<nav class="navigation index">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</nav>

Splitting WordPress Loops

I want to display a grid of thumbnails & post titles which are custom post types. I am also using fullpage.js which displays content full width & height of the browser window. Within each fullpage 'section', I want to show 6x thumbnails/titles.
How can I split the loop in order to achieve this effect? Here is my code so far:
<?php
$work_args = array(
'post_type' => 'bp_work_post_type',
'post_status' => 'publish',
'posts_per_page' => 6,
'offset' => 6
);
$work_query = new WP_Query( $work_args );
?>
<?php if ( $work_query->have_posts() ) : ?>
<div class="section">
<?php while ( $work_query->have_posts() ) : $work_query->the_post(); ?>
<div class="post-grid">
//Grid Content in here
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
you can use boostrap css to split the div's.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<?php
$work_args = array(
'post_type' => 'bp_work_post_type',
'post_status' => 'publish',
'posts_per_page' => 6,
'offset' => 6
);
$work_query = new WP_Query( $work_args );
?>
<?php if ( $work_query->have_posts() ) : ?>
<div class="section row">
<?php while ( $work_query->have_posts() ) : $work_query->the_post(); ?>
<div class="post-grid col-md-2">
//Grid Content in here
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
Use the modulus symbol (%)
<?php
$work_args = array(
'post_type' => 'bp_work_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$work_query = new WP_Query( $work_args );
$nb_posts = $work_query->post_count;
$post_per_section = 6;
?>
<?php if ( $work_query->have_posts() ) : ?>
<div class="section">
<?php $count=0; ?>
<?php while ( $work_query->have_posts() ) : $work_query->the_post(); $count++; ?>
<div class="post-grid"></div>
<?php if($count % $post_per_section == 0 && $nb_posts !== $post_per_section ): ?>
</div><div class="section">
<?php endif;?>
<?php endwhile;?>
</div>
<?php endif; ?>

Posts list with their taxonomy

I would like to display list of post for a wordpress post type but I don't know how to show taxonomy term linked to the post.
My code :
<ul class="list-group">
<?php
$args = array(
'post_type' => 'faq',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
)
;$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="list-group-item">
<span class="label label-default">xxxxx taxonomy xxxxxx</span>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
Could you help me please.
<?php query_posts(array('post_type'=>'faq', 'posts_per_page'=>5, 'order' => 'DESC', )); ?>
<?php if(have_posts()) { while(have_posts()) { the_post();?>
<div class="col-md-6 col-sm-6">
<?php if ( has_post_thumbnail() ) {?>
<div class="blog_image">
<?php the_post_thumbnail( 'full' ); ?>
</div>
<?php } else{ ?>
<?php }?>
<div class="entry-content">
<h3 class="blog-title">
<?php the_title(); ?>
</h3>
<span class="post-date"><?php echo get_the_date('d. M Y'); ?></span>
<p><?php echo wp_trim_words( get_the_content(), 50, ' (...)' );?></p>
</div>
</div>
<?php }
}
wp_reset_query();
?>
for image display <?php the_post_thumbnail( 'full' ); ?>
for title display <?php the_title_attribute(); ?> for title display
for content display <?php the_content();?>
change HTML as you want
<?php
/* FIRST
* Note: This function only returns results from the default “category” taxonomy. For custom taxonomies use get_the_terms().
*/
$categories = get_the_terms( $post->ID, 'taxonomy' );
// now you can view your category in array:
// using var_dump( $categories );
// or you can take all with foreach:
foreach( $categories as $category ) {
echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
}

Wordpress Jetpack infinite scroll is not working

When passing different posts_per_page values in functions.php number of posts shown changes but it doesnt load any other posts on scroll. Changing type to click doesn't do anything.
adding support in functions.php:
add_theme_support( 'infinite-scroll', array(
'type' => 'scroll',
'footer_widgets' => false,
'container' => 'content',
'render' => false,
'posts_per_page' => false,
) );
index.php:
<?php
get_header(); ?>
<div id="content">
<?php if (have_posts):
while (have_posts()) {
the_post();
get_template_part('content', get_post_format());
}
else:
echo '<p>No content found</p>';
endif; ?>
</div>
content.php:
<div class="post clearfix">
<div class="post-heading">
<h2><?php the_title(); ?></h2>
<h3><?php the_subtitle(); ?></h3>
</div>
<div class="post-content">
<?php the_content(); ?>
<?php if (is_single()): ?>
<div class="navigation clearfix">
<?php the_post_navigation( array(
'next_text' => 'Next',
'prev_text' => 'Previous',
'screen_reader_text' => ' '
)); ?>
</div>
<p>Main</p>
<?php endif; ?>
</div>

Resources