List posts in a select tag and display the_content of the selected option (1 post)

  1. RichardMisencik
    Posted 2 years ago #

    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>

    (in PHP it might be like this, at least this is all that I can do)

    $menu_loop = new WP_Query(
            '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; ?>
    <?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.

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by RichardMisencik
  • This topic is not resolved
  • WordPress version: 3.8.1