Wordpress acf repeater get first and second images only - php

I'm using acf plugin on a wordpress site. I am using a repeater field with an image subfield that is displayed on a blogpost. My goal is to only get and display the first and second images from a blog post to be displayed for the homepage.
I tried using this code from the acf site documentation but to no avail the code is not working. Can someone know the issue?
<?php while ($the_query -> have_posts()) : $the_query ->
the_post();
$postqID = get_the_ID();
?>
<?php
$rows = get_field('post_images', $postqID ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['post_image' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />

I just solved this issue. For those who are also having issues on displaying certain number of rows of images on a repeater field. You can refer my answer.
The code below returns two images from a repeater field. Just changed the condition ($i>1), if you wish to return certain number of image.
<?php
$i = 0;
if(get_field('post_images', $postqID)):
?>
<?php while (has_sub_field('post_images',$postqID)): ?>
<img src="<?php the_sub_field('post_image')['url']; ?>"></img>
<?php
$i++;
if($i > 1)
{
break;
}
?>
<?php endwhile; ?>
<?php endif; ?>

Related

Display whole fields from group (file + ACF)

I made the ACF plugin group with files to download. In group I have fields "File 1", "File 2"...etc.
I would like to display all attached files to page. It is possible to display all fields belong to group? I try with basic code, but in this case I have only 1 file.
How can I add iteration to this or display all fields?
<?php
$file = get_field('attachment_1');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<ul>
<li><a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<ul>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
As all your fields are set up individually, it isn't just a matter of looping through an array of all your fields of the same type (i.e. just your file fields).
There are a few ways that might work for you:
Option 1.
If all the field names for your files follow the same naming pattern and are named sequentially, you could loop using the name.
Example, assuming your fields are named attachment_1 up to attachment_5:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
// display file details as appropriate
}
}
Option 2.
If the file field names do not follow the same pattern, you could loop through an array of the field names.
Example:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
// Create an array with the field names of all your files
// N.B. This also lets you specify the order to process them
$file_fieldnames = array('file_1', 'file2', 'another_file');
foreach ($file_fieldnames as $fieldname) {
$file = get_field($fieldname);
if( $file ){
// display file details as appropriate
}
}
Option 3. If you want to loop through ALL fields on the post/page, you can save the fields into an array.
This might seem like the most generic approach at first, but it is complicated by the fact that you don't know what type each field is in order to know how to process and display them... you first have to work out what field type it is. You could do this by name (similar to above) or you could try to identify what each field by checking the field content.
Note, checking the field content is very risky, as there are other field types that can have similar featured (e.g. a file is not the only type that can have a url) so I wouldn't advise that strategy unless you are 100% sure you'll never change the field group or add another field group to the post/page.
Example:
$fields = get_fields();
foreach ($fields as $fieldname => $content) {
if (is_string ($content)){
// display the string
}
else if (is_array($content) && $content['url']) {
// then you could assume its a file and display as appropriate
}
}
Note that none of this code is tested. However it should give you an idea of the logic behind each option so you can decide what works for you.
UPDATE based on new code provided:
See below based on the code in your JSFiddle. I've ignored the caption outside the file list because it makes no sense - every file can have its own caption.
<?php
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// now display this file INSIDE the loop so that each one gets displayed:
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php endif; ?>
</li>
<?php
} // end if
} // end for loop
?>
<ul>
If you understand arrays, I'd suggest you add the file details into an array and then do a second loop to display the files... however I'm guessing you're not that proficient with basic coding constructs as you don't understand loops, so I've tried to keep it simple. I strongly recommend that you do some tutorials on programming basics if you are attempting to write code.

How to : Populate Content of Page to another page in WordPress

