Home Forums Front-end Issues Relationship Field – Unique layout per post type


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

    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!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.