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 :) )?

Recommended Answers

All 7 Replies

I've digged a bit further and I think I should do something like this:

$url = mysql_result($result,$i,"url");

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>';
}

But I still don't know how to get the right table entry/id from that url field in MySQL

I've found the table and 2 rows for that url (custom) field, but that's about it :) How do I check if an url field is empty on a custom post type testimonial page with this MySQL info.

meta_id post_id meta_key    meta_value
183     51      url
184     51      _url        field_5894f906149a5

I've marked it as "solved', but it's not !
PHP/MySQL... it's just not for me.
The more I read about it, the more confused I get, so I will keep on outsourcing this stuff :)

Ok 'unsolved' again :)

Thanks Cereal, but I had no luck with that. I tried it like this, but even the post that has an url field filled in gets wrapped in a span.

$url = get_field('url');

if (FALSE === $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>';
}

Ok 'solved' again, but this time for real :)

Cereal you've pointed me in the right direction, because I had to do it like this:

if (get_field('url')) {

    $listTestimonial .= '<a href="' . get_field('url') . '">' . get_field('name') . ' - ' . get_field('company-agency') . '</a>';

} else {

    $listTestimonial .= '<span>' . get_field('name') . ' - ' . get_field('company-agency') . '</span>';

}

It was as simple as that... after all :)

Great! I wrote the example basing on:

get_field returns false if (value == “” || value == null || value == false)

so I supposed it was returning boolean FALSE instead of mixed :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.