Home › Forums › Front-end Issues › Relationship Field – Unique layout per post type
I’m using the relationship-field to choose items to show on a page. I have different custom post types (i.e. ‘Reizen’ and ‘Accommodaties’) which I want to have different layouts on the front-end.
So I’m trying to setup a way to just choose whatever post you like to show on the page and then check if the post is a certain post type or category.
This is what I have so far. It works for categories, but I can’t get it to work for post types. If I just use the conditional is_singular it works, but when I define which singular I get no output.
<?php foreach( $featured as $featured_post ):
$featured_id = $featured_post->ID;
if ( is_singular( ) ) { ?>
// this works perfectly ...
<?php }
if ( is_singular( 'reizen' ) ) { ?>
// this gives nothing ...
<?php }
if(has_category('uitbreidingen', $featured_id)) { ?>
// this works perfectly
.....
<?php }
if( is_singular( 'accommodaties' ) ) { ?>
// this does nothing
.....
<?php } endforeach; ?>
Just to clarify, because I need different markups for the different post types I can’t get away with just using is_singular. I’m relatively new to PHP so I’m probably overlooking something. What am I missing here? Thank you!
Because you are not in a loop that includes while (have_posts) { the_post()...
WP is looking at the post type of the post in “The Loop”, the current page, and not the post referenced by$featured_post
. This post type will not match.
You either need to use a loop that set the global $post as explained in the documentation for relationship fields https://www.advancedcustomfields.com/resources/relationship/
Or you need to use a different method for testing the post type, like this:
if ($featured_post->post_type == 'reizen') {
...
I take back the first part of my last comment. You cannot fix this by using a loop. I looked into this further for another topic. is_singular()
does not look at the current posts, it looks and the main/global $wp_query.
You must use my second option
Aaah thank you so much. I tried that option earlier, but I tried it using ‘$featured_post->ID’ which didn’t work. Never thought of just using $featured_post. This solved it!
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.