这个问题在过去已经出现过几个人,但是他们的问题的解决方案对我没有用,我已经尝试了很多!
在Wordpress中,我创建了3种自定义帖子类型。 1为“视频”,“新闻”和“音乐”,每个都发布到他们自己的页面。我想添加自定义字段,以便我可以为“音乐帖子”提供“艺术家”发布年份''特色'和'关于专辑'。
我已经安装了高级自定义字段,我可以为每个字段添加自定义字段,因此当用户点击“添加新”时,字段是可见的。但我遇到的问题是,当我访问该页面时,这些字段的输出没有显示在网站上。
我使用以下内容从single.php文件创建了news.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' );
有没有人知道我缺少什么来使这项工作或我需要做什么。
任何建议都非常感谢。
答案 0 :(得分:0)
要在循环中显示自定义字段的值,您可以使用以下代码段:
<?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. ?>
告诉我它是否有效!
答案 1 :(得分:0)
使用。
从ACF输出自定义字段数据the_field('the-field-name');
get_field(&#39; the-field-name&#39;)用于条件ex if(get_field(&#39; my-field&#39;):等。您还可以使用打印内容p>
echo get_field('the-field-name');
我想说你的Vimeo短代码和自定义字段的问题可能与通过自定义字段运行构建的插件wasent有关。它可能只是通过the_contents()检查短代码。
答案 2 :(得分:0)
基本上,您需要两个步骤将自定义字段添加到自定义帖子类型:
首先添加元数据:
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' );
如果您添加或编辑“prefix-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' );