如何在我的页面模板中输出WPAlChemy重复字段的元值?

时间:2015-11-04 作者:andrw22

我有一个自定义的帖子类型Downloads,它包含一些自定义的元框,包括一个重复字段。我可以在页面模板中输出所有字段的值,但无法在页面模板中输出重复字段的值。

在my downloads\\u meta中。php,我有以下内容:

<div class="my_meta_control">
  <label>Includes:</label>
  <?php $metabox->the_field(\'cb_second_includes\'); ?>
  <p><input type="checkbox" class="second-includes-check" name="<?php $metabox->the_name(); ?>" value="1"<?php $metabox->the_checkbox_state(\'1\'); ?>/><span>Different bullet points for previous version?</span></p>
  <div class="clearfix">
    <?php while($metabox->have_fields_and_multi(\'docs\')): ?>
    <?php $metabox->the_group_open(); ?>
      <?php $metabox->the_field(\'li-text\'); ?>
      <?php $wpalchemy_media_access->setGroupName(\'li-n\'. $mb->get_the_index())->setInsertButtonLabel(\'Insert\'); ?>
      <div class="col includes">
        <?php echo $wpalchemy_media_access->getField(array(\'name\' => $metabox->get_the_name(), \'value\' => $metabox->get_the_value())); ?>
        <a href="#" class="dodelete button" style="float:right;">-</a>
      </div>
    <?php $metabox->the_group_close(); ?>
    <?php endwhile; ?>
  </div>
  <p style="margin-bottom:0; padding-top:0;"><a href="#" class="docopy-docs button">+</a></p>
</div>
在我的页面模板中,我有:

<?php
  $args = array(
    // \'posts_per_page\' => 4,
    \'post_type\' => array(\'downloads\'),
    \'post_status\'     => \'publish\',
    \'orderby\' => \'menu_order\',
    \'order\' => \'ASC\'
  );

  $temp = $wp_query;
  global $wp_query;
  global $post;
  $wp_query= null;

  $wp_query = new WP_Query($args);
?>

<?php
  if($wp_query->have_posts()) :
  while ($wp_query->have_posts()) : $wp_query->the_post();
?>

  <?php
    global $downloads_meta;
  ?>

  <ul>
    <?php while($downloads_meta->have_fields(\'docs\')): ?>
      <li><?php echo $downloads_meta->get_the_value(\'li-text\'); ?></li>
    <?php endwhile; ?>
  </ul>

<?php
  endwhile;
  endif;
?>
通过上面的代码,我可以为第一个自定义post类型输出值,但第二个类型不会为重复字段输出任何值。如果能了解我可能做错了什么,我将不胜感激。

1 个回复
SO网友:bonger

根据评论,这里有一个(非常粗略的)访问前端重复字段的示例。注意使用WPAlchemy提取模式,它为每个字段提供了自己的元条目,并使访问它们变得更简单。

还可以使用“functions.php”中的操作来添加metabox并输出内容以用于测试目的-显然,您需要调整\'init\' 编码到您的自定义帖子类型,并将\'the_content\' 页面模板中的代码。

例如,在“functions.php”中:

define( \'METABOX_ID\', \'my_meta_\' );
add_action( \'init\', function () {
    if ( is_admin() ) {
        if ( ! class_exists( \'WPAlchemy_MetaBox\' ) ) {
            require_once \'wpalchemy/MetaBox.php\'; // Assuming subdirectory of current theme directory.
            require_once \'wpalchemy/MediaAccess.php\'; // Assuming subdirectory of current theme directory.
        }
        global $wpalchemy_media_access;
        $wpalchemy_media_access = new WPAlchemy_MediaAccess();
        $download_meta = new WPAlchemy_MetaBox(
            array(
                \'id\' => METABOX_ID,
                \'types\' => array( \'post\' ), // array( \'downloads\' )
                \'template\' => get_stylesheet_directory() . \'/download_meta.php\',
                \'priority\' => \'default\',
                \'mode\' => WPALCHEMY_MODE_EXTRACT,
                \'prefix\' => METABOX_ID,
            )
        );
    }
} );

// Just for testing - should be in the custom post type template.
add_action( \'the_content\', function ( $content ) {
    if ( is_single() ) {
        $post_id = get_the_ID();
        // Access single-valued (non-array) meta.
        $ul_class = \'docs\';
        if ( $cb_second_includes = get_post_meta( $post_id, METABOX_ID . \'cb_second_includes\', true ) ) {
            $ul_class = \'docs_second_includes\';
        }
        // Access array-valued meta.
        if ( $docs = get_post_meta( $post_id, METABOX_ID . \'docs\', true ) ) {
            ob_start();
            ?>
            <ul class="<?php echo $ul_class; ?>">
            <?php foreach ( $docs as $doc ) : ?>
                <li><?php echo $doc[\'li-text\']; ?></li>
            <?php endforeach; ?>
            </ul>
            <?php
            $content = $content . ob_get_clean();
        }
    }
    return $content;
} );
这是我使用的“download\\u meta.php”的版本(注意,metabox的地址是通过$mb):

<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control">
  <label>Includes:</label>
  <?php $mb->the_field(\'cb_second_includes\'); ?>
  <p><input type="checkbox" class="second-includes-check" name="<?php $mb->the_name(); ?>" value="1"<?php $mb->the_checkbox_state(\'1\'); ?>/><span>Different bullet points for previous version?</span></p>
  <div class="clearfix">
    <?php while($mb->have_fields_and_multi(\'docs\')): ?>
    <?php $mb->the_group_open(); ?>
      <?php $mb->the_field(\'li-text\'); ?>
      <?php $wpalchemy_media_access->setGroupName(\'li-n\'. $mb->get_the_index())->setInsertButtonLabel(\'Insert\'); ?>
      <div class="col includes">
        <?php echo $wpalchemy_media_access->getField(array(\'name\' => $mb->get_the_name(), \'value\' => $mb->get_the_value())); ?>
        <a href="#" class="dodelete button" style="float:right;">-</a>
      </div>
    <?php $mb->the_group_close(); ?>
    <?php endwhile; ?>
  </div>
  <p style="margin-bottom:0; padding-top:0;"><a href="#" class="docopy-docs button">+</a></p>
</div>

相关推荐

仅为主页显示Metabox

我将尝试使用设置字段将metabox添加到主页,但出现问题,请帮助我。删除时,metabox不会显示在页面编辑器中if statement 它显示在所有页面上。add_action(\'add_meta_boxes\', \'metabox_homepage_videos\'); function metabox_homepage_videos($post) { if (is_front_page()): add_meta_box(\'metabox