Ok... back to my PHP/Wordpress adventure.
In my testimonials post type I have added 5 custom fields and 2 fields are not mandatory which means they can be empty.
One of them is the url field which I want to output within my while loop with a different HTML when it's empty (wrap it in a span
tag instead of an an a
tag), but I'm not sure exactly how to do this. I did a google search and found possible answers, but I want to know if I'm on the righ track with this or not.
I was thinking of something like this:
function getTestimonials($numberOfTestimonials) {
global $post;
$testimonials = new WP_Query();
$testimonials->query('post_type=testimonials&posts_per_page=' . $numberOfTestimonials );
$url = 0;
if ($testimonials->found_posts > 0) {
echo '<div class="slider">';
echo '<ul>';
while ($testimonials->have_posts()) {
$testimonials->the_post();
$listTestimonial = '<li>';
$listTestimonial .= '<h2>' . get_field('workshop') . '</h2>';
$listTestimonial .= '<blockquote>';
$listTestimonial .= '<p>' . get_field('testimonial') . '</p>';
$listTestimonial .= '<footer>';
$listTestimonial .= '<cite>';
if (empty($url)) {
$listTestimonial .= '<span>' . get_field('name') . ' - ' . get_field('company-agency') . '</span>';
} else {
$listTestimonial .= '<a href="' . get_field('url') . '">' . get_field('name') . ' - ' . get_field('company-agency') . '</a>';
}
$listTestimonial .= '</cite>';
$listTestimonial .= '</footer>';
$listTestimonial .= '</blockquote>';
$listTestimonial .= '</li>';
echo $listTestimonial;
}
echo '</ul>';
echo '</div>';
wp_reset_postdata();
} else {
echo '<p>No testimonial found</p>';
}
}
Where do I check if the url field is empty? Probably in my database, but how (MySQL... I never go there :) )?