根据评论,这里有一个(非常粗略的)访问前端重复字段的示例。注意使用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>