Thank you for the response, John. For those that are looking for more details and how I eventually resolved it. I added it to a previous post regarding this issue.
Next post in relationship field
There are two posts online that will offer solutions. One in mentioned in that post and the one I eventually used with some minor changes. The second option can be found HERE, as linked above.
For those that come searching for answers facing a similar issue as me. It ended up being as simple as adding the below line before getting the current index.
$current_post_id = get_the_ID();
I’m running into the same problem but @ali_jafarian solution isn’t quite working perfectly for me. I have a CPT for artworks with custom taxonomy for artists. Then I have another CPT for artists. The artist pages are basically the the page title (artist name) and a relationship field where you add the artist’s artwork from the artworks CPT where you can also filter by the custom taxonomy (since there are 50+ artists and 100s of artworks).
Each artwork is its own page as mentioned. When you click on the artwork from an artist page, we want to have the option of going to the next or previous artwork based on the order displayed on the artist page as determined by the order you set from its relationship field.
There seems to be an issue with the following code. I’m note sure how $current_post_id applies to the array_search as it isn’t referencing anything anywhere else in the code. If I print_r $current_index nothing appears. Same goes for $prev_module and $next_module. However, if I print_r $module_ids the array appears as it should in the order I set on the Artist page in the Relationship field. $first_module and $last_module show the correct IDs too. I just can’t seem to get the current post ID to then determine next and previous.
// create empty array for module ids
$module_ids = array();
// loop through modules and add them to array
foreach( $modules as $module ) :
$module_ids[] = $module->ID;
endforeach;
// get the current index
$current_index = array_search( $current_post_id, $module_ids );
// find the prev/next items
$prev_module = $current_index - 1;
$next_module = $current_index + 1;
// find first and last modules
$first_module = $module_ids[0];
$last_module = end($module_ids);
I also feel like ACF Relationship Field Prev/Next Buttons would work but I get stuck on the first part in how to pull from all artist pages or the single-artist.php template as opposed to just the page ID in…
<?php
// This should be the ID of the page the relationship field is set up on,
// NOT this page’s ID
$page_id = 5;
// Get the projects from the relationship field
$project_listing = get_field('project_listing', $page_id);
I’ve been able to get the below code to work but it selects the last instance of that particular sub field. When creating a post, the user will post content using Flexible content fields for blog_post_text, blog_post_images, and blog_post_gallery. Each post can have multiple. For instance, add one text sub field for the intro, then add an image sub field, then another text sub field, so on and so on. When I use this code, it creates the excerpt, limits it to 100 words, but selects the text from the last blog_post_text sub field added instead of the first.
add_filter('the_excerpt', 'your_function_name');
function your_function_name($excerpt) {
if( have_rows('blog_post_content') ):
while ( have_rows('blog_post_content') ) : the_row();
if( get_row_layout() == 'blog_post_text_content' ):
$my_acf_field = wp_trim_words(get_sub_field('blog_post_text'), 100);
endif;
endwhile;
else:
endif;
return $my_acf_field . '' . $excerpt;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.