Use shortcode to display custom taxonomy metadata with taxonomy

Support General Use shortcode to display custom taxonomy metadata with taxonomyResolved

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #29941
    Darren MillerDarren Miller
    Participant

    Is it possible to use the shortcode to display custom metadata associated with custom taxonomy terms alongside those terms, for all taxonomy terms associated with the current page?

    Ideally I'd then be able style each aspect of it separately as well.

    #29951
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The helper shortcode [rwmb_meta] also support showing the term meta, please read more on the documentation https://docs.metabox.io/shortcode/#examples

    #29953
    Darren MillerDarren Miller
    Participant

    Hi - yes, I've been looking at this page, and a little stumped. It looks like this is the closest thing to what I'm looking for:
    [rwmb_meta id="color" object_id="15" object_type="term"]

    I've got 3 different things to display here, as a group, all of which have their own field IDs: the custom taxonomy term itself, and 2 custom fields that have that taxonomy as their location. Essentially I'm trying to just reproduce the edit screen's fields (minus "description") on the front end.

    Can I use the shortcode to display all of that? I can't seem to figure it out. Maybe I'm just not clear on all the options I have at my disposal for "attribute" and "object type"?

    #29973
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The shortcode helps you to show the custom fields created by Meta Box, one field per shortcode.

    But if you want to create a page that supports editing the taxonomy on the frontend, I think you are going in the wrong way. The shortcode supports showing the value on the frontend instead of changing its value.

    #29977
    Darren MillerDarren Miller
    Participant

    Got it. Not trying to re-create the backend form fields, just the values from all of them at once. So it sounds like I can't accomplish that with the shortcode? Could I do it with the PHP code? This code looks like it would maybe result in only the term meta, but not the term itself as well?

    $term_id = get_queried_object_id();
    $value = rwmb_meta( $field_id, ['object_type' => 'term'], $term_id );
    echo $value;
    #29978
    Darren MillerDarren Miller
    Participant

    Or would groups be more on the right track? https://docs.metabox.io/extensions/meta-box-group/

    #29987
    Long NguyenLong Nguyen
    Moderator

    Hi Darren,

    You can refer to this documentation to create a custom shortcode to show anything that you want https://docs.metabox.io/extensions/meta-box-group/#outputing-group-with-page-builders

    #30067
    Darren MillerDarren Miller
    Participant

    Thank you!

    And then if I want to use Builder to set up sub-fields in a group, would this be the right documentation to refer to?
    https://docs.metabox.io/extensions/meta-box-builder/#custom-settings

    #30109
    Darren MillerDarren Miller
    Participant

    I've figured out how everything should be structured in the backend, but I'm really struggling to figure out how to display it in the frontend. I've tried to properly implement instructions in the documentation to no avail.

    Here's one thing I've tried:

    
    <?php
    $awards = rwmb_meta( 'awards_info' ); // ID of the group
    if ( ! empty( $awards ) ) {
        foreach ( $awards as $award ) {
        echo '<div class="awards">';
        echo '<p class="year">', $award['year'], '</p>'; // Access sub-field via its ID.
        echo '<p class="award">', $award['award'], '</p>';
        echo '<p class="status">', $award['award_status'], '</p>';
        echo '</div>';
    }
    }
    ?>
    

    When that didn't work, I went the shortcode route and tried this:

    
    add_shortcode( 'awards', function() {
        $group = rwmb_meta( 'awards_info' );
        if ( empty( $group ) ) {
            return '';
        }
    
        $output = '';
    
        // Sub-field year.
        $year = $group['year'] ?? '';
        $output .= '<h3 class="year">' . $year . '</h3>';
        
        // Sub-field award.
        $award = $group['award'] ?? '';
        $output .= '<h3 class="award">' . $award . '</h3>';
        
        // Sub-field award status.
        $award_status = $group['award_status'] ?? '';
        $output .= '<h3 class="award_status">' . $award_status . '</h3>';
    
        return $output;
    } );
    

    I also tried changing the code for the taxonomy advanced field ('award'):

    
    add_shortcode( 'awards', function() {
        $group = rwmb_meta( 'awards_info' );
        if ( empty( $group ) ) {
            return '';
        }
    
        $output = '';
    
        // Sub-field year.
        $year = $group['year'] ?? '';
        $output .= '<h3 class="year">' . $year . '</h3>';
        
        // Sub-field award.
        $terms = get_terms( array(
        'taxonomy' => 'award',
        'hide_empty' => false,
    ) );
        
        // Sub-field award status.
        $award_status = $group['award_status'] ?? '';
        $output .= '<h3 class="award_status">' . $award_status . '</h3>';
    
        return $output;
    } );
    

    I'm not sure what to do. In case it helps, this is the code for the metaboxes in the backend (though I'm using the Builder extension to create them):

    
    <?php
    add_filter( 'rwmb_meta_boxes', 'awards_display' );
    
    function awards_display( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'      => __( 'Awards', 'your-text-domain' ),
            'id'         => 'awards',
            'post_types' => ['page'],
            'context'    => 'side',
            'class'      => 'awards',
            'fields'     => [
                [
                    'name'              => __( 'Award Info', 'your-text-domain' ),
                    'id'                => $prefix . 'awards_info',
                    'type'              => 'group',
                    'clone'             => true,
                    'sort_clone'        => true,
                    'clone_as_multiple' => true,
                    'add_button'        => __( '+ Add Another', 'your-text-domain' ),
                    'before'            => __( '<div class="awards">', 'your-text-domain' ),
                    'after'             => __( '</div>', 'your-text-domain' ),
                    'class'             => 'awards',
                    'fields'            => [
                        [
                            'name'     => __( 'Year', 'your-text-domain' ),
                            'id'       => $prefix . 'year',
                            'type'     => 'select',
                            'options'  => [
                                2007 => __( '2007', 'your-text-domain' ),
                                2008 => __( '2008', 'your-text-domain' ),
                                2009 => __( '2009', 'your-text-domain' ),
                                2010 => __( '2010', 'your-text-domain' ),
                                2011 => __( '2011', 'your-text-domain' ),
                                2012 => __( '2012', 'your-text-domain' ),
                                2013 => __( '2013', 'your-text-domain' ),
                                2014 => __( '2014', 'your-text-domain' ),
                                2015 => __( '2015', 'your-text-domain' ),
                                2016 => __( '2016', 'your-text-domain' ),
                                2017 => __( '2017', 'your-text-domain' ),
                                2018 => __( '2018', 'your-text-domain' ),
                                2019 => __( '2019', 'your-text-domain' ),
                                2020 => __( '2020', 'your-text-domain' ),
                                2021 => __( '2021', 'your-text-domain' ),
                                2022 => __( '2022', 'your-text-domain' ),
                                2023 => __( '2023', 'your-text-domain' ),
                                2024 => __( '2024', 'your-text-domain' ),
                                2025 => __( '2025', 'your-text-domain' ),
                            ],
                            'required' => true,
                        ],
                        [
                            'name'           => __( 'Award', 'your-text-domain' ),
                            'id'             => $prefix . 'award',
                            'type'           => 'taxonomy_advanced',
                            'taxonomy'       => ['award'],
                            'field_type'     => 'select_tree',
                            'add_new'        => true,
                            'remove_default' => true,
                            'required'       => true,
                        ],
                        [
                            'name'     => __( 'Status', 'your-text-domain' ),
                            'id'       => $prefix . 'award_status',
                            'type'     => 'radio',
                            'options'  => [
                                'Nominee' => __( 'Nominee', 'your-text-domain' ),
                                'Winner'  => __( 'Winner', 'your-text-domain' ),
                            ],
                            'required' => true,
                        ],
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    
    #30164
    Darren MillerDarren Miller
    Participant

    @longnguyen maybe the coding is just over my head here? Do I need MB Views to accomplish this? I currently only have the Core bundle...

    Sorry to be a pest on here, I'm just working on a deadline and hoping to figure out how I'll solve this as soon as I can.

    #30182
    Long NguyenLong Nguyen
    Moderator

    Hi Darren,

    If you set the group cloneable, it will return an array of clone values so you need to create a loop to iterate through items. For example:

        $groups = rwmb_meta( 'awards_info' );
        if ( empty( $groups ) ) {
            return '';
        }
        $output = '';
        
        foreach ( $groups as $group ) {
            // Sub-field year.
            $year = $group['year'] ?? '';
    ...
            }

    Please read more here https://docs.metabox.io/cloning-fields/

    And the taxonomy_advanced field also returns an array of term IDs when retrieving the value, so you need a second loop to get the term ID. For example:

    $term_ids = $group['award'];
    foreach ( $term_ids as $term_id ) {
        $term = get_term( $term_id );
        $output .= '<h3 class="award">' . $term->name . '</h3>';
    }

    Please read more here https://docs.metabox.io/fields/taxonomy-advanced/#template-usage
    https://docs.metabox.io/extensions/meta-box-group/#sub-field-values

    The whole shortcode should be

    add_shortcode( 'awards', function() {
        $groups = rwmb_meta( 'awards_info' );
        if ( empty( $groups ) ) {
            return '';
        }
        $output = '';
        
        foreach ( $groups as $group ) {
            // Sub-field year.
            $year = $group['year'] ?? '';
            $output .= '<h3 class="year">' . $year . '</h3>';
    
            // Sub-field award.
            $term_ids = $group['award'];
            foreach ( $term_ids as $term_id ) {
                $term = get_term( $term_id );
                $output .= '<h3 class="award">' . $term->name . '</h3>';
            }
    
            // Sub-field award status.
            $award_status = $group['award_status'] ?? '';
            $output .= '<h3 class="award_status">' . $award_status . '</h3>';
        }
    
        return $output;
    } );

    Hope that makes sense.

    #30184
    Darren MillerDarren Miller
    Participant

    Oh man I can't thank you enough for breaking this down, I really appreciate it! This makes sense. I'm hoping I can run with this now for other applications. It's working like a charm!

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