Per comments, here’s a (very rough) example of accessing repeated fields in the frontend. Note using WPAlchemy extract mode, which gives each field its own meta entry, and makes accessing them a bit simpler.
Also using actions in “functions.php” to add metabox and to output content for test purposes – obviously you’d adapt the 'init'
code to your custom post type and put the 'the_content'
code in your page template.
Eg, in “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;
} );
Here’s the version of “download_meta.php” I used (note the metabox is addressed via $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>