Run
Run

Reputation: 57226

Wordpress - rwmb_meta not working on posts page

After setting a page as the index for my posts, rwmb_meta has stopped working on this page.

enter image description here

enter image description here

My code:

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

Result:

array(0) {
}

But I have images attached to that page already:

enter image description here

My setting:

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;
}

Any ideas?

This is the metabox plugin that I am using.

Upvotes: 0

Views: 2922

Answers (1)

Davis Hoang
Davis Hoang

Reputation: 11

You should use code like this and post in your index.php file:

<?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']}' />";
    }
?>

Upvotes: 1

Related Questions