So I'm using an ACF repeater to populate content. I'd like to display the total count of items at the top of the block before the while loop has even executed. I can echo $counter at the bottom of the block, and it's great. But obviously, before anything runs, it's null, or 0 for obvious reasons. I'm just not sure how to show $counter outside of the loop before the loop has even ran.
<div class="col-sm-12 text-left">
<p><strong>Total Videos:</strong>
<?php var_dump($counter); ?>
</p>
</div>
<?php if ( have_rows( 'choir_videos' ) ) :
$counter = 0;
while ( have_rows( 'choir_videos' ) ) : the_row();
$counter++;
?>
```
html block
```
<?php
endwhile; endif;
?>
<div class="col-sm-12 text-left">
<p><strong>Total Videos:</strong>
<?php echo $counter ?>
</p>
</div>
Currently, $counter is working at the bottom of the executed code, as expected. the $counter at the top is returning NULL, as expected since $counter is not a thing yet. Regardless of when and how I set $counter = 0, I can't get the number I need to display.
you have to get the field object and than count the rows. example:
$object = get_field('choir_videos');
$item_count = count($object);
maybe you want to check if the object is an array before you count, because it can be an string, int or an array.
edit: if you want to load an object from another post, you must assign the post id to the get_field() method - just check the documentatin
Documentation for the get_field() method:
get_field() documentiation
Documentation for the count() method:
php count()
So, quite embarrassing. I simply just rant through the while loop and counted how many times it ran, then echoed that and it worked fine. Holy cow, my first SO question and it was the SIMPLEST thing. Ugh.
<p><strong>Total Videos:</strong>
<?php
$vid_total = 0;
while ( have_rows( 'choir_videos' ) ) : the_row();
$vid_total++;
endwhile;
?>
<?php echo $vid_total ?>
</p>
Related
I'm racking my brain trying to think why this isn't working. Seems to be right to me but I must be missing something.
<?php
if( have_rows('cta_box') ):
while( have_rows('cta_box') ) : the_row();
echo get_sub_field('cta_header');
echo get_sub_field('cta_content');
endwhile;
endif;
?>
Am I doing something dumb?
Thanks
It's a group field, not repeater. This'd help.
So in your case you'd use it like this.
$cta = get_field('cta_box');
echo $cta['cta_header'];
echo $cta ['cta_content];
your code is proper, i think the issue is where you want to display like in the specific template or any where in the page, you just need to assign with the template or page or post whatever its will display.
Here is the code that got it working. Can someone explain? Is the code above this ACF effecting what is returned?
<?php while ( have_posts() ) : the_post();
$cta = get_field('state_call_to_action');
echo $cta['call_to_action_heading'];
echo $cta['call_to_action_body'];
endwhile; ?>
Is it possible to target the next section inside the loop? so that it will scroll down to the next section area.
Here is my code:
<section id ="<?php echo $postid; ?>" class="subpage-wrapper fullscreen background parallax" style="background-image: url('<?php echo $image[0]; ?>')" data-img-width="1400" data-img-height="717" data-diff="100" data-oriz-pos="100%">
<a href="#<?php echo $postid; ?>" class="btn-scroll" data-scroll></a>
</section>
Remember that this is inside the WP_Query Loop
Thanks!
I'm not keeping any selections within your displaying of posts in mind, but I figure you can get 'round by using get_next_post(). Something like:
global $post;
$post = get_post($post_id);
$next = get_next_post();
$next_id = $next->id;
What about using your own counter?
$nextAnchorId = 0;
while ( have_posts() ) { //the loop
$nextAnchorId++; //make this actually be a reference to the *next* post
the_post();
if ($nextAnchorId < $post_count) //don't link to the next post if it's the last one
{
echo "<a href='#post-$nextAnchorId'>next post</a>";
}
}
I don't even know where to begin with this, so I'm just going to throw it out there. Say there is a custom post type and in it there is a wpAlchemy powered meta box with repeating fields, where in a person can add multiple attachments.Say you don't expect or trust the user to sort the attachments themselves so you want to return the array of attachments in alphabetical order by the value title. How would you do this with the following code? (This code is within the loop of a single page)
<?php
global $attachment_repeat;
$meta = $attachment_repeat->the_meta();
?>
<?php if($attachment_repeat->have_value('docs')):?>
<ul class="attachments">
<?php while( $attachment_repeat->have_fields('docs') ) : ?>
<li>
<!--url --><!--title --><?php $attachment_repeat->the_value('title'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
First you should get the main array ... of "docs" $arr = $attachment_repeat->the_value('docs'); if you var_dump($arr) you will see an associative array .. you can then sort using PHP array sort functions .. maybe something like this:
usort( $arr, "custom_sort" );
function custom_sort($a,$b) {
return strcmp( $a['title'], $b['title'] );
}
I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>
query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}
I'm outside the loop and want to call a custom value "featvideo" and display it. If there isn't "featvideo" then print an image...
The video displays, but when there isn't a video a blank box displays. You can see the the issue here: http://wgl.buildthesis.com
(and yes, $images is a function defined in functions.php and works)
<?php
$feat_catbox_1 = new WP_Query("cat=$tt_feat_id&showposts=$tt_feat_postcount");
while ($feat_catbox_1->have_posts()) : $feat_catbox_1->the_post();
$key = 'featvideo';
$video_url = get_post_custom_values($key);
$featuredvideo = $video_url[0];
?>
<div class="contentdiv">
<div id="featured-thumb">
<?php if ($key=="featvideo")
echo $featuredvideo;
elseif ($key=="")
echo $images('1', '390', '244', 'alignleft', true); ?>
</div>
</div>
Since you have set $key = 'featvideo' and then test if it is set later, it will always return true. You never change the value of $key anywhere in your code except when you set it.
I would suggest something like the following for your if statement:
<?php
if($featuredvideo)
echo $featuredvideo;
else
echo $images('1', '390', '244', 'alignleft', true);
?>