Using Radio Fields Properly

Support General Using Radio Fields Properly

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #7975
    tsqueztsquez
    Participant

    Hi there,

    Quick question. I cannot seem to wrap my head around using radio buttons and displaying the information on the frontend. Checkboxes are OK, I can do those all day long with no problem. But for this particular metabox, I need to use radio buttons for the options. Here is what I have:

    //Featured Image Options
    function xxxxxx_featured_image_options_custom_meta_box( $meta_boxes ) {
    	$prefix = 'xxxxxx_';
    	$meta_boxes[] = array(
    		'id' => 'xxxxxx-featured-image-options',
    		'title' => esc_html__('Featured Image Options','xxxxxx'),
    		'post_types' => array('post','page'),
    		'context' => 'side',
    		'priority' => 'default',
    		'autosave' => true,
    		'fields' => array(
    			array(
    				'id' => $prefix . 'featured_image_layout_options',
    				'type' => 'radio',
    				'placeholder' => '',
    				'options' => array(
    					'show_blow_title' => esc_html__('Featured Image Below Title','xxxxxx'),
    					'show_above_title' => esc_html__('Featured Image Above Title','xxxxxx'),
    					'page_header_contained' => esc_html__('Page Header Contained.','xxxxxx'),
    					'page_header_contained_title_overlay' => esc_html__('Page Header Cont. w/Title','xxxxxx'),
    					'page_header_contained_title_overlay_parallax' => esc_html__('Page Header Cont. w/Title - Parallax','xxxxxx'),
    					'page_header_full_width' => esc_html__('Page Header Full WIdth','xxxxxx'),
    					'page_header_full_width_title_overlay' => esc_html__('Page Header FW w/Title','xxxxxx'),
    					'page_header_full_width_title_overlay_parallax' => esc_html__('Page Header FW w/Title - Parallax','xxxxxx')
    				),
    				'inline' => false,
    				'std' => 'show_blow_title',
    			),
    			array(
    				'id' => $prefix . 'page_header_height',
    				'type' => 'text',
    				'name' => esc_html__('Page Header Height','xxxxxx'),
    				'desc' => esc_html__('Use PX, %, REM, EM and VH based values.','xxxxxx'),
    			),
    		),
    	);
    	return $meta_boxes;
    }
    add_filter('rwmb_meta_boxes','xxxxxx_featured_image_options_custom_meta_box');

    OK so the Featured Image Options metabox shows up fine. As you can see, the default is set to show_blow_title. This means that the featured image will display below the title of the post/page. Should be easy enough right? But nooooooo...lol!

    So if the default is show_blow_title, how do I get the featured image to show under the post/page title? This is what I would like to use in the theme if possible (if not oh well):

    <?php if (what_is_the_code_that_goes_here)): ?>
    	<?php do_action('xxxxxx_featured_image_single'); ?>
    <?php endif; ?>

    would that even be correct?

    Now before anyone starts posting read this, or read that, I did read the following:

    https://docs.metabox.io/fields/radio/

    https://docs.metabox.io/rwmb-meta/

    https://docs.metabox.io/rwmb-the-value/

    and I did do an extensive search of the forums and for the life of me I cannot find an answer and therefore cannot figure out what to do.

    I do apologize if the answer is "stupid" simple and I am missing it, I am really new to metaboxes but I am finding that they are freaking awesome to use.

    Any help is greatly appreciated.

    Hope everyone has a Merry Christmas

    #7987
    Truong GiangTruong Giang
    Participant

    Hi there,

    Please try this code:

    
    $option = rwmb_meta( 'xxxxxx-featured-image-options' );
    if ( ! $option || 'show_blow_title' == $option ) {
        // do action.
    }
    

    For other options, you don't need ! $option

    Thank you.

    #8005
    tsqueztsquez
    Participant

    HI there,

    Thanks for taking the time to try and help me out. It's greatly appreciated. Unfortunately it is not working. Itried this:

    if ( ! function_exists('totalpress_build_content_page_entry_header')) :
    	function totalpress_build_content_page_entry_header() { ?>
    
    		<?php $option = rwmb_meta('tp-primo-featured-image-options');
    		if ( $option || 'show_above_title' == $option ) {
    		    do_action('totalpress_featured_image_single');
    		} ?>
    
    		<header class="entry-header">	
    			<?php if (get_post_meta($post->ID,'totalpress_page_options_hide_title',true)) {
    		   		the_title('<h1 class="entry-title hide" itemprop="headline">','</h1>');
    			  } else {
    		   		the_title('<h1 class="entry-title" itemprop="headline">','</h1>');
    			  } ?>
    		</header><!-- .entry-header -->
    
    		<?php $option = rwmb_meta('tp-primo-featured-image-options');
    		if ( ! $option || 'show_below_title' == $option ) {
    		    do_action('totalpress_featured_image_single');
    		} ?>
    		
    	<?php
    	}
    	add_action('totalpress_content_page_entry_header','totalpress_build_content_page_entry_header');
    endif;

    You can see where I added the code you referenced above, but it is not working. ANy idea what I am doing wrong? I am not getting any errors or warnings, so I don't know what it could be.

    Thanks again for the help. It's greatly appreciated.

    #8006
    tsqueztsquez
    Participant

    either way I try and use the code you provided above, nothing is happening? What am I doing wrong?

    #8018
    tsqueztsquez
    Participant

    OK I just re-read what i posted and your code does work perfectly. I just realized that I asked the wrong question.

    I should have asked how do I display the featured image above the header when someone selects "show_above_title" etc. Or how do I display ALL of the options if one is selected like this:

    if 'show_below_title' show this
    if 'show_above_title' show this
    if 'page_header_contained' show this
    if 'page_header_contained_title_overlay' show this
    if 'page_header_contained_title_overlay_parallax' show this
    if 'page_header_full_width' show this
    if 'page_header_full_width_title_overlay' show this
    if 'page_header_full_width_title_overlay_parallax' show this

    In other words how do I use/display the information when someone selects one of the options, This is what is confusing me. This is what I am looking for. I am just trying to understand.

    Thank you.

    #8025
    Truong GiangTruong Giang
    Participant

    Hi there,

    Maybe using if...else is complexible. I think you should separate each option to a template file, like this:

    
    $option = rwmb_meta('tp-primo-featured-image-options');
    if ( ! $option ) {
    	$option = 'show_above_title'; // Make default value work.
    }
    
    get_template_part( "header/{$option}.php" );
    
    #8038
    tsqueztsquez
    Participant

    Hi there,

    Thanks for the suggestion, however I do not think that will work.

    I am going to try and explain this one more time using a different meta box..

    OK, so I have a metabox that I created and here it is:

    
    	function how_do_I_use_radio_buttons_meta_box( $meta_boxes ) {
    		$prefix = 'how_to_use_';
    
    		$meta_boxes[] = array(
    			'id' => 'how-to-use-radio-buttons',
    			'title' => esc_html__('How To Use Radio Fields','text-domain'),
    			'post_types' => array('post','page'),
    			'context' => 'side',
    			'priority' => 'default',
    			'autosave' => true,
    			'fields' => array(
    				array(
    					'id' => $prefix . 'radio_buttons',
    					'type' => 'radio',
    					'placeholder' => 'boogie',
    					'options' => array(
    						'button_one_selected' => 'Radio Button One',
    						'button_two_selected' => 'Radio Button Two',
    						'button_three_selected' => 'Radio Button Three',
    						'button_four_selected' => 'Radio Button Four',
    						'button_five_selected' => 'Radio Button Five',
    					),
    					'inline' => false,
    				),
    			),
    		);
    
    		return $meta_boxes;
    	}
    	add_filter( 'rwmb_meta_boxes', 'how_do_I_use_radio_buttons_meta_box' );
    

    As you can see there are five (5) choices. I have five (5) questions regarding this metabox:

    First question: When a user selects Radio Button One, how would I display "I Clicked on Radio Button One" somewhere in the theme?

    Second question: When a user selects Radio Button Two, how would I display "I Clicked on Radio Button Two" somewhere in the theme?

    Third question: When a user selects Radio Button Three, how would I display the words "I Clicked on Radio Button Three" somewhere in the theme?

    Fourth question: When a user selects Radio Button Four, how would I display the words "I Clicked on Radio Button Four" somewhere in the theme?

    Fifth question: When a user selects Radio Button Five, how would I display the words "I Clicked on Radio Button Five" somewhere in the theme?

    So if you could answer those questions, then maybe I can understand how to use radio fields in meta boxes 😉

    #8056
    Anh TranAnh Tran
    Keymaster

    Hi,

    Please try to use the following code for your 5 questions:

    $value = rwmb_meta( 'how_to_use_radio_buttons' );
    if ( 'button_one_selected' == $value ) {
        echo 'I Clicked on Radio Button One';
    } elseif ( 'button_two_selected' == $value ) {
        echo 'I Clicked on Radio Button Two';
    } elseif ( 'button_three_selected' == $value ) {
        echo 'I Clicked on Radio Button Three';
    } elseif ( 'button_four_selected' == $value ) {
        echo 'I Clicked on Radio Button Four';
    } elseif ( 'button_five_selected' == $value ) {
        echo 'I Clicked on Radio Button Five';
    }

    Basically, it's the if..else statement. You just need to check the value of the custom field and output the corresponding text.

    #8070
    tsqueztsquez
    Participant

    I would like to say thank you very much for taking the time and energy to show me how to use a meta box with radio fields. I did however figure it out but it was not with your solution.

    Someone on another site who had a tutorial on meta boxes explained what I needed to do precisely and it worked beautifully.

    Thanks for all the effort Anh and Truong put in.

    #8084
    Anh TranAnh Tran
    Keymaster

    Hi Thomas,

    Glad that you have resolved this problem. If you don't mind, please share your solution (or link to the tutorial) here so others can refer if they have a similar problem.

    Thanks,
    Anh

    #8097
    tsqueztsquez
    Participant

    Hi there,

    Yes I thought the same thing. However I misspoke. When I stated

    Someone on another site who had a tutorial on meta boxes explained what I needed to do precisely and it worked beautifully.

    I should have said the way they explained it on this other site made the code that Truong Giang gave me understandable.

    Truong Giang gave me the following:

    $option = rwmb_meta( 'xxxxxx-featured-image-options' );
    if ( ! $option || 'show_blow_title' == $option ) {
        // do action.
    }

    So after I read the article on the other site, I took what Truong wrote and redid it like this:

    $featured_image_layout = get_post_meta(get_the_ID(),'totalpress_featured_image_layout_options',true);

    Then I wrote the code to display what I needed it to like this:

    <?php if ($featured_image_layout === 'show_above_title' ): ?>
         <?php do_action('totalpress_featured_image_single'); ?>
    <?php endif; ?>

    and it worked out beautimously...lol.

    So yes you did help but I did not understand at first and only when I understood is when what you gave me helped.

    Thanks again for everything.

    #8105
    Anh TranAnh Tran
    Keymaster

    Oh I see. Glad that you figured it out 🙂

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Using Radio Fields Properly’ is closed to new replies.