相关帖子和自定义meta_box?

时间:2013-07-12 作者:vimes1984

我正在尝试编写代码,以选择哪些帖子应该包含在与用户正在编辑的当前帖子相关的帖子中。我使用了@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;

1 个回复
最合适的回答,由SO网友:Mat_ 整理而成

看来你必须使用

$meta = get_post_meta($post->ID, \'_selected_post\', true); echo $meta;

已注册的帖子类型使用\\隐藏,其名称在以下行中定义:

$this->meta_key = "_selected_{$this->SELECT_POST_TYPE}";

其中,SELECT\\u POST\\u TYPE在以下行定义为“POST”:

var $SELECT_POST_TYPE = \'post\';

希望有帮助。

结束

相关推荐

如何在用户添加新帖子时为非管理员隐藏插件metabox

我已经能够在后端为非管理员禁用所有元框,但我在Facebook AWD All in one 它创建了一个小部件,所有用户在创建新帖子时都可以看到。如何禁用它?是否有某种命令适用于由插件创建的任何小部件,我可以将其添加到我的函数中。php如果我知道插件的短代码?我需要你的帮助!谢谢http://wordpress.org/extend/plugins/facebook-awd/我使用以下代码删除元框######################################################