Link to relationship post - php

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

Related

Wordpress ACF Repeater sub fields

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/

ACF Flexible Content - Not showing anything when taking the data from author page

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.

Wordpress query_posts displayed by category custom field

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

ACF: Display Fields in a template based on Cpt ui

I have created a custom post type based on CPT Ui I am tryin to display the items of that in a template ideally what I want to be able to do is
www.siteurl.com/profile/profilename
Hence why I have created a template page with the following code.
But I dont no how I would get profilename into my query posts and then display the relivent data this is what I have tired so far to get id and permalink to display but to no avail.
I really want to be able to do it like how the documentation states. Ideally i would suspect my best approach would be to check if user has created a profile in the front end if not direct them to do so?.
Display a field
<p><?php the_field('field_name'); ?></p>
http://www.advancedcustomfields.com/resources/code-examples/
<?php
/**
* Template Name: Profile Page
*/
get_header(); ?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => 'david'
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo '<li>' . get_the_title($post->ID) . '</li>';
}
echo '</ul>';
}
?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
If anyone has done anything simlar maybe I should be using current user but again i dont no how to link that up to acf as I am new to that plugin
Perhaps you could try another approach and build a custom query with WP_Query instead. The example bellow use the WP_Query object rather than the get_posts function, however the arguments and logic remain the same.
This implies we will find all posts that have a post_type of profiles where the ’custom field’ profilename is equal to david. The custom field ‘profilename’ in this case could be a text field, a radio button or a select field (something that saves a single text value).
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h1>__('Listing Posts by:', 'text_domain');</h1>
<?php echo '<p class="text-center">' . esc_html( $user ) . '</p>'?>;
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => $user // retrieve the actual profilename field value
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
The code above is an example of how to query ("get and display") posts in WP using WP_Query object within a template based on custom field called ‘profilename’ which has a value of ‘david’ or whatever data is saved inside the field as per your example.
Perhaps you want to display the relevant data (values) of the custom fields you've created for the WP default user profile page, and if this is the case, you should follow another approach and instead of query posts you simply output the fields a user might fill on their profile. In order to that you could Get values from a user.
This example will display the field 'profilename' value from a user with an ID of 1.
<?php the_field('field_name', 'user_1'); ?>
Within your loop you could use $user_ID = get_current_user_id(); to retrieve the user_id as follows:
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
$user_ID = get_current_user_id();
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h5>__('Listing Posts by:', 'text_domain');</h5>
<h1><?php the_field('profilename', '$user_ID'); ?>;</h1> // this will retrieve the data/value saved in the 'profilename' field for the current user
...
Check ACF's Query Posts by Custom Fields documentation for other ways to retrieve an array of post objects from the database using native WP functions.

Create dynamic HTML ID based off wordpress query

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

Categories