WP_Query don't show pagination for frontpage - wordpress

Can someone take a look on my code ? I can't figure why post pagination not showing, although when I type page/2 in browser url, the list of post works properly.
<?php
/**
* Template Name: Frontpage
*
* #package Bootstrap Canvas WP
* #since Bootstrap Canvas WP 1.0
*/
get_header(); ?>
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array('post_type' => 'post', 'orderby' => 'ASC', 'paged' => $paged, 'posts_per_page' => 9 );
$loop = new WP_Query($args);
$count = 0;
?>
<div class="row">
<div class="col-sm-12 blog-main pt-3 pb-5">
<?php if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); ?>
<?php $count++ ?>
<?php $date_format = get_option( 'date_format' ); ?>
<?php if($count == 1) : ?>
<div class="row">
<?php endif ?>
<?php if(($count == 1) || ($count == 9)) : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-8 col-xl-8 np-big">
<?php endif ?>
<?php if($count == 2 || $count == 7) : ?>
<div class="col-sm-12 col-md-4 col-xl-4 np-small">
<?php endif ?>
<?php if($count > 1 && $count < 4 || $count > 6 && $count <= 9 ) : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-12 col-xl-12">
<?php elseif ($count >= 4) : ?>
<?php if ($count == 5) : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-4 col-xl-4 np-small">
<?php else : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-4 col-xl-4">
<?php endif ?>
<?php endif ?>
<a href="<?php echo get_the_permalink(); ?>"><div class="post-bg-home" style="background: linear-gradient( rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.7) ), url(<?php echo get_the_post_thumbnail_url() ?>);">
<h2><?php echo get_the_title(); ?></h2>
<p class="data"><?php echo the_time( $date_format ); ?></p>
</div></a>
<?php if($count == 1) : ?>
</div>
<?php endif ?>
<?php if($count > 1 ) : ?>
</div>
<?php endif ?>
<?php if($count == 3 || $count == 8) : ?>
</div>
<?php endif ?>
<?php endwhile; ?>
</div>
</div>
<div class="paginacja col-sm-12 col-md-12 col-xl-12">
<hr class="paginacja-hr">
<?php the_posts_pagination(); ?>
</div>
<?php else : ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1>Not Found</h1>
</div>
<?php endif; ?>
</div><!-- /.blog-main -->
</div><!-- /.row -->
<?php get_footer(); ?>

Try this code
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array('post_type' => 'post', 'orderby' => 'ASC', 'paged' => $paged, 'posts_per_page' => 3 );
$loop = new WP_Query($args);
$count = 0;
?>
<div class="row">
<div class="col-sm-12 blog-main pt-3 pb-5">
<?php if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); ?>
<?php $count++ ?>
<?php $date_format = get_option( 'date_format' ); ?>
<?php if($count == 1) : ?>
<div class="row">
<?php endif ?>
<?php if(($count == 1) || ($count == 9)) : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-8 col-xl-8 np-big">
<?php endif ?>
<?php if($count == 2 || $count == 7) : ?>
<div class="col-sm-12 col-md-4 col-xl-4 np-small">
<?php endif ?>
<?php if($count > 1 && $count < 4 || $count > 6 && $count <= 9 ) : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-12 col-xl-12">
<?php elseif ($count >= 4) : ?>
<?php if ($count == 5) : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-4 col-xl-4 np-small">
<?php else : ?>
<div id="post-<?php the_ID(); ?>" class="col-sm-12 col-md-4 col-xl-4">
<?php endif ?>
<?php endif ?>
<a href="<?php echo get_the_permalink(); ?>"><div class="post-bg-home" style="background: linear-gradient( rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.7) ), url(<?php echo get_the_post_thumbnail_url() ?>);">
<h2><?php echo get_the_title(); ?></h2>
<p class="data"><?php echo the_time( $date_format ); ?></p>
</div></a>
<?php if($count == 1) : ?>
</div>
<?php endif ?>
<?php if($count > 1 ) : ?>
</div>
<?php endif ?>
<?php if($count == 3 || $count == 8) : ?>
</div>
<?php endif ?>
<?php endwhile; ?>
</div>
</div>
<div class="paginacja col-sm-12 col-md-12 col-xl-12">
<hr class="paginacja-hr">
<?php $GLOBALS['wp_query']->max_num_pages = $loop->max_num_pages;
the_posts_pagination( array(
'mid_size' => 1,
'prev_text' => __( 'Back', 'green' ),
'next_text' => __( 'Next', 'green' ),
'screen_reader_text' => __( 'Posts navigation' )
) ); ?>
</div>
<?php else : ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1>Not Found</h1>
</div>
<?php endif; ?>
</div><!-- /.blog-main -->
</div><!-- /.row -->
<?php get_footer(); ?>

