List posts in a select tag and display the_content of the selected option
-
I would like to create a select tag with posts from my custom post type and then, when an option is selected, I need to get the_content() from that selected post by its ID and display the content below.
Esentially it would look like this:
<select name="menu" id="menu"> <option value="0">Please choose a food menu:</option> // random IDs <option value="5">Food menu 1</option> <option value="11">Food menu 2</option> <option value="19">Food menu 3</option> </select>
(in PHP it might be like this, at least this is all that I can do)
$menu_loop = new WP_Query( array( 'post_type' => 'menu', 'posts_per_page' => -1 ) ); if ($menu_loop->have_posts()) { ?> <select name="menu" id="menu"> <option value="0">Please choose a food menu:</option> <?php while ($menu_loop->have_posts()) : $menu_loop->the_post(); $the_id = get_the_ID(); ?> <option value="<?php echo $the_id; ?>"><?php the_title(); ?></option> <?php endwhile; ?> </select> <?php } ?>
And then something like:
// get content by ID of the selected option <?php the_content($post->ID); ?>
I know its much harder than what I wrote above, but I just dont know how to do this, any help is appreciated, Thank you.
- The topic ‘List posts in a select tag and display the_content of the selected option’ is closed to new replies.