我遇到了非常奇怪的错误,有一个帖子类型“作者”,我在帖子类型“文章”中显示作者列表,该列表成功显示所有作者并且也成功保存,但是当我想在前端显示它们时作者没有在循环中显示,但是当我
print_r("author")
时,数组向我显示选定的作者,但在循环中没有作者。这是我的代码:
我还使用了 foreach 循环。
函数.php
<?php
function ct_downlaod_meta($post){
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
echo '<select name="opt_meta_author[]" id="opt_meta_author" multiple="multiple">';
$val = get_post_meta($post->ID, 'opt_meta_author', true);
$q = get_posts('post_type=author&post_parent=0&numberposts=-1&orderby=menu_order&order=ASC');
$val_array = explode('<br />', $val); echo $val_array;
foreach ($q as $obj)
{
echo '<option value="'.$obj->post_title.'"'.selected($obj->post_title, $val);
if(in_array($obj->post_title, $val_array)) { echo ' selected="selected"'; };
echo '>'.$obj->post_title.'</option>';
}
echo '</select>';
}
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
global $post;
$opt_meta_author = implode('<br />',$_POST['opt_meta_author']);
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
}
?>
作者-bio.php
<?php
$opt_meta_author_array = explode('<br />', get_post_meta($post->ID, 'opt_meta_author', true));
$args = array(
'post__in' => $opt_meta_author_array,
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC',
);
print_r($args);
$author = new WP_Query($args);
if($author->have_posts()) :
?>
<h3 id="entry-author-title"><?php _e('About The Author(s)', 'framework') ?></h3>
<?php
while($author->have_posts()) :
$author->the_post();
?>
<section id="entry-author" class="clearfix">
<div style=" margin-bottom:12px; float:left; width:100%; padding-bottom:5px;">
<div class="gravatar">
<?php
if(has_post_thumbnail()){
the_post_thumbnail( array( 70, 70 ) );
}
else{
echo '<img src="http://0.gravatar.com/avatar/6179f2642031d79f16bf2f03f0f66df9?s=70&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D70&r=G" />';
}
?>
</div>
<?php /*?> <h4><?php echo the_title() ?></h4><?php */?>
<div style="width:100%; margin:0 0px;">
<?php //echo get_post_meta($post->ID, 'txt_meta_country', true); ?>
</div>
<div class="entry-author-desc">
<?php echo the_content() ?>
</div>
</div></section>
<?php
endwhile;
endif;
?>
当我 print_r($args);数组给了我真实的结果
Array ( [post__in] => Array ( [0] => Andrew Turnell [1] => Adrian Gimpel ) [post_type] => author [orderby] => title [order] => ASC )
但循环中结果为空。
所以有一天延迟我在functions.php中找到了自己的解决方案
<?php
function ct_downlaod_meta($post){
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
echo '<select name="opt_meta_author[]" id="opt_meta_author" multiple="multiple">';
$val = get_post_meta($post->ID, 'opt_meta_author', true);
$q = get_posts('post_type=author&post_parent=0&numberposts=-1&orderby=menu_order&order=ASC');
$val_array = explode('<br />', $val); echo $val_array;
foreach ($q as $obj)
{
echo '<option value="'.$obj->ID.'"'.selected($obj->ID, $val);
if(in_array($obj->ID, $val_array)) { echo ' selected="selected"'; };
echo '>'.$obj->post_title.'</option>';
}
echo '</select>';
}
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
global $post;
$opt_meta_author = implode('<br />',$_POST['opt_meta_author']);
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
}
?>
在author-bio.php中我没有改变任何东西并且我的代码运行良好