phpwordpresspluginsmeta-boxesnews-ticker

Get custom field value and display in the frontend wordpress


I am currently working in smart mag theme where in the homepage newsticker all the latest news are posted by default. But I want to show only selected post in the newsticker.For that I installed the plugin 'Meta box'.And wrote a custom meta field

add_filter( 'rwmb_meta_boxes', 'breaking_news_radio_demo' );

   function breaking_news_radio_demo( $meta_boxes )

   {


        $prefix = 'rw_';

    $meta_boxes[] = array(
        'title'  => __( 'Breaking news', '$prefix' ),
        'fields' => array(
            array(
                'name'    => __( 'Show', 'rw' ),
                'id'      => 'radio',
                'pages'   => array('post-new'),
                'type'    => 'radio',
                // Array of 'value' => 'Label' pairs for radio options.
                // Note: the 'value' is stored in meta field, not the 'Label'
                'options' => array(
                    'YES' => __( 'Yes', '$prefix' ),
                    'NO' => __( 'No', '$prefix' ),
                ),
            ),
        )
    );

    return $meta_boxes;
}

The meta box show fine in the 'Add new post'. But using the radio button i want to control which posts are displayed in the news-ticker. And the news-ticker in the theme are displayed using the following code

<?php if (!Bunyad::options()->disable_topbar_ticker): ?>
                <div class="trending-ticker">
                    <span class="heading"><?php echo Bunyad::options()->topbar_ticker_text; // filtered html allowed for admins ?></span>

                    <ul>
                        <?php $query = new WP_Query(apply_filters('bunyad_ticker_query_args', array('orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 8))); ?>

                        <?php while($query->have_posts()): $query->the_post(); ?>

                            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

                        <?php endwhile; ?>

                        <?php wp_reset_postdata(); ?>
                    </ul>
                </div>
                <?php endif; ?>

Any help is highly appreciated.


Solution

  • You must add condition in loop where you compare rwmb_meta( 'radio' )
    Check documentation for more details about rwmb_meta.

    It could be looks like that:

    <?php while($query->have_posts()): $query->the_post(); ?>
        <?php if( rwmb_meta( 'radio' ) == 'Yes' ): ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        <?php endif; ?>
    <?php endwhile; ?>
    

    Another way, probably better, is change the WP_Query by adding key and meta value that you looking for.

    <?php $query = new WP_Query(apply_filters('bunyad_ticker_query_args', array('meta_key' => 'radio', 'meta_value' => 'Yes','orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 8))); ?>