我想我已经找到了问题所在。从…起wordpress get_posts 文档中有一个get\\u post示例
<ul>
<?php
global $post;
$args = array( \'posts_per_page\' => 5, \'offset\'=> 1, \'category\' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
问题是循环中的变量$post,我不确定是否是因为它与第4行中的另一个全局变量$post冲突。
因此,我更改了以下代码:
$countries = get_posts(array(
\'post_type\' => \'country\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
));
?>
<p>
<!-- my custom value Select -->
<label for="location_country_id">Country:</label>
<select name=\'location_country_id\' id=\'location_country_id\'>
<option value=""> -- choose Country -- </option>
<?php
if ( $countries ):
foreach ( $countries as $post ) : setup_postdata($post);
$selected = \'\';
if ( $current_country == $post->ID)
{
$selected = \'selected\';
}
?>
<option value="<?php echo esc_attr($post->ID); ?>" <?=$selected?>><?php echo esc_html($post->post_title); ?></option>
<?php endforeach;
/* Restore original Post Data */
wp_reset_postdata();
endif;
?>
</select>
</p>
为了这个
$countries = get_posts(array(
\'post_type\' => \'country\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
));
?>
<p>
<!-- my custom value Select -->
<label for="location_country_id">Country:</label>
<select name=\'location_country_id\' id=\'location_country_id\'>
<option value=""> -- choose Country -- </option>
<?php
if ( $countries ):
foreach ( $countries as $country ) : setup_postdata($country);
$selected = \'\';
//if($player->id == $item->title)
if ( $current_country == $country->ID)
{
$selected = \'selected\';
}
?>
<option value="<?php echo esc_attr($country->ID); ?>" <?=$selected?>><?php echo esc_html($country->post_title); ?></option>
<?php endforeach;
/* Restore original Post Data */
wp_reset_postdata();
endif;
?>
</select>
</p>
再也没有断杆了。
我希望这对其他人有帮助。