Related

WP Template customizing

i'm trying to change my single-post template ,But i can't the the_content Function.
Where should it be ?
<div class="single-post-content clearfix">
<?php
the_content(esc_html__('Read more!', 'voyager'));
wp_link_pages(array('before' => '<div class="page-link">' . esc_html__('Pages', 'voyager') . ': ', 'after' => '</div>'));
?>
</div>
Update 1 the entire code
<?php
/**
* The template for displaying all single posts and attachments
*/
get_header();
$pf = get_post_format();
$voyager_single_post_sidebar_under = cstheme_option( 'single_post_sidebar_under' );
$single_post_sidebar = cstheme_option( 'single_post_sidebar' );
$single_post_sidebar_position = '';
if( $single_post_sidebar == 'left-sidebar' ) {
$single_post_sidebar_position = 'pull-right';
}
/* ADD 1 view for this post */
$post_views = (get_post_meta(get_the_ID(), "post_views", true) > 0 ? get_post_meta(get_the_ID(), "post_views", true) : "0");
update_post_meta(get_the_ID(), "post_views", (int)$post_views + 1);
$single_post_featured_img = cstheme_option( 'single_post_featured_img' );
$featured_image_url = wp_get_attachment_url(get_post_thumbnail_id());
?>
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="blog-single-wrap" class="<?php echo 'format-' . $pf . ' featured_img_' . $single_post_featured_img ?> clearfix">
<?php if( isset( $single_post_featured_img ) && $single_post_featured_img == 'fullwidth' ) { ?>
<div class="single_post_header">
<div class="featured_img_bg" style="background-image:url(<?php echo $featured_image_url; ?>);"></div>
<div class="single_post_meta_category"><?php the_category(', '); ?></div>
<h2 class="single-post-title"><?php the_title(); ?></h2>
<div class="single_post_header_bottom">
<div class="single_post_meta">
<span class="post-meta-date"><?php the_time('M j, Y') ?></span>
<span class="post_meta_views"><i class="fa fa-eye"></i> <span><?php echo (get_post_meta(get_the_ID(), "post_views", true) > 0 ? get_post_meta(get_the_ID(), "post_views", true) : "0"); ?></span></span>
<span class="post-meta-likes"><?php echo cstheme_likes(); ?></span>
<span class="post-meta-comments"><i class="fa fa-comments"></i><?php echo get_comments_number(get_the_ID()); ?></span>
</div>
</div>
</div>
<?php if ( $single_post_sidebar != 'no-sidebar' && $voyager_single_post_sidebar_under != 'next' && ( $pf != 'image' && $pf != 'standard' ) ) { ?>
<div class="post_format_content mb55 text-center">
<?php get_template_part( 'framework/post-format/post', get_post_format() ); ?>
</div>
<?php } ?>
<?php } else { ?>
<div class="single_post_header">
<div class="single_post_meta_category"><?php the_category(', '); ?></div>
<h2 class="single-post-title"><?php the_title(); ?></h2>
<div class="row single_post_header_bottom">
<div class="col-md-4 text-left">
<div class="single_post_meta_author"><?php echo esc_html__('posted by', 'voyager') ?> <a class="heading_font" href="<?php echo esc_url( get_author_posts_url(get_the_author_meta('ID')) ); ?>"><?php echo get_the_author_meta('display_name') ?></a></div>
</div>
<div class="col-md-4">
<div class="single_post_meta">
<span class="post-meta-date"><?php the_time('M j, Y') ?></span>
<span class="post_meta_views"><i class="fa fa-eye"></i> <span><?php echo (get_post_meta(get_the_ID(), "post_views", true) > 0 ? get_post_meta(get_the_ID(), "post_views", true) : "0"); ?></span></span>
<span class="post-meta-likes"><?php echo cstheme_likes(); ?></span>
<span class="post-meta-comments"><i class="fa fa-comments"></i><?php echo get_comments_number(get_the_ID()); ?></span>
</div>
</div>
<div class="col-md-4 text-right">
<?php if(cstheme_option('single_post_sharebox') != 0) { get_template_part( 'templates/blog/sharebox' ); } ?>
</div>
</div>
</div>
<?php if ( $voyager_single_post_sidebar_under != 'next' ) { ?>
<div class="post_format_content mb55 text-center">
<?php get_template_part( 'framework/post-format/post', get_post_format() ); ?>
</div>
<?php } ?>
<?php } ?>
<?php if( $single_post_sidebar != 'no-sidebar' ) { ?>
<div class="row">
<div class="col-md-9 <?php echo $single_post_sidebar_position; ?>">
<?php } ?>
<?php if( isset( $single_post_featured_img ) && ( $single_post_featured_img == 'fullwidth' ) && ( $pf != 'image' && $pf != 'standard' ) && $voyager_single_post_sidebar_under != 'under' ) { ?>
<div class="post_format_content mb55 text-center">
<?php get_template_part( 'framework/post-format/post', $pf ); ?>
</div>
<?php } else if ( $single_post_featured_img != 'fullwidth' && $voyager_single_post_sidebar_under != 'under' ) { ?>
<div class="post_format_content mb55 text-center">
<?php get_template_part( 'framework/post-format/post', get_post_format() ); ?>
</div>
<?php } ?>
<div class="single-post-content clearfix">
<?php
the_content(esc_html__('Read more!', 'voyager'));
wp_link_pages(array('before' => '<div class="page-link">' . esc_html__('Pages', 'voyager') . ': ', 'after' => '</div>'));
?>
</div>
<div class="posts_nav_link"><?php posts_nav_link(); ?></div>
<div class="single_sharebox_wrap clearfix">
<div class="single_post_meta_tags pull-left">
<?php if( has_tag() ) {
the_tags('','', '');
} ?>
</div>
<div class="pull-right">
<?php if(cstheme_option('single_post_sharebox') != 0) { get_template_part( 'templates/blog/sharebox' ); } ?>
</div>
</div>
<?php if(cstheme_option('single_post_authorinfo') != 0) { get_template_part( 'templates/blog/authorinfo' ); } ?>
<?php if(cstheme_option('single_post_navigation') != 0) { ?>
<div class="single_post_nav clearfix">
<?php
$prev_post = get_adjacent_post(false, '', true);
$next_post = get_adjacent_post(false, '', false);
if($prev_post){
$post_url = get_permalink($prev_post->ID);
echo '<div class="pull-left"><p class="heading_font"><i class="fa fa-chevron-left"></i>' . esc_html__('Previous','voyager') . '</p><b>' . $prev_post->post_title . '</b></div>';
}
if($next_post) {
$post_url = get_permalink($next_post->ID);
echo '<div class="pull-right text-right"><p class="heading_font">' . esc_html__('Next','voyager') . '<i class="fa fa-chevron-right"></i></p><b>' . $next_post->post_title . '</b></div>';
}
?>
</div>
<?php } ?>
<?php if( $single_post_sidebar != 'no-sidebar' ) { ?>
</div>
<div class="col-md-3">
<?php get_sidebar(); ?>
</div>
</div>
<?php } ?>
<?php if(cstheme_option('single_post_relatedposts') != 0) { get_template_part('templates/blog/related-posts'); } ?>
<?php
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
</div>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
The page is enter link description here

