0

I have a custom post type for a simple schedule list and using the meta box plugin i have two meta boxes assigned to custom post type. 1 for an image and 1 for an array of date times .

enter image description here

In my functions.php file I have created a shortcode to retrieve and display the info. HOWEVER it is only showing the post once and where i display the date time it says "array". I want to show the post 3 times based on the fact that I have stored 3 date times with the meta box. I'm not sure how to adjust my query or loop to make the post show 3 times in order of date. Below is my shortcode from my functions.php file. Any help would be greatly appreciated.

// SCHEDULE LIST
add_shortcode( 'schedule', 'display_schedule' );

function display_schedule(){
    global $paged;

    $args = array(
        'post_type' => 'tbschedule',
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 10
    );

    $string = '';
    $query = new WP_Query( $args );

    if( $query->have_posts() ){

        $string .= '<span class="schedules">';
        while( $query->have_posts() ){

            $query->the_post();
            $schedule_image = "";
            $schedule_image_url = "";
            $images = rwmb_meta( 'tbf_scheduleimage', 'type=image&size=full');

            foreach ( $images as $image ){ 
                $schedule_image = "<img src='{$image['url']}' class='first' alt='{$image['alt']}' title='{$image['title']}' />";
                $schedule_image_url = $image['url'];
            } 

            if($schedule_image_url == ""){

                $string .= '<div class="schedule"><span class="scheduletitle"><a href="'.get_permalink().'">' . get_the_title() . '<span class="scheduleDate">schedule date: '.rwmb_meta('tbf_scheduledatetime').'</span></a></span></div>';

            }else{

                $string .= '<div class="schedule">
                <a href="'.get_permalink().'"><span class="scheduleimage" style="background-image: url('. $schedule_image_url .');background-repeat: no-repeat;background-position: center center;background-size: contain;"></span></a>
                <span class="newstitle"><p style="height: 122px;"><a href="'.get_permalink().'">' . get_the_title() . '<span class="scheduleDate">schedule date: '.rwmb_meta('tbf_scheduledatetime').'</span></a></p></span> 
                </div>';

            }
        }
    }

    $string .=  '</span>';

    // Paging      
    $q = $query;
    $big = 999999999; // need an unlikely integer
    $string .= '<p>&nbsp;</p>'.paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $q->max_num_pages //$q is your custom query
        ) );       

    wp_reset_postdata();

    return $string;
}
// END SCHEDULE LIST

2 Answers 2

0

I don't know what rwmb_meta() does. But if rwmb_meta('tbf_scheduledatetime') returns array, then try something like this:

...
$images = rwmb_meta( 'tbf_scheduleimage', 'type=image&size=full');
$dates = rwmb_meta('tbf_scheduledatetime');
var_dump( $dates );
... 

You'll probably want to loop thru $dates and repeat the post data for each element in $dates.

0

I also don't understand what rwmb_meta() does, but I can see a couple obvious problems in the code as written.

Apparently, you want the foreach to deliver as many "posts" as it finds $image in $images, and echo a result each time. If so, you would remove the bracket from:

$schedule_image_url = $image['url'];
            } 

and place it after each $image item being added to $string:

foreach ( $images as $image ){ 
    $schedule_image = "<img src='{$image['url']}' class='first' alt='{$image['alt']}' title='{$image['title']}' />";
    $schedule_image_url = $image['url'];
// } removing the bracket here

    if($schedule_image_url == ""){

        $string .= '<div class="schedule"><span class="scheduletitle"><a href="'.get_permalink().'">' . get_the_title() . '<span class="scheduleDate">schedule date: '.rwmb_meta('tbf_scheduledatetime').'</span></a></span></div>';

     } else {

       $string .= '<div class="schedule">
            <a href="'.get_permalink().'"><span class="scheduleimage" style="background-image: url('. $schedule_image_url .');background-repeat: no-repeat;background-position: center center;background-size: contain;"></span></a>
            <span class="newstitle"><p style="height: 122px;"><a href="'.get_permalink().'">' . get_the_title() . '<span class="scheduleDate">schedule date: '.rwmb_meta('tbf_scheduledatetime').'</span></a></p></span> 
            </div>';

     }
} //bracket added here means for each $image, you get one of the above two depending on whether $schedule_image_url is found

As for what's going on with rwmb_meta('tbf_scheduledatetime'), if it "just says array" when you try to output it, then that would be because it's an array, as I think shanebp is getting at. Without, like shanebp, knowing how it's set up, I have to guess how to access its content, but, if tbf_sheduledatetime is a simple sub-array with keys [0], [1] etc., then you could use an incrementing variable. So, when you're initializing variables prior to the foreach:

$i = 0;
$datetime_array = rwmb_meta('tbf_scheduledatetime');

Then inside the foreach loop (as above), the $string line would read:

$string .= '<div class="schedule"><span class="scheduletitle"><a href="'.get_permalink().'">' . get_the_title() . '<span class="scheduleDate">schedule date: '. $datetime_array[$i] .'</span></a></span></div>';
$i++;

so, the first time it finds $datetime_array['0'] and the next time $datetime_array['1'], etc.

Hope that helps, but offered without high confidence it's really what you're looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.