WordPress sticky post not showing - wordpress

In my Index.php template file I have the following code which should show a featured post if it's sticky and then loop through the remaining posts below. However on the second loop shows anything and the sticky post does not appear...
The post has definitely been made sticky and second loop has successfully hidden it but the first loop just doesn't acknowledge it :/ it's not set to private and is marked as published.
Can anyone see any issues with the code? Thanks
<?php get_header(); ?>
<div class="clearfix">
<?php if(is_home()) { //if home then show the featured post and others ?>
<div class="main-column">
<?php
$sticky = get_option( 'sticky_posts' );
$sticky_query = new WP_Query( 'p=' . $sticky[0] );
// The Loop
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<div class="featured">
<article class="clearfix">
<h2><?php echo $post->post_title ?></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" />';
}?>
<p><?php the_excerpt_max_charlength(300) ?></p>
</article>
</div>
<?php endwhile; ?>
<div class="articles">
<?php $query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page' => 10 ) );
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
$count++; ?>
<div <?php if($count % 2 == 0) echo 'class="no-margin"' ?>>
<article>
<h2><?php echo $post->post_title ?></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" />';
}?>
<p><?php the_excerpt_max_charlength(150) ?></p>
</article>
</div>
<?php if($count % 2 == 0) echo '<hr/>' ?>
<?php endwhile; ?>
</div>
<?php }else if(is_search() || is_category || is_tag()){ //if other than home (search, tag, category) then show normal list (no featured style) ?>
<div class="main-column search">
<?php
$count = 0;
if(is_search()){
echo '<h1>Search Results</h1>';
} elseif(is_author()){ ?>
<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
<h1><?php echo $curauth->user_firstname; ?> <?php echo $curauth->user_lastname; ?></h1>
<div class="archive-meta">
<p><?php echo $curauth->user_description; ?></p>
</div>
<?php } else if(is_category()){ ?>
<h1><?php single_cat_title( '', true ); ?></h1>
<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo '<div class="archive-meta">' . $category_description . '</div>';
?>
<?php }else if (is_tag()){
echo '<h1>'.the_tag().'</h1>';
}
if ( have_posts() ) : while ( have_posts() ) : the_post();
$count++; ?>
<div>
<article>
<h2><?php echo $post->post_title ?></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" />';
}?>
<p><?php the_excerpt_max_charlength(300) ?></p>
</article>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php } ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>

Changing the code to:
$sticky_query = new WP_Query( 'p=' . $sticky[1] );
fixes the problem but I don't know why???

Related

How to get all the post titles on a specific page in WordPress

I want to display all the posts on a specific page called "Articles".
However, when I used the_title(); in a loop, it seems that the title of this page is displayed instead.
relative code in articles.php (linked to the Articles page created in dashboard)
<div class="container">
<?php if ( have_posts() ) : ?>
<div class="content">
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
content.php
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-header section medium-padding">
<div class="post-meta-top">
<?php the_time(get_option('date_format')); ?>
<?php
if ( comments_open() ) {
echo '<span class="sep">/</span> ';
if ( is_single() )
comments_popup_link( '0 comments', '1 comment', '% comments', 'post-comments' );
else
comments_number( '0 comments', '1 comment', '% comments' );
}
?>
</div>
<h2 class="post-title"><?php the_title(); ?></h2>
</a>
This part works well in index.php.
Step 1: Create a new template articles.php inside the folder of content.php and insert this code:
<?php
$articles = new WP_Query( array( 'posts_per_page' => -1 ) );
while ( $articles->have_posts() ) :
$articles->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-header section medium-padding">
<div class="post-meta-top">
<?php the_time(get_option('date_format')); ?>
<?php
if ( comments_open() ) {
echo '<span class="sep">/</span> ';
if ( is_single() )
comments_popup_link( '0 comments', '1 comment', '% comments', 'post-comments' );
else
comments_number( '0 comments', '1 comment', '% comments' );
}
?>
</div>
<h2 class="post-title"><?php the_title(); ?></h2>
</a>
<?php endwhile; ?>
Step 2: In articles.php replace this line:
<?php get_template_part( 'content', get_post_format() ); ?>
with this one:
<?php get_template_part( 'articles' ); ?>
Not tested but it should work.

