如何在插件中使用自定义帖子类型字段输出

时间:2012-10-03 14:45:25

标签: php wordpress wordpress-plugin custom-post-type custom-field-type

如果您有自定义帖子类型,涉及多个字段,您可能希望在插件中使用某个字段的输出。对于自定义帖子类型,您只需使用<?php the_field('paragraph_1'); ?>即可显示内容。在插件中,这不起作用。没有与自定义帖子类型相关的输出。如何实现这一目标?

在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 );

1 个答案:

答案 0 :(得分:0)

到目前为止,您刚刚在代码中使用的是注册分类,而不是自定义帖子类型。在任何情况下,如果要调用自定义字段的值,可以使用get_post_meta(): http://codex.wordpress.org/Function_Reference/get_post_meta

在这种情况下,它将是:

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