Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Not sure if I'm going about this the right way but I've been stuck on this for a couple of days and it's killing me.

Got a custom post type called 'featured-work' and custom fields in it. These two custom fields are 'lightbox-url' and 'lightbox-subtitle'.

For every post ID, there are about 5 lightbox URL's and 5 lightbox subtitles. On the page, I print out the featured image and these are the subtitles are used as the 'title' html tag with the lightbox URL as the 'href' html tag - the first one must use the featured image though.

Here is my code:

<ul id="work-list">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    $title = current(explode(' ', get_the_title()));
    $post_thumbnail_id = get_post_thumbnail_id();
    $post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id);
    global $wpdb;
    $metatable = $wpdb->prefix."postmeta";
    $lightboxurl = $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM $metatable where post_id=%d AND meta_key='wpcf-lightbox-url'",$post_id));
    $lightboxtitle = $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM $metatable where post_id=%d AND meta_key='wpcf-lightbox-subtitle'",$post_id));
    foreach ($lightboxurl as $i => $lburl) {
        if ($i > 0) {
            echo '<a class="html5lightbox" data-group="' . $title . '" title="' . $lgd . '" href="' . $lburl->meta_value . '"></a>';
        else {
            echo '<a class="html5lightbox" data-group="' . $title . '" title="' . $lgd . '" href="' . $lburl->meta_value . '">' . '<img src="' . $post_thumbnail_url . '">' . '</a>';

    foreach ($lightboxtitle as $lbtitle) {
        $lgd = $lbtitle->meta_value;

    <?php endwhile; else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

I can obviously see the second foreach isn't doing anything. I need it to loop through both at the same time. There is an equal number of subtitles and URL's btw.

EDIT: I also know the title tag isn't printing anything at the moment because it's not in a loop, I've just been messing around with the code.

Any ideas welcome. I'm really stumped.

EDIT 2: Here is an example: http://www.enginegroup.com.au/work.php - It's the simple staticly coded version that I did. Have a look at the code and what I mean. Btw I didn't use a gallery plugin because I need it to spit out videos as well.

share|improve this question
Fixed it myself but not needing two foreach's but using a while loop and counting until $i is less than or equal to the count of lightboxurl and then printing out in the title html tag the $lightboxtitle[$i]->meta_value –  Jonathan Aug 5 '13 at 4:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.