Skip Navigation

[Resolved] Loading a section title if related posts exist

This support ticket is created 2 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by gabrielB 2 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2329479

Hi,

I have a website for a training academy. I have a Lecturer CPT and a Course CPT. In the single-course.php file, I am loading Lecturers related to that Course.

Course with related Lecturer: hidden link
Course without related Lecturer, still showing title: hidden link

Here's my feeble attempt:

<?PHP
$course_id = get_the_ID();
$related_lecturers = toolset_get_related_posts(
$course_id, // get posts related to this one
'course-lecturer', // relationship between the posts
'parent', // get posts where $writer is the parent in given relationship
100, 0, // pagination
array(), // How was his surname, again…?
'post_object',
'child'
);
?>

<?php if( $related_lecturers != '' ) { ?>
<section id="course-lecturer" class="inner-spaced-top appear-1 generic-grid">
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="cell medium-6">
<h4 class="section-title">Lecturers</h4>
<div class="grid-x grid-margin-x archive-grid flex-container generic-grid-inner">
<?php } else {} ?>

#2329607

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Gabriel,

Thank you for getting in touch.

Ideally we would like to test each step of the way. Given that your algorithm is heavily dependent on the $related_lecturers value being correct my advice here is to dump the value of that field on the page to ensure that you're getting the correct value.

Add this to your code to see what the $related_lecturers is returning.


var_dump($related_lecturers);


However I believe the correct way to structure the relationship function is like below.

$writer = toolset_get_related_post( $book_id, array( 'writer', 'book' ) );

Its perhaps a bit more readable in this context.

Thanks,
Shane

#2329991

Hi Shane,

Thanks for getting back to me.

I don't think I explained myself well. The code I have is working.

I just want to be able to display a title for this section depending on if there are related posts or not. So basically, if there are posts, I want to display the following, before the grid of related posts.

<h4 class="section-title">Lecturers</h4>

Do you think you can help?

#2330241

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Gabriel,

Then you should be able to do this.

<?php if( !empty($related_lecturers) ) { ?>
<section id="course-lecturer" class="inner-spaced-top appear-1 generic-grid">
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="cell medium-6">
<h4 class="section-title">Lecturers</h4>
<div class="grid-x grid-margin-x archive-grid flex-container generic-grid-inner">
<?php } else {} ?>

I'm using the empty() function to check if the field is empty. However i'm negating the function with the "!" symbol which essentially reads If Not Empty. Adding it to the conditional will only show the items in the conditional field only if the value isn't empty.

Thanks,
Shane

#2334973

That's exactly what I needed, thank you Shane!

#2334975

My issue is resolved now. Thank you!