Wordpress - rwmb_meta不在帖子页面上工作

时间:2017-02-19 12:58:29

标签: php wordpress meta-boxes

将页面设置为我的帖子的索引后,rwmb_meta已停止在此页面上工作。

enter image description here

enter image description here

我的代码:

<?php
$images = rwmb_meta( 'angel_imgadv', 'type=image&size=full');
var_dump($images);
?>

结果:

array(0) {
}

但我已经将图片附加到该页面了:

enter image description here

我的设置:

add_filter( 'rwmb_meta_boxes', 'angel_register_meta_boxes' );

/**
 * Register meta boxes
 *
 * Remember to change "your_prefix" to actual prefix in your project
 *
 * @param array $meta_boxes List of meta boxes
 *
 * @return array
 */
function angel_register_meta_boxes( $meta_boxes )
{
    /**
     * prefix of meta keys (optional)
     * Use underscore (_) at the beginning to make keys hidden
     * Alt.: You also can make prefix empty to disable it
     */
    // Better has an underscore as last sign
    $prefix = 'angel_';

    // 2nd meta box
    $meta_boxes[] = array(
        'title' => __( 'Advanced Images', 'angel_' ),

        'post_types' => array( 'post', 'page' ),

        'fields' => array(
            array(
                'name'             => __( 'Carousal', 'angel_' ),
                'id'               => "{$prefix}imgadv",
                'type'             => 'image_advanced',
                'max_file_uploads' => 10,
            ),
            // DIVIDER
            array(
                'type' => 'divider',
                'id'   => 'fake_divider_id', // Not used, but needed
            ),
            // IMAGE ADVANCED (WP 3.5+)
            array(
                'name'             => __( 'Cover Images', 'angel_' ),
                'id'               => "{$prefix}imgcover",
                'type'             => 'image_advanced',
                'max_file_uploads' => 2,
            ),
            // DIVIDER
            array(
                'type' => 'divider',
                'id'   => 'fake_divider_id', // Not used, but needed
            ),
            // IMAGE ADVANCED (WP 3.5+)
            array(
                'name'             => __( 'Lightbox Images', 'angel_' ),
                'id'               => "{$prefix}imglightbox",
                'type'             => 'image_advanced',
                // 'max_file_uploads' => 10,
            ),
        ),
    );

    // Get post/page ID.
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

    return $meta_boxes;
}

有什么想法吗?

这是我正在使用的metabox plugin

1 个答案:

答案 0 :(得分:1)

您应该使用这样的代码并在index.php文件中发布:

<?php 
    global $wp_query;
    $images = rwmb_meta( 'angel_imgadv', 'type=image_advanced&size=full', $wp_query->get_queried_object_id() );
    foreach ( $images as $image ) {
        echo "<img src='{$image['url']}' width='{$image['width']}' height='{$image['height']}' alt='{$image['alt']}' />";
    }
?>