How to create function like the_tags() or the_category() in wordpress?

488 Views Asked by At

I created a meta box for post, named as "Resource" where I can select a value from predefined select element in the backend. And after saving it it shows in frontend. Please see the images.

Back-end

Front-end

Now I want this to work like tags, author and categories. When I click it will show all the posts selected with the same value. But do not know where to start.

I used Meta Box plugin and did the following codes in function.php

add_filter( 'rwmb_meta_boxes', 'resource_meta_box' );
function resource_meta_box( $meta_boxes ) {
    
    $meta_boxes[] = array(
        'id'         => 'resource_box',
        'title'      => esc_html__( 'Resource' ),
        'post_types' => array( 'post' ),
        'context'    => 'side',
        'priority'   => 'high',
        'autosave'   => true,

        // List of meta fields
        'fields'     => array(
            // RESOURCE BOX
            array(
                'name'        => esc_html__( '' ),
                'id'          => "resource",
                'type'        => 'select',
                // Array of 'value' => 'Label' pairs for select box
                'options'     => array(
                    'Item 1' => esc_html__( 'Item 1' ),
                    'Item 2' => esc_html__( 'Item 2' ),
                    'Item 3' => esc_html__( 'Item 3' ),
                    'Item 4' => esc_html__( 'Item 4' ),
                    'Item 5' => esc_html__( 'Item 5' ),
                ),
                // Placeholder of Select.
                'placeholder' => esc_html__( 'None' )
            )
        )
    );
    return $meta_boxes;
}

and the following code where it needed to show <?php echo rwmb_meta( 'resource'); ?>

Thanks in advance.

1

There are 1 best solutions below

1
On

you should go for custom taxonomy, cause it have the features required by you inbuilt with it. paste the code given bellow in your theme functions.php file.enter code here

$labels = array(
    'name'                       => _x( 'Resources', 'taxonomy general name', 'textdomain' ),
    'singular_name'              => _x( 'Resource', 'taxonomy singular name', 'textdomain' ),
    'search_items'               => __( 'Search Resources', 'textdomain' ),
    'popular_items'              => __( 'Popular Resources', 'textdomain' ),
    'all_items'                  => __( 'All Resources', 'textdomain' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Edit Resource', 'textdomain' ),
    'update_item'                => __( 'Update WResource', 'textdomain' ),
    'add_new_item'               => __( 'Add New Resource', 'textdomain' ),
    'new_item_name'              => __( 'New Resource Name', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separate Resources with commas', 'textdomain' ),
    'add_or_remove_items'        => __( 'Add or remove Resources', 'textdomain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Resources', 'textdomain' ),
    'not_found'                  => __( 'No Resources found.', 'textdomain' ),
    'menu_name'                  => __( 'Resources', 'textdomain' ),
);

$args = array(
    'hierarchical'          => true,
    'labels'                => $labels,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var'             => true,
    'rewrite'               => array( 'slug' => 'resource' ),
);

register_taxonomy( 'resource', 'your-post-type-slug', $args );

replace the text 'your-post-type-slug' with the post-type slug (e.g. 'post' for Posts or 'page' for Pages etc) of the post type, with which you want to associate the taxonomy.

then use bellow code to retrieve all terms created under 'resource' taxonomy

get_terms( 'resource', array(
    'hide_empty' => false,
) );

create template file, taxonomy-resource.php from archive.php to show filtered (by the newly added taxonomy) post list.

use the_terms(get_the_ID(),'resource', $before, $sep, $after ); to show selected resources for a specific post inside loop.