自定义帖子类型的Wordpress自定义字段 [英] Wordpress Custom Fields for Custom Post Types

查看:98
本文介绍了自定义帖子类型的Wordpress自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,这个问题已经出现在几个人身上,但是解决他们问题的方法对我没有用,我已经尝试了很多!

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!

在Wordpress中,我创建了3种自定义帖子类型。 1代表视频,新闻和音乐,并且每个都张贴到自己的页面上。我想添加自定义字段,以便为音乐帖子添加艺术家,发行年份,功能和关于专辑。

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.

我创建了news.php,music.php和视频.php来自single.php文件,其内容如下:

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(); ?>

在functions.php中,我有以下内容:

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.

任何建议都值得赞赏。

推荐答案

在以下位置显示自定义字段的值您可以在循环中使用以下代码段:

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. ?>

告诉我是否可行!

这篇关于自定义帖子类型的Wordpress自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