• Resolved reypm

    (@reypm)


    Hi there, I have defined a few metaboxes as follow:

    <?php
    add_filter('rwmb_meta_boxes', 'elclarin_register_meta_boxes');
    
    function elclarin_register_meta_boxes($meta_boxes)
    {
        $prefix = 'rw_';
    
        $posts = get_posts(
            array(
                'post_type' => 'post',
                'posts_per_page' => 100,
                'orderby' => 'date',
                'order' => 'DESC',
                'post_status' => 'publish'
            )
        );
    
        $options = array();
        foreach ($posts as $post) {
            if ($post->post_title !== "") {
                $options[$post->ID] = $post->post_title;
            }
        }
    
        // 1st meta box - related news
        $meta_boxes[] = array(
            'id' => 'post_related_news',
            'title' => __('Selección de noticias relacionadas'),
            'pages' => array('post'),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array(
                    'name' => __('Noticia relacionada'),
                    'id' => $prefix . 'related_link',
                    'type' => 'select_advanced',
                    'options' => $options,
                    'clone' => true,
                    'placeholder' => 'Seleccione una noticia'
                ),
            )
        );
    
        // 2nd meta box - exclusive news && show at home && news show at
        $meta_boxes[] = array(
            'id' => 'post_news_options',
            'title' => __('Configuración de la noticia'),
            'pages' => array('post'),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array(
                    'name' => __('Contenido exclusivo?'),
                    'id' => $prefix . 'exclusive_news',
                    'type' => 'checkbox',
                    'desc' => __('Marcar si quiere mostrar la noticia como un contenido exclusivo en la página de inicio')
                ),
                array(
                    'name' => __('Destacado?'),
                    'id' => $prefix . 'show_at_home',
                    'type' => 'checkbox',
                    'desc' => __('Marcar si quiere mostrar la noticia como destacada en la página de inicio')
                ),
                array(
                    'name' => __('Mostrar noticia en:'),
                    'id' => $prefix . 'show_at_position',
                    'type' => 'select',
                    'options' => array(
                        'news_at_left' => __('Izquierda'),
                        'news_at_right' => __('Derecha'),
                    )
                ),
            )
        );
    
        // 3st meta box - news sources
        $meta_boxes[] = array(
            'id' => 'post_news_source',
            'title' => __('Fuente de la noticia'),
            'pages' => array('post'),
            'context' => 'normal',
            'fields' => array(
                array(
                    'name' => __('Fuente de la noticia'),
                    'id' => $prefix . 'news_source',
                    'type' => 'url',
                    'size' => 100
                )
            )
        );
    
        // 4th meta box - news author
        $meta_boxes[] = array(
            'id' => 'post_news_author',
            'title' => __('Autor de la noticia'),
            'pages' => array('post'),
            'context' => 'normal',
            'fields' => array(
                array(
                    'name' => __('Autor de la noticia'),
                    'id' => $prefix . 'news_author',
                    'type' => 'text',
                    'size' => 100
                )
            )
        );
    
        return $meta_boxes;
    }

    And I am trying to show it’s values in single.php using this code:

    <?php echo rwmb_meta('post_news_source', array(), $post->ID) ?>
    <?php echo rwmb_meta('rw_post_news_source', array(), $post->ID) ?>

    And doesn’t work since I get empty values? What I did wrong? Also how do I iterate over values on id#post_related_news meta box in order to display them as anchor to each news on that list?

    https://ww.wp.xz.cn/plugins/meta-box/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Did you ever find a solution to this?
    I have the exact same problem.
    Thanks.

    Plugin Author Anh Tran

    (@rilwis)

    1. You don’t have any field ID post_news_source or rw_post_news_source (you only have meta box with that ID, but that’s meta box’s ID, not field’s), so you get empty string.

    To fix that, change the field ID or use code: rwmb_meta('rw_news_source', array(), $post->ID)

    2. To iterate related news, use code:

    $related = rwmb_meta( 'rw_related_link' );
    foreach ( $related as $post_id )
    {
        echo '<a href="' . get_permalink( $post_id ) . '">' . get_the_title( $post_id . '</a>';
    }

    Hope that help!
    Best regards

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Can't display values in single’ is closed to new replies.