Splitting WordPress Loops - wordpress

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

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>

WPquery + add row every nth element

So the case if following - i've got a wpquery like on the code below.
<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div class="row">
<div class="col-sm-12 col-lg-3">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
</div>
</div>
</div>
</section>
What I would like to achieve is to have the loop working like that:
<ROW>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
</ROW>
SO in fact what i would lke to achieve is to have 4 elements inside row without creating different loops. I know i should use some counter but i have no clue how;/
thanks
Add new row after 4 cols
<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
$count=0;
?>
<div class="row">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$count++;
?>
<div class="col-sm-12 col-lg-3">
<h1><?php the_title(); ?></h1>
</div>
<?php
if($count%4==0)
{
echo '</div><div class="row">';
}
endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>

Pagination is not working in archive page in 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>

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.

Resources