簡體   English   中英

在選擇標簽中列出帖子並顯示所選選項的_內容

[英]List posts in a select tag and display the_content of the selected option

我試圖用所有帖子標題創建一個select標簽,然后,當選擇一個選項時,我需要從該選中帖子中通過其ID獲取the_content()並在下面顯示內容。

基本上看起來像這樣:

<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>

(在PHP中可能是這樣,至少這是我能做的全部)

$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 } ?>

然后類似:

// get content by ID of the selected option
<?php the_content($post->ID); ?>

我比上面寫的要難得多,但是我只是不知道該怎么做。

周圍的東西

$postid = 12;
$content_post = get_post($postid);
$content = $content_post->post_content;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM