I have the following code for a metabox which allows the user to select a post category but I cant seem to ouput the chosen option on the front end. Any ideas ?

Heres what I have so far :

// Choose Category For Posts
    $cmb->add_field( array(
        'name'    => esc_html__( 'Category', 'majestica' ),
        'desc'    => esc_html__( 'Category of Posts to Display. If using one of the templates that displays a number of posts.', 'majestica' ),
        'id'      => 'majestica_posts_category',
        'taxonomy'       => 'category', //Enter Taxonomy Slug
        'type'           => 'taxonomy_select',
    ) );

And on the front end :

$categoryname = get_post_meta( get_the_ID(), 'majestica_posts_category', true );
echo $categoryname;

It doesnt display anything, I simply want it to echo out the chosen category.

Please help.

  • Are you sure that get_the_ID gives you the correct ID for a post? Sometimes themes are using custom loops and the global $post variable gets changed... – Krzysiek Dróżdż Apr 24 '19 at 16:23
  • Hi, i have similar things on the same page, it just wont work for the select taxonomy feild – Scott Hunter Apr 25 '19 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.