Getting multiple copies of images when using WP_query to display custom post

I'm using WP-query to display my 3 most recent 'events' which are a custom post type.
Visually it looks like its working fine, but after doing some debugging I noticed that the same image is being loaded 4 times for each post.
<div class="container">
<?php
$args = array(
'post_type' => 'projects',
'posts_per_page' => '2'
);
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
?>
<div class="row small-up-1 medium-up-2 large-up-<?php echo $count ?>" data-equalizer="news" data-equalize-on-stack="false" data-equalize-on="medium">
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="column">
<div class="card">
<div class="card-divider" data-equalizer-watch="news">
<h4> <?php the_title(); ?></h4>
<?php
// FLEXIBLE CONTENT NAME
if( have_rows('main_sections') ):
// FLEXIBLE CONTENT NAME
while ( have_rows('main_sections') ) : the_row(); ?>
<?php // LAYOUT NAME
if( get_row_layout() == 'standard_details' ): ?>
<?php if( get_sub_field('date_of_event') ):
$field_name1 = "date_of_event";
$field1 = get_sub_field_object($field_name1);
?>
<p><?php echo $field1['value']; ?></p>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php the_excerpt(__('(more…)')); ?>
</div>
<?php if ( 'video' == get_post_format() ) { ?>
<div class="video-icon">
<i class="fa fa-play fa-3x" aria-hidden="true"></i>
<?php } ?>
<a href="<?php the_permalink() ?>">
<img data-interchange="[<?php echo the_post_thumbnail_url('recent-news'); ?>, small]" alt="<?php the_title(); ?>" />
</a>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>

