我试图在名为CMB2的自定义post类型上使用'projects' 'Multiple‘字段类型。但是这些图片并没有显示在帖子上。到目前为止这是我的安排。
My functions.php
/**
* Get the bootstrap!
*/
if ( file_exists( __DIR__ . '/cmb2/init.php' ) ) {
require_once __DIR__ . '/cmb2/init.php';
} elseif ( file_exists( __DIR__ . '/CMB2/init.php' ) ) {
require_once __DIR__ . '/CMB2/init.php';
}
add_action( 'cmb2_admin_init', 'cmb2_metaboxes' );
/**
* Define the metabox and field configurations.
*/
function cmb2_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_cubo_';
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'cubo_metabox',
'title' => __( 'Project', 'cmb2' ),
'object_types' => array( 'projects' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
) );
$cmb->add_field( array(
'name' => __( 'Images', 'cmb2' ),
'desc' => __( 'Upload project images.', 'cmb2' ),
'id' => $prefix . 'file_list',
'type' => 'file_list',
'preview_size' => array( 150, 150 ), // Default: array( 50, 50 )
) );
}template-tags.php
/**
* Sample template tag function for outputting a cmb2 file_list
*
* @param string $file_list_meta_key The field meta key. ('wiki_test_file_list')
* @param string $img_size Size of image to show
*/
function cmb2_output_file_list( $file_list_meta_key, $img_size = 'medium' ) {
// Get the list of files
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
echo '<div class="file-list-wrap">';
// Loop through them and output an image
foreach ( (array) $files as $attachment_id => $attachment_url ) {
echo '<div class="file-list-image">';
echo wp_get_attachment_image( $attachment_id, $img_size );
echo '</div>';
}
echo '</div>';
}single-projects.php
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while (have_posts()) : the_post();
get_template_part('template-parts/content', 'projects', get_post_format());
// // If comments are open or we have at least one comment, load up the comment template.
// if (comments_open() || get_comments_number()) :
// comments_template();
// endif;
cmb2_output_file_list( 'cubo_metabox', 'small' );
endwhile; // End of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->然后转到管理区域> projects > add >,并将一些图片添加到我新创建的字段的帖子中。保存岗位。但是这些图像并没有显示在页面上。如果我检查源,我可以在标记中看到div已经创建,但它是空的。
<div class="file-list-wrap"><div class="file-list-image"></div></div>我在文章中添加了这些图像,使其具有一个后缀id-70。我检查了mySQL数据库,并在post_id 70下添加了该数据库。
70 _cubo_file_list a:7:{i:73;s:54:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x-6.jpg";i:78;s:54:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x-1.jpg";i:77;s:54:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x-2.jpg";i:76;s:54:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x-3.jpg";i:74;s:54:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x-5.jpg";i:75;s:54:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x-4.jpg";i:72;s:52:"http://cubo.dk/wp-content/uploads/2016/08/dta_2x.jpg";}因此,至少在我看来,这些文件必须正确地添加到数据库中,但是我不知道我哪里出错了,因为它们没有显示在页面的div中?
谢谢。
发布于 2016-08-26 14:14:54
'cubo_metabox'不是你的$file_list_meta_key,'_cubo_file_list'是。因此,产出应该是:
cmb2_output_file_list( '_cubo_file_list', 'small' );https://stackoverflow.com/questions/39165277
复制相似问题