Wordpress Custom Fields for Custom Post Types

2019-03-04 08:16发布

问题:

This issue has arisen for several people in the past but the solutions for their issues have not worked for me and I have tried alot!

In Wordpress I have created 3 custom post types. 1 for 'videos', 'news' and 'music' and each of these post to their own page. I want to add custom fields so I can have 'artist' 'year of release' 'featuring' and 'about the album' for the music posts for instance.

I have installed Advanced Custom Fields and I can add custom fields to each of these so when the user clicks 'add new' the fields are visible. But the issue I have is that the output of these fields is not displaying on the site when I visit the page.

I created news.php, music.php and videos.php from the single.php file with the following:

    <?php
/**
 * Template Name: music Page
 *
 * Selectable from a dropdown menu on the edit page screen.
 */

get_header(); ?>

    <div id="primary" class="site-content">
        <div id="content" role="main">
<?php query_posts( 'post_type=music'); ?>
<?php the_meta(); ?> 
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content',  get_post_format() ); ?>
                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

And in functions.php I have the following:

/*---------music Custom Post Types---------------------------------*/

function my_custom_post_music() {
    $labels = array(
        'name'               => _x( 'music', 'post type general name' ),
        'singular_name'      => _x( 'music', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New music' ),
        'edit_item'          => __( 'Edit music' ),
        'new_item'           => __( 'New music' ),
        'all_items'          => __( 'All music' ),
        'view_item'          => __( 'View music' ),
        'search_items'       => __( 'Search music' ),
        'not_found'          => __( 'No music found' ),
        'not_found_in_trash' => __( 'No music found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Music'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our music and music specific data',
        'public'        => true,
        'menu_position' => 15,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,

    );
    register_post_type( 'music', $args );   
}
add_action( 'init', 'my_custom_post_music' );



function my_taxonomies_music() {
    $labels = array(
        'name'              => _x( 'music Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'music Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search music Categories' ),
        'all_items'         => __( 'All music Categories' ),
        'parent_item'       => __( 'Parent music Category' ),
        'parent_item_colon' => __( 'Parent music Category:' ),
        'edit_item'         => __( 'Edit music Category' ), 
        'update_item'       => __( 'Update music Category' ),
        'add_new_item'      => __( 'Add New music Category' ),
        'new_item_name'     => __( 'New music Category' ),
        'menu_name'         => __( 'music Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'music_category', 'music', $args );
}
add_action( 'init', 'my_taxonomies_music', 0 );


/*---------news Custom Post Types---------------------------------*/

function my_custom_post_news() {
    $labels = array(
        'name'               => _x( 'news', 'post type general name' ),
        'singular_name'      => _x( 'news', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New news' ),
        'edit_item'          => __( 'Edit news' ),
        'new_item'           => __( 'New news' ),
        'all_items'          => __( 'All news' ),
        'view_item'          => __( 'View news' ),
        'search_items'       => __( 'Search news' ),
        'not_found'          => __( 'No news found' ),
        'not_found_in_trash' => __( 'No news found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'News'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our news and news specific data',
        'public'        => true,
        'menu_position' => 10,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
    );
    register_post_type( 'news', $args );    
}
add_action( 'init', 'my_custom_post_news' );

Does anyone know what I am missing to get this working or what I need to do.

Any suggestions much appreciated.

回答1:

To display the value of custom fields in your loop you can use this snippet of code:

<?php query_posts( 'post_type=music'); ?>
  <?php while ( have_posts() ) : the_post(); ?>
   <?php get_template_part( 'content',  get_post_format() ); ?>

   <?php $what_name_you_want=get_post_meta($post->ID,'Your Custom Field Name',true); ?>

    <?php echo $what_name_you_want; ?>// This call the value of custom field


                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>

Tell me if it works!



回答2:

Output the custom field data from ACF with.

the_field('the-field-name');

get_field('the-field-name') is used for conditionals ex if (get_field('my-field'): etc. You can also print the contents using

echo get_field('the-field-name');

I would say that your issue with Vimeo shortcodes and Custom Fields might be related to the plugin wasent built to run thru custom fields. It might be that it only checks thru the_contents() for the shortcode.



回答3:

Basically you need two steps to add a Custom Field to a Custom Post Type:

  1. Create a metabox which holds your Custom Field
  2. Save your Custom Field to the database Add a Custom Field called "function" to a Custom Post Type called "prefix-teammembers".

First add the metabox:

function prefix_teammembers_metaboxes( ) {
   global $wp_meta_boxes;
   add_meta_box('postfunctiondiv', __('Function'), 'prefix_teammembers_metaboxes_html', 'prefix_teammembers', 'normal', 'high');
}
add_action( 'add_meta_boxes_prefix-teammembers', 'prefix_teammembers_metaboxes' );

If your add or edit a "prefix-teammembers" the add_meta_boxes_{custom_post_type} hook is triggered.

function prefix_teammembers_metaboxes_html()
{
    global $post;
    $custom = get_post_custom($post->ID);
    $function = isset($custom["function"][0])?$custom["function"][0]:'';
?>
    <label>Function:</label><input name="function" value="<?php echo $function; ?>">
<?php
}

In the second step you have your custom field to the database. On saving the save_post_{custom_post_type} hook is triggered

function prefix_teammembers_save_post()
{
    if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new? 
    global $post;
    update_post_meta($post->ID, "function", $_POST["function"]);
}   

add_action( 'save_post_prefix-teammembers', 'prefix_teammembers_save_post' );