I was wondering how I can create a dynamic set of IDs based off the amount of custom posts that are queried for the given post.
I'm using the Advance Custom Fields plugin and then I"m querying the custom fields in the given post. If you take a look below you'll see I have my custom fields being queried each one is wrapped in a div with an id "section-1". What I need is for the "section-1" to update to "section-3", "section-4" each time a new field name is queried. So if 5 fields are queried they each have their own ID.
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
<div id="section-1">
the_sub_field('sub_field_name');
</div>
endwhile;
else :
// no rows found
endif;
?>
Just set an index variable before the loop and increment it at each iteration. Use that inside your id.
<?php
$index = 1;
while ( have_rows('repeater_field_name') ) : the_row(); ?>
<div id="section-<?= $index; ?>">
<?php the_sub_field('sub_field_name'); ?>
</div>
<?php $index++
endwhile; ?>
Try this
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
$i = 0;
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
<div id="section-<?php echo ++$i; ?>">
the_sub_field('sub_field_name');
</div>
endwhile;
else :
// no rows found
endif;
?>
Related
I am having an issue in Wordpress with ACF Repeater sub-fields link object not returning href on front-end. Basically the link in the front-end does not output any links set in WP admin. Text output is working.
Repeater fields:
Field label = Link to doc
Field name = link_to_doc
Field type = Repeater
Repeater sub-fields 1:
Field label = Link
Field name = link
Field type = Link
Repeater sub-fields 2:
Field label = Text of link
Field name = text_of_link
Field type = Text
PHP code:
<?php if ($section['link_to_doc']) : ?>
<?php foreach ($section['link_to_doc'] as $link) : ?>
<div>
<a href="<?= $link['link'] ?>" class="btn-txt">
<?= $link['text_of_link'] ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Could anyone check the php code and let me know what's wrong?
This topic has no answer for a while, you've probably found a solution. But now ACF is explaining how to access the data for nested repeaters directly in the documentation.
<?php
// Loop over the first repeater
if( have_rows('type') ):
while( have_rows('type') ) : the_row();
// Loop over sub repeater rows.
if( have_rows('type') ):
while( have_rows('type') ) : the_row();
// Get sub value.
$child_title = get_sub_field('name');
endwhile;
endif;
endwhile;
endif;
There is also other ways to access this kind of data, here's the list:
https://www.advancedcustomfields.com/resources/repeater/
The content doesn't want to display. I have read and went through the documentation many times. I even asked other developers to see and check the code, and it looked correct for them.
I need this to be on the Author page. I have tested this on a custom post type, a page etc... and it worked. However, this info must come from the author page.
Here are my fields:
Here is where the fields are displayed:
And below is my code. It always displays nothing. Why is that? I have version 5.6.1
```
// check if the flexible content field has rows of data
if( have_rows('social_media') ):
// loop through the rows of data
while ( have_rows('social_media') ) : the_row();
if( get_row_layout() == 'social_media_icons' ):
echo the_sub_field('media_facebook');
endif;
endwhile;
else :
echo "nothing";// no layouts found
endif;
?>
```
Try This:
$page_id = //your page id;
// check if the flexible content field has rows of data
if( have_rows('social_media',$page_id) ):
// loop through the rows of data
while ( have_rows('social_media',$page_id) ) : the_row($page_id);
if( get_row_layout() == 'social_media_icons' ):
echo the_sub_field('media_facebook');
endif;
endwhile;
else :
echo "nothing";// no layouts found
endif;
?>
Remove echo from this line the_sub_field('media_facebook');
// check if the flexible content field has rows of data
if( have_rows('social_media') ):
// loop through the rows of data
while ( have_rows('social_media') ) : the_row();
if( get_row_layout() == 'social_media_icons' ):
the_sub_field('media_facebook');
endif;
endwhile;
else :
echo 'nothing'; // no layouts found
endif;
I have figured it out.
It's an Author page. There are many Authors and each of them is unique.
If we try to pull the field, and we have say 10authors, what field will it pull? A standard loop won't work here.
We need to pass the author id. user_1.
Now, that's static, we want it dynamic, so we can get the author id, and then concatinate it to the user_$id.
Such as:
$author_id = get_the_author_meta('ID');
while ( have_posts() ) : the_post();
if( have_rows('social_media','user_'. $author_id) ):
while ( have_rows('social_media', 'user_'. $author_id) ) : the_row();
if( get_row_layout() == 'social_media_icons' ): ?>
<?php endif; ?>
<?php endwhile;
else :
echo 'no content';
endif;
endwhile; // End of the loop.
This will work, because the ID now is dynamic, and we are getting the current user ID and then concatenating it to the 'user_'.
That code there works 100% as of version 5.6.5, and should work in further as well.
I am working on a WordPress website that has installed Advanced Custom Fields. One of the pages is showing a release by a band. I have created a custom post type for releases and one for lyrics. The release has a flexible content field called 'tracklist'. This field has a layout called 'tracks' with a row with two fields called 'trackname' (text), and 'lyrics' (relationship). 'lyrics' has been set to max one post, so you can only choose the lyrics for that specific song.
How am I able to get the link from this relationship field called 'lyrics'?
Here's the code I have so far, but of course $lyrics contains an array, so it won't work:
// check if the flexible content field has rows of data
if( have_rows('tracklist') ):
$x = 1;
echo '<p class="tracklist">';
// loop through the rows of data
while ( have_rows('tracklist') ) : the_row();
if( get_row_layout() == 'tracks' ):
$trackname = get_sub_field('trackname');
$lyrics = get_sub_field('lyrics');
echo '<strong>'.$x.'</strong> '.$trackname.' (lyrics)<br />';
endif;
$x++;
endwhile;
echo '</p>';
endif;
From the website of the plugin, I found this code. But is there an easier way as there only are one relationship?
<?php
$posts = get_field('relationship_field_name');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
<li>
<?php echo get_the_title( $p->ID ); ?>
<span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Have wordpress based site, where:
MainCategory
Subcategory(custom-field value = custom1)
Subcategory2(custom-field value = custom2)
Subcategory3(custom-field value = custom3)
MainCategory2
Subcategory(custom-field value = custom1)
Subcategory2(custom-field value = custom2)
Subcategory3(custom-field value = custom3)
MainCategory3
Subcategory(custom-field value = custom1)
Subcategory2(custom-field value = custom2)
Subcategory3(custom-field value = custom3)
As you can see, all subacegories in main categories are the same. With same name (not slug), and there are custom fields with same field values.
I need to display posts that is in MainCategory2 AND subcategory has custom field with value custom2. Is this possible?
P.S. I use ACF plugin for custom fields.
I'm not sure if this is the best solution, but it will hopefully solve your problem.
Loop all posts of MainCategory2 (let's assume, this category has the ID 2)
Check if the content of the custom-field is equal custom2.
Define what should be looped (in this example it's the blogtitle and link)
The code would look like this:
<?php query_posts( 'showposts=20&cat=2&order=ASC' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if( get_field('custom-field') == 'custom2' ): ?>
<?php the_title(); ?><br />
<?php else : ?>
<?php endif; ?>
<?php endwhile; endif; ?>
I'm trying to display a food menu created via Advanced Custom Fields Pro, however when I try to display the information from the fields I get nowhere despite doing it a lot of times in the past. I've been going over the code a bunch of times but I can't find what's wrong with it..
My structure of the fields look like this:
The top image is what it looks like when creating the information. The other two is the name of the fields, which are days and the sub field is called days_day, I got 5 sub fields, one for each day of the week.
Then I try to display this info via this script:
<ul id="food-menu-content" class="clearfix no-bullet-list">
<?php
$args = array(
'post_type' => 'meny'
);
$menu_query = new WP_Query( $args );
?>
<?php if ( $menu_query->have_posts() ) : while ( $menu_query->have_posts() ) : $menu_query->the_post(); ?>
<?php if ( have_rows('days') ) : while ( have_rows('days') ) : the_row(); ?>
<?php if ( have_rows('days_day' ) ) : while ( have_rows( 'days_day' ) ) : the_row(); ?>
<li>
<h3><?php the_sub_field('days_day'); ?></h3>
</li>
<?php endwhile; endif; ?> <!-- have_rows('days_day') -->
<?php endwhile; endif; ?> <!-- have_rows('days') -->
<?php endwhile; endif; ?> <!-- $menu_query -->
</ul>
The fields are connected to a custom post type called "meny" which I run my own WP_Query on and then I try and access the information from the fields during the current iteration.
Problem with this script is that I only get one li from the loop and it's completely empty of information. When really I should get 5 since I have 5 sub fields (days_day).
What is wrong here?