WordPress的自定义字段自定义日志类型(Wordpress Custom Fields for

2019-08-31 15:59发布

这个问题已经出现了几个人过去,但对于他们的问题解决方案并没有为我工作,我有很多的尝试!

在WordPress的我已创建3种自定义日志类型。 1“视频”,“新闻”和“音乐”和每一个岗位,以自己的页面。 我想添加自定义字段,所以我可以有“艺术家”发布的“年”“特色”和“关于册”为实例的音乐帖子。

我已经安装了先进的自定义字段,我可以添加自定义字段到每一种这样当用户点击“添加新”领域都可见。 但我有问题是这些领域的输出不会显示在网站上,当我访问该网页。

我创建news.php,从下面的single.php中文件music.php和videos.php:

    <?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的我有以下几点:

/*---------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' );

有谁知道我缺少得到这个工作或我需要做什么。

任何建议非常赞赏。

Answer 1:

要显示在你的循环中,您可以使用此代码段的自定义字段的值:

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

告诉我,如果它的工作原理!



Answer 2:

从ACF与输出的自定义字段数据。

the_field('the-field-name');

get_field(下称“场名”)用于条件语句前,如果(get_field(“我的场”)等,还可以使用打印的内容

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

我要说的是,以插件wasent建通自定义字段与运行Vimeo的简码和自定义字段您的问题可能有关。 这可能是只检查直通the_contents()的简码。



Answer 3:

基本上,你需要两个步骤,将自定义字段添加到自定义帖子类型:

  1. 创建持有您的自定义字段一个metabox
  2. 保存您的自定义字段到数据库中添加自定义字段名为“功能”到一个名为“前缀teammembers”自定义文章类型。

首先添加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' );

如果您添加或编辑“前缀teammembers”的add_meta_boxes_{custom_post_type}钩被触发。

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
}

在第二个步骤,你有你的自定义字段到数据库。 在保存save_post_{custom_post_type}钩被触发

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


文章来源: Wordpress Custom Fields for Custom Post Types