Wordpress ACF Repeater Field - Only pull in first 2 fields - wordpress

Topic kinda says it all. I need to only pull in the first two fields of an ACF repeater field.
Here is what I am trying to use but it is obviously not right:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
How am I supposed to do this?

<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
You are mixing apples and pears, WP_Query and ACF Repeater field. WP_Query returns the post data, whilst the ACF function have_rows( $repeater_field_name, $post_id ); checks whether there are any data in the custom field repeater that is on your page/post (or if you specify $post_id on the relevant post/page). More info at https://www.advancedcustomfields.com/resources/repeater/

<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>

Related

how to use Custom post type dynamically

I have created a new template page and I am displaying custom post type in that page as follows,
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'astroalbums',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<?php $link = get_permalink($post->ID); ?>
<?php the_title( '<h3 class="entry-title">', '</h3>' );?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
My custom post type is "astroalbums" and I want to use it dynamically. I have 4 custom post types. I want to create new page in dashboard and assign the above page template i have created. and each page will call different custom post type.
It will be really great help
Thank you,
Trupti
You're retrieving the posts correctly, but it's seems that the problem is inside the foreach loop. As the default WordPress loop is not being used, you need to call functions which receive the post id as parameter or use the properties present in the $post object (which is an instance of the WP_POST class) in order to display the data.
One possible solution:
<?php
$posts = get_posts([
'post_type' => 'astroalbums',
'posts_per_page' => 1
]);
?>
<?php foreach( $posts as $post ): ?>
<?php $link = get_permalink( $post->ID ); ?>
<h3 class="entry-title">
<a href="<?php echo esc_url( $link ); ?>" rel="bookmark">
<?php echo get_the_title( $post->ID ); ?>
</a>
</h3>
<a href="<?php echo esc_url( $link ); ?>">
<?php echo get_the_post_thumbnail( $post->ID ); ?>
</a>
<?php endforeach; ?>

ACF + Isotope: Filter Post Object Data

I'm trying to filter data from a custom post-type called "Clients", based on category. What I need to appear is the logo for each particular client.
I've set up a repeater field of Post Objects, so I can change the order that the logos will be displayed.
What I have currently works, however I cannot figure out how to incorporate the Post Object selector, so that what appears is determined by the instances I've added via the Repeater.
Here is the link to the site. Appreciate any answers!
See below for screenshot of my dashboard setup:
<ul id="filters">
<?php
$terms = get_terms("category", array(
'orderby' => 'slug'
)); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php $the_query = new WP_Query(array(
'post_type' => 'clients',
'posts_per_page' => '-1',
'order' => 'ASC'
)); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="isotope-list-container">
<div id="isotope-list" class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> portfolio columns"> <?php // 'portfolio' is used as an identifier (see Setp 5, line 6) ?>
<div class="portfolio-item-container">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</div> <!-- end portfolio item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
I was able to make this considerably simpler by using the Post Types Order plugin to reorder my posts.
https://wordpress.org/plugins/post-types-order/

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

Get a ACF field using get_pages()

I'm displaying a list of sibling pages using the below code. As well as retrieving the page title and link. I need to grab a custom field called excerpt and output it for each sibling item. How do I do this using get_pages? Currently the var_dump I'm using is grabbing the current pages excerpt, which is incorrect.
<?php
global $post;
if ( is_page() && $post->post_parent ) {
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$our_pages = get_pages('child_of='.$parent.'&exclude='.$post->ID);
$ex = get_field('excerpt', $our_pages);
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php var_dump($ex) ?>
<?php endforeach;
endif;
}
UPDATE
Ok so I have put in <?php print_r($page_item); ?> as suggested and it has returned the following:
I don't see any of my ACF fields here, so how do I retrieve those? What are my next steps?
You should be using the_field(), or echo get_field(), within your forloop, and should pass the post ID as the second parameter of the function:
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php the_field('excerpt', $page_item->ID); ?>
</a>
<?php endforeach;
endif;
You can read more about how get_field() works, in the documentation.
You full code like below:
<?php
global $post;
if ( is_page() && $post->post_parent ) {
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$our_pages = get_pages('child_of='.$parent.'&exclude='.$post->ID);
$ex = get_field('excerpt', $our_pages);
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php echo get_post_meta( $page_item->ID, 'excerpt', true); ?>
<?php endforeach;
endif;
}
If you didn't get the data like above then change you custom filed name with any other name and then try.
I've got the same problem with AFC and get_pages().
Try to access ACF fields by retrieving all posts by page type.
$args = array(
'post_type' => 'page',
'numberposts' => -1, // Accepts -1 for all. Default 5.
);
$pages = get_posts($args);
Now ACF get_field('excerpt'); function will work, because get_pages() doesn't return all needed data.
If you'd like to get specific pages like siblings you could pass theirs id's as get_posts() 'include' argument (arr).
Read more about get_posts() in documentation.

list custom fields from different custom post types

I have two different custom post types: "Movies" and "Press" with different custom fields.
Now I want to display both custom fields in the single.php.
home.php:
...
$query = array(
'post_type' => 'movies',
'posts_per_page' => 3,
);
$loop = new WP_Query( $query );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="movie_cover">
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
</div>
...
single.php:
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'single', 'film' ); ?>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
single-film.php:
...
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
...
the field 'cover' is from "Movies", but now i want to list the field 'release' from "Press".
Does I need a second loop?
if you add movie_id custom field to press, and set it to the ID of the movie
then you can do something like inside single-film.php
query_posts('meta_key=movie_id&meta_value=' . get_the_ID());
and do another loop for the reviews
while have_posts() ...
There's a lot of additional stuff but this should get you going in the
right direction.
I'd consider using get_posts instead of query_posts to not override wordpress' main query loop, but then you'd have to do your own manual loop:
$posts = get_posts('meta_key=movie_id&meta_value=' . get_the_ID());
foreach ($posts as $p) {
echo $p->post_title;
...
}

Resources