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/
Related
I'm having trouble finding documentation for displaying the relationship fields between two custom post types.
Essentially I have a stores post type that holds store names and images. Then I have a product post type where each product will have fields where you can select what stores it is available at and a section for a link to the product within that store.
Setup:
I have two custom post types 1: Stores, 2: Products
The fields for "products" are
[repeater] product_stores
[sub field] store_name (RELATIONSHIP)
[sub field] store_link (URL)
The fields for "stores" are
title (WP DEFAULT TITLE)
image (IMAGE)
I'm able to display the custom field type for the retailer link but having trouble pulling the retailer name and image into the page.
What I have so far
<?php
if( have_rows('product_stores') ): ?>
<?php while( have_rows('product_stores') ): the_row(); ?>
<?php the_sub_field('store_link'); ?>
<?php endwhile; ?>
<?php endif; ?>
You don't need to use a Repeater field with a Relationship field inside it - just use a single Relationship field. The field will return an array of post objects which you can pull the title and link from.
<?php
$stores = get_field('product_stores'); // your Relationship field
if( $stores ) {
foreach( $stores as $post) {
setup_postdata($post);
the_title();
the_permalink(); // pull whatever you need from the post.
}
wp_reset_postdata();
}
?>
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; ?>
I have several custom fields in every custom post type called "product", I grab those fields with get_field_objects() this way:
$fields = get_field_objects();
if ( !empty($fields) ){
echo "<dl class='clearfix'>";
foreach ( $fields as $field ){
if (empty($field['value'])) continue;
echo "<dt>".$field['label']."</dt><dd>".$field['value']."</dd>";
echo '<br class="clear">';
}
echo "</dl>";
}
My client wants to be able to sort the the fields by the menu order (so he can just drag and drop in the plugin area).
But I didn't see any ordering documentations here- http://www.advancedcustomfields.com/resources/get_field_objects/
Right know the order is- the field that create last is the last one.
Is there any good solution for this?
What you could do is to make use of the repeater type of acf.
That way, you client will be able to add as many fields as he require, and sort them like he want by drag and dropping them.
If you need to know the "type" of each field (to change the style for example), you could add a select field (in your repeater field) with all your types of field.
You would then have something like this :
And something like this to display the fields :
<?php $fields = get_field('fields'); ?>
<?php if ( !empty($fields) ) : ?>
<dl class='clearfix'>
<?php foreach ( $fields as $field ) : ?>
<dt><?= $field['label']; ?></dt><dd><?= $field['value']; ?></dd>
<br class="clear">
<?php endforeach; ?>
</dl>
<?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 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;
?>