如何使用get_posts()和表单选择

时间:2012-07-02 23:02:03

标签: wordpress plugins

大家好,我是wp插件开发的新手。到目前为止,我做得很好,除了这个形式选择片似乎踢我的头。我正在尝试在选择字段中显示最近发布的列表供管理员选择并执行一些操作,但标题不会显示。我在单选按钮甚至文本字段中尝试了这个并且没有结果。请问,我在这种形式中缺少什么?

<form method="post"><select name="Article">
$args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );     foreach ($postslist as $post) : setup_postdata($post);

echo'<option value='".the_ID()."'>'".the_title()."'</option>';
endforeach;
echo"</select></form>";

2 个答案:

答案 0 :(得分:0)

我现在无法测试,但有几种可能性。

首先,the_ID()将立即回显该值 - 它在连接过程中无法正常工作。您需要get_the_ID()代替。

其次,当您echo option时,引号的匹配似乎很奇怪。假设您希望输出类似于

<option value="1">Title</option>

您的echo应该是

echo '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';

答案 1 :(得分:0)

它在连接过程中无法正常工作。您需要get_the_ID($post)代替。

  <?php 
        $args = array( 'numberposts=>-1&order=>ASC&orderby=>title' );
        $postslist = get_posts( $args );     foreach ($postslist as $post) : setup_postdata($post);
        echo '<option value="'.get_the_id($post).'">'.get_the_title($post).'</option>';
        endforeach;
?>

您的echo应该是

echo '<option value="' . get_the_ID($post) . '">' . get_the_title($post) . '</option>';