How to get the total number of count in acf field - wordpress

<p class="showitem">Showing
<span>
<?php if ( have_rows( 'packaging', 'options' ) ) : ?>
<?php while ( have_rows( 'packaging', 'options' ) ) : the_row(); ?>
<?php $count = count( get_sub_field( 'content' ) ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php echo $count; ?>
</span>
items</p>
I need output Showing 20 items. But it shows Showing 1 items
I tried to give inside the loop but it shows 1 20 times

Related

Wordpress how to display query results in specific div class if total number of posts is < 2

I'm working on a new Wordpress site and am trying to achieve something specific. I'd like to do a query_post and if there is only 1 post available, display that information in a specific div class, otherwise use a different class. Here is a simplified version of the code I am currently using. The one I will end up using, actually has additional query_posts within this:
<?php query_posts('post_type=events&showposts=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary left">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary left">
<?php endif; ?>
<?php else: ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary right">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary right">
<?php endif; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
I hope this makes sense..
You can check the global query after query_posts() like this before you run through the posts and display them:
<?php query_posts( 'post_type=events&showposts=-1' );
global $wp_query;
$classes = array( 'secondary' );
if( $wp_query->post_count == 1 ) :
$classes[] = 'left';
else :
$classes[] = 'right';
endif; ?>
With the above, your code can be shortened to something like this:
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
$type = get_field( 'event_type' );
if ( $type && in_array( 'public', $type ) ) :
$classes[] = 'public';
$action = get_permalink();
else :
$classes[] = 'private';
$action = 'mailto:johnsmith#gmail.com';
endif; ?>
<!-- Output HTML -->
<a href="<?php echo $action; ?>">
<div id="event" class="<?php echo join( ' ', $classes ); ?>">
<?php endwhile;
endif; ?>

ACF repeater inside group

I tried to get ACF repeater from the group field.
I must be blind, but I cannot find a mistake inside my code.
<?php if( have_rows( 'acf_group' ) ): ?>
<?php while( have_rows( 'acf_group' ) ): the_row(); ?>
<?php if( have_rows( 'acf_repeater' ) ): ?>
<ul>
<?php while( have_rows( 'acf_repeater' ) ): the_row();
$link_text = get_sub_field( 'acf_repeater_field_text' );
$link_url = get_sub_field( 'acf_repeater_field_url' );
?>
<li>
<?php echo $link_text; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

Display custom post type categories (terms) in loop

I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

how to add a class at particular child in wordpress loop

This is my code and i want to add a class in second child in loop. please tell me how can i do it. i am new in wordpress.
<?php
$args = array(
'posts_per_page' => 3,
'post_type' => 'hosting_plan',
'order' => 'ASC'
);
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<div class="hostplbx /*here i want to add class on second child*/">
<h3><?php the_field('plan_name'); ?></h3>
<div class="hostprice">
<span class="hosprice"><b class="rs"><?php the_field('plan_price'); ?></b> per month</span>
<span class="plandetail"><?php the_field('tag_line'); ?></span>
</div>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
You could use a simple iterator variable -- some sample code you can alter to apply to your template:
$counter = 1;
if ( have_posts() ) : while ( have_posts() ) : the_post();
if($counter == 2) echo "<div class='second_child_class'>content goes here</div>";
else echo "<div>content goes here</div>";
$counter++;
endwhile; endif;

How to add HTML code in Post Grid in Wordpress?

In a wordpress website showing 3 post per row and almost unlimited rows due to ajax autoload for pagination.
What is the best way to add say for example a custom HTML code instead of post every 5th post?
You will need to add a count to the loop that is pulling the posts into the rows.
for example:
<?php $args = array(
'post_type' => 'posts',
'posts_per_page' => -1
);
$posts = get_posts( $args );
if ( $posts ) :
$temp_post = $post; ?>
<div class="row">
<?php $count = 1; foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?></h1>
<?php //This count adds the custom html mark usful if its only a sinle line of markup.
echo ( $count % 5 == 0 ) ? '<h2 class="custom-html-markup">Custom Markup</h2>' : ''; ?>
<?php // If the markup is longer than a single line.
if ( $count % 5 == 0 ) : ?>
<div class="custom-html-markup">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php // This count closes and re-opens the rows
echo ( $count % 3 == 0 ) ? '</div><div class="row">' : ''; ?>
<?php $count++; endforeach; ?>
</div>
<?php $post = $temp_post;
endif; ?>

Resources