Wordpress theme... error on my loop?

The loop looks good and no errors but when i paginating the loop didn't works and not change the posts showed but the page/2 or page/3 is showed in the url and by these error in the loop the posts are showed 2 hours or more late.
Elsewhere my code is:
<div class="uk-grid contenido" id="contenido">
<div class="uk-width-1-6 uk-hidden-small uk-hidden-medium"></div>
<div class="uk-width-large-2-6 uk-width-small-1-1 uk-width-medium-1-2">
<button class="uk-button-primary" type="button">Al momento</button>
<hr class="uk-grid-divider">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="uk-article">
<h2 class="uk-article-title"><?php the_title(); ?></h2>
<p class="uk-article-Meta p4d"><?php the_time('l j'); ?> de <?php the_time('F'); ?> de <?php the_time('Y'); ?> - Hace <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ); ?> | <?php $categories = get_the_category();
$separator = ','; $output = ''; if ( ! empty( $categories ) ) {foreach( $categories as $category ) {$output .= '' . esc_html( $category->name ) . '' . $separator; } echo trim( $output, $separator );} ?> | <?php if(function_exists('the_views')) { the_views(); } ?></p>
<p class="uk-article-lead"><?php echo substr(get_the_excerpt(), 0,130); ?>...<a class="more" href="<?php the_permalink(); ?>">(+)</a></p>
<div class="uk-thumbnail uk-thumbnail-large uk-overlay-hover uk-overlay-grayscale"><?php echo get_the_post_thumbnail($post->ID, 'articulo-normal-index'); ?></div>
<p class="uk-article-meta"><?php $categories = get_the_category();
$separator = ','; $output = ''; if ( ! empty( $categories ) ) {foreach( $categories as $category ) {$output .= '' . esc_html( $category->name ) . '' . $separator; } echo trim( $output, $separator );} ?> / <?php the_title(); ?></p>
<hr class="uk-article-divider">
</article>
<?php endwhile; ?>
<?php numeric_posts_nav(); ?>
<?php else : ?>
<p>Disculpa, no hay articulos</p>
<?php endif; ?>
</div>
<div class="separate uk-hidden-small uk-hidden-medium"></div>
<div class="uk-width-large-1-6 uk-hidden-small uk-width-medium-1-2 uk-block-muted min-padding">
<button class="uk-button-primary" type="button">Destacadas</button>
<hr class="uk-grid-divider">
<article class="uk-article uk-text-center">
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'destacada' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<h2 class="uk-article-title-small uk-text-center"><?php the_title(); ?></h2>
<p class="uk-article-meta uk-text-center"><?php the_time('l j'); ?> de <?php the_time('F'); ?> de <?php the_time('Y'); ?> - Hace <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ); ?></p>
<div class="uk-thumbnail uk-thumbnail-medium uk-container-center"><?php echo get_the_post_thumbnail($post->ID, 'articulo-destacadas-sidebar'); ?></div>
<hr class="uk-article-divider">
</article>
<?php
endforeach;
?>
<div class="uk-text-center data-uk-lightbox"><?php dynamic_sidebar( 'anuncio-destacadas' ); ?></div>
</div>
<?php get_sidebar(); ?>
<div class="uk-width-1-6"></div>
</div>

Two Wordpress search results pages

