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: enlace oculto
Course without related Lecturer, still showing title: enlace oculto
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 {} ?>
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
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?
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
That's exactly what I needed, thank you Shane!
My issue is resolved now. Thank you!