I am using OptionTree for WordPress option panel, I am getting the page ID, and using that page ID I want to populate the content of the page to another page. Here is my code:
<?php
$test_input = ot_get_option( 'for_myapp_features' );
?>
<?php $the_query = new WP_Query( 'page_id=$test_input' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile;?>
Any help would be helpful.
You can use get_post_field and get_the_title() to get
content and title.
$test_input = ot_get_option('for_myapp_features');
echo get_post_field('post_title', $test_input); // to get the title
//or
//echo get_the_title($test_input); // to get the title
echo get_post_field('post_content', $test_input); //to get the content
Hope this helps!
If you want to display the content for that page, then add the_content() within your while loop. the_content() will display the page’s content. I would also add a query reset, wp_reset_query();, after the endwhile to restores the global post data to the original query.

Post excerpt doesnt work

Hello stackoverflow people, I need help. I'm using wordpress and somehow I can't use this function:
$post_id = 12;
echo get_post($post_id)->post_excerpt;
Somehow it prints nothing. Here is the full div. Can you help me to solve this problem?
<div id="block1">
<div class="inblock1">
<h2>About boots</h2>
<p><?php $post_id = 12;
echo get_post($post_id)->post_excerpt; ?> </p>
apac
</div>
</div>
It sounds like you don't actually have an excerpt set for this post. You can always use a conditional to test it, and output a custom excerpt (from the post_content) if one doesn't exist:
$my_post = get_post($post_id);
// If the excerpt is empty, generate one from post_content, else display the saved excerpt
echo empty($my_post->post_excerpt) ? wp_trim_words($my_post->post_content, 55, '...') : $my_post->post_excerpt;
Read more about wp_trim_words() in the Codex.
You may need to use setup_postdata() because <?php $post_id = 12; echo get_post($post_id)->post_excerpt; ?> will return excerpt if you have excerpt data in excerpt field but below code will return data from content if you don't have data in excerpt field.
$post_id = 12;
$tempVar = $post;
$post = get_post($post_id);
setup_postdata($post);
the_excerpt();
wp_reset_postdata();
$post = $tempVar;

Switch not working within while loop (Wordpress PHP)

I've tried what I could on this and am still stuck, so I'm looking for some help. I'm sure there's something small I'm overlooking or am not aware of, so I'd be grateful for another set of eyes to take a look!
I'm trying to use a switch within a Wordpress while loop to set dimensions on post thumbnails for specific posts. The switch uses an auto-incrementing value ($count). Inside the loop, $count will return the right number for div ID's, but it will not work with the switch. All of the thumbnails go to the size defined before the loop begins (see $thumbsize)
Here's the code:
// Setup loop to pull only posts tagged slider
$max = 6;
$args = array('tag' => 'slider','posts_per_page' => $max);
$featuredPosts = new WP_Query();
$featuredPosts->query($args);
// Defaults for post thumbnail display
$thumbargs = array('class' => 'featured-blocks-img');
$thumbsize = array(640,360);
$count = 0;
// Begin loop
if ($featuredPosts->have_posts()) : while ($featuredPosts->have_posts()) : $featuredPosts->the_post();
$count++;
// Get post category and format for div class name
$category = get_the_category();
$catname = $category[0]->cat_name;
$catdash = 'cat-';
$catdash .= str_replace(' ', '-', $category[0]->cat_name);
$catdash = strtolower($catdash);
// Change post thumbnail size conditionally
switch ($count) {
case 2:
case 5:
case 6:
$thumbsize == array(320,260);
break;
default:
$thumbsize == array(640,360);
} // End switch
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div id="home-featured-post-<?php echo $count;?>" class="featured-blocks-post <?php echo $catdash; ?>">
<h2 class="home-featured-title"><?php the_title(); ?></h1>
<?php the_post_thumbnail($thumbsize, $thumbargs); ?>
</div>
</a>
<?php
endwhile;
endif; // End loop
And here it is in Gist form if that's helpful to anyone: https://gist.github.com/anonymous/8984741
I tried to add comments that would provide some context.
Any ideas of what's happening? I can provide the resulting HTML source if that would help also.
Looks like you aren't actually setting $thumbsize in the below code
$thumbsize == array(320,260);
== is comparing $thumbsize to that array, not creating an array with those values.
You really want it to just look like this:
$thumbsize = array(320,260);

Total image widths in while

I have some php code which echos the width of a specific image size in WordPress. Currently, if the image width is greater than 80, then it echos "frog"...what I would like it to do is count all of the image widths within my while and if the total of those images are greater than 600 (hypothetical number) then echo "frog". The code I am using looks like (I am using this code within my while):
<?php
$image = wp_get_attachment_image_src (get_post_thumbnail_id($post_id), 'gallery-thumbnail');
list($width) = getimagesize($image[0]);
echo $width;
if( $width > 80 ) {
echo "frog";
}
?>
My while is the basic WordPress standard:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- some code here -->
<?php endwhile; else: ?>
<!-- some code here -->
<?php endif; ?>
Any ideas?
Thanks, Josh
Part pseudo code/part solution:
$sumOfWidths = 0;
foreach($images as $image)
{
$sumOfWidths = $sumOfWidths + $image['width'];
}
if($sumOfWidths>600)
{
echo 'frog';
}
Simply put the above code loops through each image and adds the image width to the $sumOfWidths variable.
After the foreach loop, has completed, there should be a number in the $sumOfWidths which you can check and then undertake your logic as you see fit.
Have a look at this code snippet to get you started

Categories