I'm having trouble placing endwhile in the proper place in a shortcode I made to grab data from a custom post type.
I'm using ACF (advanced custom fields) for custom fields.
As of now, the code doesn't fully work and I suspect it has to do with no 'endwhile' , 'endif' , or 'wp_reset_query()' (though I'm new to php).
Here's the shortcode I have for this:
// Add Shortcode
function custom_shortcode() {
// find todays date
$date = date('Ymd');
// args
$args = array(
'post_type' => 'events',
'meta_key' => 'event_date',
'meta_value' => '$date'
);
// query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$out .= '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$out .= "<div>";
$out .= get_field('event_name') . "<br>";
$out .='<img class="myimage" style="max-width:180px;" src='. get_field('event_image') . ">" ;
$out .= " <br>";
$out .= '<div class="date">' . get_field('event_date') . "</div>" . "<br>";
$out .= '<div class="time">' . get_field('event_time') . "</div>" ;
$out .= get_field('buy_ticket_button') . "<br>";
$out .= "</div>";
}
}
return $out;
}
add_shortcode( 'todaysevent', 'custom_shortcode' );
'$date'
in the$args
array. The syntax otherwise seems fine. Would you please explain 'doesn't work fully'? What are you expecting to happen? What does happen?wp_reset_postdata()
before the closing brace of theif
statement. From there, divide and conquer. Debug the value of$date
. Is it in the same format as what's saved in the custom fields? BTW, you don't needendif
orendwhile
because theif
andwhile
loops are using regular{}
brackets, and not the alternateif :
endif;
&while :
endwhile;
syntax.while( true ): endwhile;
or like this:while( true ) { }
, you've done the latter, and so you don't need to putendwhile;
anywhere, just look at the matching bracessrc
attribute in theimg
tag is missing quotes around the value.