ACF Pagination not working when the_content is present

I have this custom template in Wordpress which includes a script that paginates ACF fields. Here is the source code:
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="pagetitle"><?php the_title(); ?></h1>
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<?php
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$row = 0;
$files_per_page = 30; // How many images to display on each page
$files = get_field( 'fisier' );
$total = count( $files );
$pages = ceil( $total / $files_per_page );
$min = ( ( $page * $files_per_page ) - $files_per_page ) + 1;
$max = ( $min + $files_per_page ) - 1;
?>
<div class="container">
<div class="row">
<div class="col-md-8">
<section id="content">
<div class="wrapper">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php if( have_rows('fisier') ): ?>
<div class="article-files">
<h4>Files</h4>
<div class="row">
<?php while ( have_rows('fisier') ) : the_row();
$row++;
if($row < $min) { continue; }
if($row > $max) { break; }
?>
<div class="col-xs-12 file">
<?php $x = 0; ?>
<?php $file = get_sub_field('link'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php $file = get_sub_field('fisier_intern'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php if( $x == 0 ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<div class="file-pagination">
<?php
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'show_all' => false,
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => $pages
));
?>
</div> <!-- file-pagination -->
</div>
<?php endif; ?>
</div> <!-- wrapper -->
</section> <!-- content -->
</div> <!-- col -->
<div class="col-md-4">
<aside id="sidebar">
<?php get_sidebar(); ?>
</aside> <!-- aside -->
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<?php get_footer(); ?>
The code above doesn't display the pagination links but if I remove this part:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Everything works perfectly. Any idea about how I can fix this?
Thanks
Solved the problem! I had to move the pagination settings after the_content() as shown below:
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="pagetitle"><?php the_title(); ?></h1>
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<div class="container">
<div class="row">
<div class="col-md-8">
<section id="content">
<div class="wrapper">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$row = 0;
$files_per_page = 30; // How many images to display on each page
$files = get_field( 'fisier' );
$total = count( $files );
$pages = ceil( $total / $files_per_page );
$min = ( ( $page * $files_per_page ) - $files_per_page ) + 1;
$max = ( $min + $files_per_page ) - 1;
?>
<?php if( have_rows('fisier') ): ?>
<div class="article-files">
<h4>Files</h4>
<div class="row">
<?php while ( have_rows('fisier') ) : the_row();
$row++;
if($row < $min) { continue; }
if($row > $max) { break; }
?>
<div class="col-xs-12 file">
<?php $x = 0; ?>
<?php $file = get_sub_field('link'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php $file = get_sub_field('fisier_intern'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php if( $x == 0 ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<div class="file-pagination">
<?php
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'show_all' => false,
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => $pages
));
?>
</div> <!-- file-pagination -->
</div>
<?php endif; ?>
</div> <!-- wrapper -->
</section> <!-- content -->
</div> <!-- col -->
<div class="col-md-4">
<aside id="sidebar">
<?php get_sidebar(); ?>
</aside> <!-- aside -->
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<?php get_footer(); ?>

How to display Custom Post type in bootstrap columns in Wordpress [duplicate]

I'm trying to achieve a 3x3 grid view of all the WordPress posts on the "blog" page (index.php). I'm building the site based on Bootstrap 3.
Therefore the loop has to create the columns and rows with PHP.
I'd like to have it set up in rows, so that potential height differences are being reset every row. The bootstrap grid would look like this:
<div class="row">
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
</div>
<div class="row">
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
</div>
<div class="row">
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
<div class="col-sm-4">content</div>
</div>
Lacking the PHP skills for setting up the loop properly, I tried hacking my way around, coming up with 3 times this (modifying the offsets):
<?php query_posts('posts_per_page=1&offset=0'); while (have_posts()) : the_post(); ?>
<div class="row">
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php endwhile; ?>
<?php query_posts('posts_per_page=1&offset=1'); while (have_posts()) : the_post(); ?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php endwhile; ?>
<?php query_posts('posts_per_page=1&offset=2'); while (have_posts()) : the_post(); ?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
</div>
<?php endwhile; ?>
It has obvious disadvantages:
a lot of unnecessary PHP requests/loops
filtering by categories, tags, etc doesn't work
Could you help me out with creating the PHP loop?
The most related question I found is this, but the column layout is somehow skewed!
Thanks a lot! Philipp
The easiest would be to use one container and put all the contetn items in it, then equal their height via js like that.
PHP
<?php query_posts('posts_per_page=9');while (have_posts()) : the_post();?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php endwhile?>
JS:
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.each(function() { $(this).height(tallest); });
}
$(document).ready(function() {
equalHeight($(".thumb"));
});
If thats no option, you could do sth. like that:
PHP
<div class="row">
<?php
$count=0;
query_posts('posts_per_page=9');
while (have_posts()) : the_post();
?>
<div class="col-sm-4 blog-post thumb">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<?php
$count++;
if($count == 3 || $count == 6 ) echo '</div><div class="row">';
endwhile;
?>
</div>
Every three post objects must be contained within a row. So it will be like <div class="row"> <!-- post - post - post -> </div> <div class="row"> <!-- post - post - post -> </div>
If you would like to do this in php, and still maintain proper 'rowage' your code could look something like this:`
<div class="container">
<?php
$countturtle = 0 ;
$countbang = 0 ;
$count_posts = wp_count_posts( 'portobello' )->publish;
$args = array( 'post_type' => 'portobello', 'posts_per_page' => 32 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $countbang++ ?>
<?php if ( $countbang >= 2 ) {
$countturtle = $countturtle + 1 ; } ?>
<?php if ( $countbang == 1 ) {
echo '<div class="row first-training">'; } elseif ( ( $countturtle % 3 ) == 0 ) {
echo '<div class="row">'; } ; ?>
<div id="post-<?php the_ID(); ?>" class="training-block <?php echo $countbang; ?>-block-training col-sm-4" >
<header class="entry-header training-header">
<h1 class="entry-title train">
<?php the_title(); ?>
</h1>
</header><!-- .entry-header -->
<div class="entry-imogin">
ddd
</div><!-- .entry-imogin -->
</div><!-- #post -->
<?php if ( $countbang % 3 == 0 ) {
echo '</div>'; }
elseif ( $countposts == $countbang ) { echo '</div>';} ; ?>
<?php endwhile; ?>
</div>
Here a solution for 3 columns
layout :
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
...
<div class="row">
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'news',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
</div>
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'news',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 2): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
</div>
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'news',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 3): ?>
<h2><?php the_title(); ?></h2>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
</div>
</div>

Wordpress: Recent post by tags

this is my content.php I am trying to list 2 diffrent tag on my home page. 5 post from each tag. Example 5 tags from tag:Football down of it 5 tags from tag:Basketball and here is my content.php Thanks in advance.
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb col-md-4 col-sm-4 col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('home-thumb'); ?>
</a>
</div>
<?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="entry-summary <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="post-info">
<?php if ( 'post' == get_post_type() ) : ?>
<?php areview_posted_on(); ?>
<?php endif; ?>
<span class="cat-link">
<?php
$category = get_the_category();
if($category[0]){
echo '<i class="fa fa-folder"></i>' . esc_attr($category[0]->cat_name) . '';
}
?>
</span>
<?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) {
echo do_shortcode('[yasr_overall_rating]');
} ?>
</div>
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
<div class="buttons-area">
<?php if ( function_exists('cfs') && ($cfs->get('button_link') !='' ) && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1) ) : ?>
<?php echo esc_html($cfs->get('button_title')); ?>
<?php endif; ?>
<?php echo __('Read more', 'areview'); ?>
</div>
You should loop the tags and do a query for each one, and for each post insert your existing HTML, like this:
<?php
$tags = array(
'Football',
'Basketball'
);
foreach ($tags as $tag) {
query_posts(array(
'post_type' => 'post'
'posts_per_page' => 5,
'tax_query' => array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => $tag
)
));
if (have_posts()) {
while(have_posts()) {
the_post();
?>
///.... insert your existing code here
<?php
}
}
wp_reset_query();
}
?>
Just don't forget to insert your existing html instead of the ///.... insert your existing code here block.
Try this
<div class="relatedposts">
<h3>Related posts</h3>
<?php
$args=array(
'post_status' => 'publish',
'tag' => 'football,basketball', //Tag slug
'posts_per_page'=>4, // Number of related posts to display.
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
<?php the_title(); ?>
</a>
</div>
<?php }
wp_reset_query();
?>
</div>
Thank you everyone!! Finally I got it using this code:
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<div class="relatedposts">
<h3 class="widget-title">Football</h3>
<div class="decoration-bar"></div><br>
<?php
$args=array(
'post_status' => 'publish',
'tag' => 'football', //Tag slug
'posts_per_page'=>5, // Number of related posts to display.
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb col-md-4 col-sm-4 col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('home-thumb'); ?>
</a>
</div>
<?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="entry-summary <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="post-info">
<?php if ( 'post' == get_post_type() ) : ?>
<?php areview_posted_on(); ?>
<?php endif; ?>
<span class="cat-link">
<?php
$category = get_the_category();
if($category[0]){
echo '<i class="fa fa-folder"></i>' . esc_attr($category[0]->cat_name) . '';
}
?>
</span>
<?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) {
echo do_shortcode('[yasr_overall_rating]');
} ?>
</div>
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
<div class="buttons-area">
<?php if ( function_exists('cfs') && ($cfs->get('button_link') !='' ) && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1) ) : ?>
<?php echo esc_html($cfs->get('button_title')); ?>
<?php endif; ?>
<?php echo __('Read more', 'areview'); ?>
</div>
</article>
<?php }
wp_reset_query();
?>
</div>
<center><h4 >---> More Posts <---</h4></center>
<div class="relatedposts">
<h3 class="widget-title">Basketball</h3>
<div class="decoration-bar"></div><br>
<?php
$args=array(
'post_status' => 'publish',
'tag' => 'basketball', //Tag slug
'posts_per_page'=>3, // Number of related posts to display.
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb col-md-4 col-sm-4 col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('home-thumb'); ?>
</a>
</div>
<?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="entry-summary <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="post-info">
<?php if ( 'post' == get_post_type() ) : ?>
<?php areview_posted_on(); ?>
<?php endif; ?>
<span class="cat-link">
<?php
$category = get_the_category();
if($category[0]){
echo '<i class="fa fa-folder"></i>' . esc_attr($category[0]->cat_name) . '';
}
?>
</span>
<?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) {
echo do_shortcode('[yasr_overall_rating]');
} ?>
</div>
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
<div class="buttons-area">
<?php if ( function_exists('cfs') && ($cfs->get('button_link') !='' ) && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1) ) : ?>
<?php echo esc_html($cfs->get('button_title')); ?>
<?php endif; ?>
<?php echo __('Read more', 'areview'); ?>
</div>
</article>
<?php }
wp_reset_query();
?>
</div>
<center><h4 >---> More Posts <---</h4></center>
<div class="relatedposts">
<h3 class="widget-title">Handball</h3>
<div class="decoration-bar"></div><br>
<?php
$args=array(
'post_status' => 'publish',
'tag' => 'handball', //Tag slug
'posts_per_page'=>2, // Number of related posts to display.
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb col-md-4 col-sm-4 col-xs-12">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('home-thumb'); ?>
</a>
</div>
<?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="entry-summary <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="post-info">
<?php if ( 'post' == get_post_type() ) : ?>
<?php areview_posted_on(); ?>
<?php endif; ?>
<span class="cat-link">
<?php
$category = get_the_category();
if($category[0]){
echo '<i class="fa fa-folder"></i>' . esc_attr($category[0]->cat_name) . '';
}
?>
</span>
<?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) {
echo do_shortcode('[yasr_overall_rating]');
} ?>
</div>
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
<div class="buttons-area">
<?php if ( function_exists('cfs') && ($cfs->get('button_link') !='' ) && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1) ) : ?>
<?php echo esc_html($cfs->get('button_title')); ?>
<?php endif; ?>
<?php echo __('Read more', 'areview'); ?>
</div>
</article>
<?php }
wp_reset_query();
?>
</div>
<center><h4 >---> More Posts <---</h4></center>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Resources