I would like to have two different search results depending on the page, that the user is on.
I have duplicated how the previous search contents would display, added the extra code that I want the staff search to show, and then put an if statement around it.
If user is on page 'staff' -
then show this
Else
show this.
I cant seem to get the if-statement to work, any help would be appreciated.
EDIT
This is the original working code, before i put the IF statement in, to try and create another set of results for staff searching from a specific page.
<?php
if ( is_home() ) {
$paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : get_query_var( 'paged' );
$args = array(
'showposts' => (int) get_option('trim_homepage_posts'),
'paged' => $paged,
'category__not_in' => (array) get_option('trim_exlcats_recent'),
);
query_posts( apply_filters( 'et_home_args', $args ) );
}
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="entry post clearfix preownedSearch">
<h2 class="et_pt_portfolio_title"><?php the_title(); ?></h2>
<?php
$index_postinfo = get_option('trim_postinfo1');
if ( $index_postinfo ){
echo '<p class="meta">';
et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
echo '</p>';
}
?>
<div class="post-content clearfix">
<?php
$thumb = '';
$width = apply_filters('et_image_width',260);
$height = apply_filters('et_image_height',170);
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
<div class="featured_box filter">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
</a>
</div> <!-- end .featured_box -->
<?php } ?>
</div>
</article> <!-- end .post-->
<?php
endwhile;
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { get_template_part('includes/navigation','entry'); }
else:
get_template_part('includes/no-results','entry');
endif;
if ( is_home() ) wp_reset_query(); ?>
This is the coding after i've put the if-statement in -
<?php
if ( is_home() ) {
$paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : get_query_var( 'paged' );
$args = array(
'showposts' => (int) get_option('trim_homepage_posts'),
'paged' => $paged,
'category__not_in' => (array) get_option('trim_exlcats_recent'),
);
query_posts( apply_filters( 'et_home_args', $args ) );
}
?>
<?php if (is_page('stock-managers')) { ?>
<article class="entry post clearfix preownedSearch">
<h2 class="et_pt_portfolio_title"><?php the_title(); ?><?php the_meta(); ?></h2>
<?php
$index_postinfo = get_option('trim_postinfo1');
if ( $index_postinfo ){
echo '<p class="meta">';
et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
echo '</p>';
}
?>
<div class="post-content clearfix">
<?php
$thumb = '';
$width = apply_filters('et_image_width',260);
$height = apply_filters('et_image_height',170);
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
<div class="featured_box filter">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
</a>
</div> <!-- end .featured_box -->
<?php } ?>
</div>
</article> <!-- end .post-->
<?php } else { ?>
<article class="entry post clearfix preownedSearch">
<h2 class="et_pt_portfolio_title"><?php the_title(); ?></h2>
<?php
$index_postinfo = get_option('trim_postinfo1');
if ( $index_postinfo ){
echo '<p class="meta">';
et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
echo '</p>';
}
?>
<div class="post-content clearfix">
<?php
$thumb = '';
$width = apply_filters('et_image_width',260);
$height = apply_filters('et_image_height',170);
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
<div class="featured_box filter">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
</a>
</div> <!-- end .featured_box -->
<?php } ?>
</div>
</article> <!-- end .post-->
<?php } endif; ?>
<?php
endwhile;
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { get_template_part('includes/navigation','entry'); }
else:
get_template_part('includes/no-results','entry');
endif;
if ( is_home() ) wp_reset_query(); ?>
Edit 2 Match
<?php
if ( is_home() ) {
$paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : get_query_var( 'paged' );
$args = array(
'showposts' => (int) get_option('trim_homepage_posts'),
'paged' => $paged,
'category__not_in' => (array) get_option('trim_exlcats_recent'),
);
query_posts( apply_filters( 'et_home_args', $args ) );
}
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($post->post_name('stock-managers')) { ?>
<article class="entry post clearfix preownedSearch">
<h2 class="et_pt_portfolio_title"><?php the_title(); ?><?php the_meta(); ?></h2>
<?php
$index_postinfo = get_option('trim_postinfo1');
if ( $index_postinfo ){
echo '<p class="meta">';
et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
echo '</p>';
}
?>
<div class="post-content clearfix">
<?php
$thumb = '';
$width = apply_filters('et_image_width',260);
$height = apply_filters('et_image_height',170);
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
<div class="featured_box filter">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
</a>
</div> <!-- end .featured_box -->
<?php } ?>
</div>
</article> <!-- end .post-->
<?php else : ?>
<article class="entry post clearfix preownedSearch">
<h2 class="et_pt_portfolio_title"><?php the_title(); ?></h2>
<?php
$index_postinfo = get_option('trim_postinfo1');
if ( $index_postinfo ){
echo '<p class="meta">';
et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
echo '</p>';
}
?>
<div class="post-content clearfix">
<?php
$thumb = '';
$width = apply_filters('et_image_width',260);
$height = apply_filters('et_image_height',170);
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
<div class="featured_box filter">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
</a>
</div> <!-- end .featured_box -->
<?php } ?>
</div>
</article> <!-- end .post-->
<?php endif; ?>
<?php
endwhile;
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { get_template_part('includes/navigation','entry'); }
else:
get_template_part('includes/no-results','entry');
endif;
if ( is_home() ) wp_reset_query(); ?>
Your php Syntax is wrong.
PHP Syntax Check: Parse error: syntax error, unexpected 'endif' (T_ENDIF) in your code on line 7 <?php } endif;?> PHP Syntax Check: Errors parsing your code
With removing endif;you're good to go
<?php if (is_page('staff')) { ?>
<article class="entry post clearfix">
<-- Title / Thumbnail / Price / Description coding -->
</article>
<?php } else { ?>
<article class="entry post clearfix">
<-- Title / Thumbnail / Description coding -->
</article>
<?php } ?>
Learn more about PHP's Alternativ syntax
See explanation of using if(condition){}and if(condition):endif; at this answer
Edit:
If you're using is_page within the Loop, it'll be allways false /see Source.
Instead you could compare a chosen value (here it's staff) to any other field of your current post/page.
<?php if ( $post->post_name=='staff' ) { ?>
<article class="entry post clearfix">
<-- Title / Thumbnail / Price / Description coding -->
</article>
<?php } else { ?>
<article class="entry post clearfix">
<-- Title / Thumbnail / Description coding -->
</article>
<?php } ?>
Edit No.2:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($post->post_name('stock-managers')) { ?>
<article class="entry post clearfix preownedSearch">
</article> <!-- end .post-->
<?php else : ?>
<article class="entry post clearfix preownedSearch">
</article> <!-- end .post-->
<?php endif; ?>
<?php
endwhile;
?>

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

How do I set ACF repeater to only show X items per row

I'm using ACF pro repeater fields, which renders individual images per row. However it shows all images until it wraps to the next line:
<div class="client-logo-web-wrapper"><?php $logo_images = get_field( 'logo' ); ?>
<?php if ( $logo_images ) : ?>
<?php foreach ( $logo_images as $logo_image ): ?>
<a href="<?php echo $logo_image['url']; ?>">
<img src="<?php echo $logo_image['sizes']['thumbnail']; ?>" alt="<?php echo $logo_image['alt']; ?>" />
</a>
<p><?php echo $logo_image['caption']; ?></p>
<?php endforeach; ?>
<?php endif; ?>
<?php if ( have_rows( 'logo_gallery' ) ) : ?>
<?php while ( have_rows( 'logo_gallery' ) ) : the_row(); ?>
<?php $client_logo = get_sub_field( 'client_logo' ); ?>
<?php if ( $client_logo ) { ?>
<div class="client-logo"><img src="<?php echo $client_logo['url']; ?>" alt="<?php echo $client_logo['alt']; ?>" />
</div>
<?php } ?>
<?php endwhile; ?></div>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
Instead I would like to display a set number of images (for ex. 4) per row. Thanks!
You need to define the limit, and the break the loop once you reach that limit. Also, you can also set the max number of items in your ACF field definition.
Try the following with the limit set:
<?php
$show = 4;
$count = 0;
?>
<?php if ( $logo_images ) : ?>
<?php foreach ( $logo_images as $logo_image ): ?>
<?php $count++; ?>
<a href="<?php echo $logo_image['url']; ?>">
<img src="<?php echo $logo_image['sizes']['thumbnail']; ?>" alt="<?php echo $logo_image['alt']; ?>" />
</a>
<p><?php echo $logo_image['caption']; ?></p>
<?php if ($count > $show) { break; } ?>
<?php endforeach; ?>
<?php endif; ?>

Resources