How to display label of select advanced custom field?

Support General How to display label of select advanced custom field?Resolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #20499
    A. KurniawanA. Kurniawan
    Participant

    I'm trying to find out how to display label from select advanced custom field for the last couple of hours.

    When I use rwmb_meta() it returns the field's value, instead of the label.
    But when I use rwmb_the_value() it returns the label with<a>..</a> tag wrapping it.

    Is there any way to display the field without this <a> wrapper in a correct way?

    Thanks

    #20502
    Long NguyenLong Nguyen
    Moderator

    Hi,

    When using the function rwmb_the_value() to show the value of the field select_advanced, it just shows the list with HTML tag <ul> <li>, not the <a> tag.

    Or you can use the function rwmb_get_field_settings() to get all settings of a field.

    #20504
    A. KurniawanA. Kurniawan
    Participant

    Weird, on my installation it's always wrap with <a></a>, so I always make a link when displaying the label.

    #20505
    Long NguyenLong Nguyen
    Moderator

    Could you please share the code that created the field select_advanced and show on the front end? I will take a closer look and give a quick response.

    #20506
    A. KurniawanA. Kurniawan
    Participant
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = array (
            'title' => esc_html__( 'Server Specifications', 'text-domain' ),
            'id' => 'server-specifications',
            'post_types' => array(
                0 => 'dedicated-server',
            ),
            'context' => 'after_title',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => $prefix . 'cpu_model_06x5z89bthw',
                    'type' => 'post',
                    'name' => esc_html__( 'CPU Model', 'text-domain' ),
                    'post_type' => array(
                        0 => 'cpu',
                    ),
                    'field_type' => 'select_advanced',
                    'required' => 1,
                    'after' => '<div class="addon-link">&raquo; &nbsp;<a href="post-new.php?post_type=cpu">Add new CPU</a></div>',
                ),
                array (
                    'id' => $prefix . 'ram_rpvl3tynznp',
                    'type' => 'post',
                    'name' => esc_html__( 'RAM', 'text-domain' ),
                    'post_type' => array(
                        0 => 'ram',
                    ),
                    'field_type' => 'select',
                    'query_args' => array(
                        'order' => 'ASC',
                    ),
                    'required' => 1,
                    'after' => '<div class="addon-link">&raquo; &nbsp;<a href="post-new.php?post_type=ram">Add new RAM</a></div>',
                ),
                array (
                    'id' => $prefix . 'drives_046wdfgugzub',
                    'type' => 'post',
                    'name' => esc_html__( 'Drives', 'text-domain' ),
                    'post_type' => array(
                        0 => 'drive',
                    ),
                    'field_type' => 'select_advanced',
                    'required' => 1,
                    'after' => '<div class="addon-link">&raquo; &nbsp;<a href="post-new.php?post_type=drive">Add new drive</a></div>',
                    'multiple' => true,
                ),
                array (
                    'id' => $prefix . 'price_14ab48oth3ih',
                    'type' => 'text',
                    'name' => esc_html__( 'Price', 'text-domain' ),
                    'desc' => esc_html__( 'Currency sign is not required', 'text-domain' ),
                    'placeholder' => esc_html__( 'xxx.xx', 'text-domain' ),
                    'required' => 1,
                    'label_description' => esc_html__( 'Regular price', 'text-domain' ),
                ),
                array (
                    'id' => $prefix . 'featured_aaljhych5q',
                    'type' => 'switch',
                    'name' => esc_html__( 'Featured', 'text-domain' ),
                    'style' => 'rounded',
                    'on_label' => 'Yes',
                    'off_label' => 'No',
                    'label_description' => esc_html__( 'Whether item is featured', 'text-domain' ),
                ),
                array (
                    'id' => $prefix . 'onsale_pn2jn0x6aw',
                    'type' => 'switch',
                    'name' => esc_html__( 'On Sale', 'text-domain' ),
                    'style' => 'rounded',
                    'on_label' => 'Yes',
                    'off_label' => 'No',
                    'label_description' => esc_html__( 'Whether item is on sale or discounted', 'text-domain' ),
                ),
                array (
                    'id' => $prefix . 'discounted_ye8xr7m539i',
                    'type' => 'text',
                    'name' => esc_html__( 'Discounted Price', 'text-domain' ),
                    'placeholder' => esc_html__( 'xxx.xx', 'text-domain' ),
                    'label_description' => esc_html__( 'Price after discount', 'text-domain' ),
                    'visible' => array(
                        'when' => array(
                            array (
                                0 => 'onsale_pn2jn0x6aw',
                                1 => '=',
                                2 => 1,
                            ),
                        ),
                        'relation' => 'and',
                    ),
                ),
                array (
                    'id' => $prefix . 'recommended_63mxmfmup4t',
                    'type' => 'switch',
                    'name' => esc_html__( 'Recommended', 'text-domain' ),
                    'style' => 'rounded',
                    'on_label' => 'Yes',
                    'off_label' => 'No',
                    'label_description' => esc_html__( 'For home page', 'text-domain' ),
                ),
                array (
                    'id' => $prefix . 'order_url_3hdpmvlrlhd',
                    'type' => 'url',
                    'name' => esc_html__( 'Order URL', 'text-domain' ),
                    'placeholder' => esc_html__( 'https://yourdomain.com/?url=something', 'text-domain' ),
                    'required' => 1,
                ),
            ),
        );
    
        return $meta_boxes;
    }

    Actually it's not just select_advanced, but also the one that using select

    if( is_home() && is_front_page() ) {
        // The Query
        $the_query = new WP_Query( array( 'post_type' => 'dedicated-server', 'posts_per_page' => 1 ) );
    
        // The Loop
        if ( $the_query->have_posts() ) {
           
            while ( $the_query->have_posts() ) {
                
                $the_query->the_post();
    
                $recommend  = rwmb_meta('recommended_63mxmfmup4t');
                
                if($recommend == 1 || $recommend == 'Yes') {
                    ?>
    <?php 
    <!--- other tags ... -->
    
    <h5 class="h5-z5-1"><?php rwmb_the_value('cpu_model_06x5z89bthw');?>
    
    <!--- other tags ... -->
    <?php
    
                }
            }        
        } else {
            // no posts found
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    }
    
    ?>
    #20508
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Because the type of the field is post so the helper function rwmb_the_value() always returns the title of the post wrapped in the <a> tag to link to the post.

    You can use the function get_the_title() to show only the post title base on the post ID which returned by the helper function rwmb_meta().

    $select_advanced = rwmb_meta( 'cpu_model_06x5z89bthw' );
    echo get_the_title( $select_advanced );

    For more information, please follow the document below.
    https://developer.wordpress.org/reference/functions/get_the_title/
    https://docs.metabox.io/rwmb-the-value/

    #20512
    A. KurniawanA. Kurniawan
    Participant

    Thank you, it works!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.