Pagination for a list of posts with custom_terms - wordpress

I have code that gets all posts for a custom term,
I want to apply pagination for that code.
<div> <?php
$text = $_POST['text'];
/* echo ($text); */
$args = array( 'numberposts' => -1 );
$posts = get_posts( $args );
foreach($posts as $post)
{
$custom_terms = get_the_terms($post->ID,'Characters');
foreach($custom_terms as $custom_term)
{
if ( ($custom_term->name) == ($text) )
{
?>
<div class="col-sm-2">
<article class="item">
<div class="article-inner">
<div class="overlay"></div>
<div class="img-wrapper"><img class="img-100p latest-post-image"
src="<?php the_post_thumbnail_url('large'); ?>" alt="img"></div>
<div class="article-info">
<h4 class="entry-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php
//echo $countA ?><?php //echo $count_posts?><?php //echo $count%$st?></a>
</h4>
</div>
</div>
</article>
</div>
<?php
}
}
}
?>
</div>
I'm using plugin
https://wordpress.org/plugins/wck-custom-fields-and-custom-post-types-creator/
to create that custom_terms on backend admin wordpress.
and try to use this code but when go to page number 2 no posts apperas
<div>
<?php
$text = $_POST['text'];
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$count = 0;
$st = 15;
$args = array( 'numberposts' => -1 );
$posts = get_posts( $args );
$stack = array(2005);
$poststatus = array( 'publish' );
$current_category = single_cat_title("", false);
foreach($posts as $post)
{
$custom_terms = get_the_terms($post->ID,'characters');
foreach($custom_terms as $custom_term)
{
if ( (($custom_term->name) == ($text)) )
{
$counter ++ ;
array_push($stack,($post->ID) );
}
}
}
$args1 = array(
'post_type' => 'post',
'post__in' => $stack,
//'category_name' => '',
'post_status' => 'publish',
'posts_per_page' => 18,
//'offset' => 5,
'order_by' => 'date',
'paged' => $paged,
'ignore_sticky_posts' => 5 ,
);
$wp_query = new WP_Query( $args1 );
$count_posts = $wp_query->post_count;
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$count++;
if ($count%$st == 4) :
$count = 1;
?>
<?php
endif;
?>
<div class="col-sm-4">
<article class="item">
<div class="article-inner">
<div class="overlay"></div>
<div class="img-wrapper"><img class="img-100p latest-post-image"
src="<?php the_post_thumbnail_url('large'); ?>" alt="img"></div>
<div class="article-info">
<h4 class="entry-title">
<?php the_title(); ?>
</h4>
</div>
</div>
</article>
</div><!--col-sm-4-->
<?php
endwhile;
//wp_reset_postdata();
//wp_reset_query();
//wp_reset_postdata();
endif;
?>
#ManoharSingh
This is full template file
<?php
/**
<?php /* Template Name: C1
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you'd like to further customize these archive views, you may create a
* new template file for each one. For example, tag.php (Tag archives),
* category.php (Category archives), author.php (Author archives), etc.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Primer
* #since 1.0.0
*/
get_header();
$temp = $wp_query;
// $wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=post_type_name'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div>
<?php
$text = $_POST['text'];
/* echo ($text); */
$args = array( 'numberposts' => -1 );
$posts = get_posts( $args );
foreach($posts as $post)
{
$custom_terms = get_the_terms($post->ID,'characters');
foreach($custom_terms as $custom_term)
{
if ( ($custom_term->name) == ($text) )
{
?>
<div class="col-sm-2">
<article class="item">
<div class="article-inner">
<div class="overlay"></div>
<div class="img-wrapper"><img class="img-100p latest-post-image" src="<?php the_post_thumbnail_url('large'); ?>" alt="img"></div>
<div class="article-info">
<h4 class="entry-title">
<?php the_title(); ?><?php //echo $countA ?><?php //echo $count_posts?><?php //echo $count%$st?>
</h4>
</div>
</div>
</article>
</div>
<?php
}
}
}
?>
</div>
<?php endwhile; ?>
<section class="all-pagination">
<div class="container">
<div class="col-md-12">
<div class="col-md-12 in-pagination">
<ul class="pagination">
<?php wp_pagenavi( array( 'query' => $wp_query ) ); ?>
<?php //if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?>
</ul>
</div>
</div>
</div>
</section>
<?php ?>
<?php get_footer(); ?>

Try this code,
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=post_type_name'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
And you can try this plugin for pagination 'https://wordpress.org/plugins/wp-pagenavi/'

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.

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,
));

wordpress shortcode for tab

i added this by custom post type. but i want to create this by shortcode method with the conditional statement. how to do this ? please anyone ans this ?
<?php
$args = array( 'posts_per_page' => 4, 'post_type'=> 'tab-items');
$myposts = get_posts( $args );
?>
<!--nab section date content area-->
<div class="date_section_area">
<!-- Nav tabs -->
<div class="date_section_list">
<ul>
<?php foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>
</div>
<!-- Tab panes -->
<div class="date_section_content">
<?php foreach( $myposts as $post ) : setup_postdata($post); ?>
<div class="single_date_section_content tab-pane fade" id="<?php the_ID(); ?>">
<?php the_post_thumbnail('tab-image'); ?>
<?php the_content(); ?>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
</div>
</div>
As far as i understand your question,
i have an idea that you can do like this:-
add_shortcode('my_post_shorcode', 'my_shortcode_function');
function my_shortcode_function($attr, $content)
{
extract(
shortcode_atts( array(
'posts_per_page' => '4',
'post_type' => 'tab-items',
'style' => 'default'
), $atts)
);
if($style == 'style-1')
{
//Add Your Style-1
} else if($style == 'style-2') {
//Add Your Style-2
} else {
$args = array( 'posts_per_page' => $posts_per_page, 'post_type'=> $post_type);
$myposts = get_posts( $args );
$tab_title = "";
$tab_content = "";
foreach ($myposts as $post)
{
setup_postdata($post);
$tab_title .= '<li>'.the_title().'</li>';
$tab_content .= '<div class="single_date_section_content tab-pane fade" id="tab-post-'.the_ID().'>
' . the_post_thumbnail('tab-image') . the_content() . '
</div>';
wp_reset_postdata();
}
?>
<!--nab section date content area-->
<div class="date_section_area">
<!-- Nav tabs -->
<div class="date_section_list">
<ul>
<?php echo $tab_title;?>
</ul>
</div>
<!-- Tab panes -->
<div class="date_section_content">
<?php echo $tab_content;?>
</div>
</div>
<?php
}
}
And shortcode will be like this:-
[my_post_shorcode posts_per_page="4" post_type="tab-items" style="default"]
Hope this will help you.

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