WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. RichardMisencik
    Member
    Posted 5 months 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>
    </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.

Reply

You must log in to post.

About this Topic

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