phpwordpresscustom-post-typecustom-field-type

How to use custom post type field output in a plugin


If you have a custom post type, with multiple fields involved, you may want to use a certain field's output within a plugin. For the custom post type, you simply use <?php the_field('paragraph_1'); ?> to display the content. In a plugin, this does not work. Nothing is output relating to the custom post type. How can this be accomplished?

In functions.php:

    // Add custom taxonomy for post_type=portfolio
function create_portfolio_taxonomies() 
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name' => _x( 'Portfolio Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Categories' ),
    'all_items' => __( 'All Categories' ),
    'parent_item' => __( 'Parent Category' ),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item' => __( 'Edit Category' ), 
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Genre Category' ),
    'menu_name' => __( 'Category' ),
  );    

  register_taxonomy('work-category',array('portfolio'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'work-category' ),
  ));

}
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_portfolio_taxonomies', 0 );

Solution

  • So far, what you just putted there on the code is to register a taxonomy, not a custom post type. In any case, if you want to call the value of a custom field, you can use get_post_meta(): http://codex.wordpress.org/Function_Reference/get_post_meta

    In this case, it would be:

    <?php echo get_post_meta($post->ID,'paragraph_1',true); ?>