我正在尝试编写代码,以选择哪些帖子应该包含在与用户正在编辑的当前帖子相关的帖子中。我使用了@MikeSchinkel非常有用的帖子here, 为了在元字段中包含帖子列表,我使用了this patch 为了使选项能够多选,我还使用jquery Selected插件稍微整理了一下列表。我现在完全被困在如何在前端检索循环中的值上,我尝试了以下方法:
$intro = get_post_meta( $post->ID, \'SELECT_POST_TYPE\', true );
echo $intro;
var_dump( $_POST)
还有这个:
$intro = get_post_meta( $post->ID, $selected_post_id, true );
echo $intro;
var_dump($intro)
还有这个:
$intro = get_post_meta( $post->ID, \'select_box\', true );
echo $intro;
var_dump($intro)
注册meta\\u框的代码如下:
class WPSE_85107 {
var $FOR_POST_TYPE = \'question\';
var $SELECT_POST_TYPE = \'post\';
var $SELECT_POST_LABEL = \'Post\';
var $box_id;
var $box_label;
var $field_id;
var $field_label;
var $field_name;
var $meta_key;
function __construct() {
add_action( \'admin_init\', array( $this, \'admin_init\' ) );
}
function admin_init() {
add_action( \'add_meta_boxes\', array( $this, \'add_meta_boxes\' ) );
add_action( \'save_post\', array( $this, \'save_post\' ), 10, 2 );
$this->meta_key = "_selected_{$this->SELECT_POST_TYPE}";
$this->box_id = "select-{$this->SELECT_POST_TYPE}-metabox";
$this->field_id = "selected-{$this->SELECT_POST_TYPE}";
$this->field_name = "selected_{$this->SELECT_POST_TYPE}";
$this->box_label = __( "Select {$this->SELECT_POST_LABEL}", \'wpse-85107\' );
$this->field_label = __( "Choose {$this->SELECT_POST_LABEL}", \'wpse-85107\' );
}
function add_meta_boxes() {
add_meta_box(
$this->box_id,
$this->box_label,
array( $this, \'select_box\' ),
$this->FOR_POST_TYPE,
\'side\'
);
}
function select_box( $post ) {
$selected_post_id = get_post_meta( $post->ID, $this->meta_key, true );
global $wp_post_types;
$save_hierarchical = $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical;
$wp_post_types[$this->SELECT_POST_TYPE]->hierarchical = true;
wp_dropdown_pages( array(
\'id\' => $this->field_id,
\'name\' => $this->field_name,
\'selected\' => empty( $selected_post_id ) ? 0 : $selected_post_id,
\'post_type\' => $this->SELECT_POST_TYPE,
\'show_option_none\' => $this->field_label,
\'multiselect\' => true,
\'class\' => \'chzn-select\'
));
$wp_post_types[$this->SELECT_POST_TYPE]->hierarchical = $save_hierarchical;
}
function save_post( $post_id, $post ) {
if ( $post->post_type == $this->FOR_POST_TYPE && isset( $_POST[$this->field_name] ) ) {
update_post_meta( $post_id, $this->meta_key, $_POST[$this->field_name] );
}
}
}
new WPSE_85107();
更新:多亏了@matt\\uo,这是您前端循环模板中的anwser代码:
$meta = get_post_meta($post->ID, \'_selected_post\', true);
if ( ! $meta ):
echo \'\';
else:?>
<a class="related_post" href="<?php echo get_permalink($meta); ?>"><?php echo get_the_title($meta); ?></a>
<br/>
<?php endif;