Wordpress 显示自定义帖子类型及其字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11216787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress displaying custom post types and their fields
提问by lowercase
I have set up a Custom Post Type called 'RELEASES' - think music cd release.
我已经设置了一个名为“RELEASES”的自定义帖子类型 - 想想音乐 cd 发行版。
This post type has fields named 'release_artist', 'release_title', 'release_date', 'release_artwork' and 'release_tracklisting' for entering all of the relevant music cd information.
此帖子类型具有名为“release_artist”、“release_title”、“release_date”、“release_artwork”和“release_tracklisting”的字段,用于输入所有相关的音乐 CD 信息。
I am having real trouble actually displaying this information in my Wordpress template. I have really only had luck outputting a list of the titles and none of the other data.
我在 Wordpress 模板中实际显示此信息时遇到了真正的麻烦。我真的只是很幸运地输出了标题列表而没有输出任何其他数据。
Any idea what I put in the LOOP to display all of the information? Preferably each in its own LIST item so I can style each separately?
知道我在 LOOP 中放了什么来显示所有信息吗?最好每个都在自己的 LIST 项目中,这样我就可以分别设计每个项目吗?
Any thoughts greatly appreciated.
任何想法都非常感谢。
回答by Tomek Buszewski
Are those custom fields? If yes, try what codex.wordpress.orgis saying. Better yet, try ACF plugin.
这些是自定义字段吗?如果是,请尝试使用codex.wordpress.org所说的内容。更好的是,尝试ACF 插件。
-- edit
- 编辑
If you want to display parts of your pages on other ones (eg. on your home), you need to use query_posts. This is fairly simple function. For your loop, try something like this:
如果您想在其他页面(例如在您的家中)显示您的部分页面,则需要使用query_posts。这是相当简单的功能。对于您的循环,请尝试以下操作:
<?php global $wp_query; query_posts(array( 'post_type' => 'releases' ));
<?php global $wp_query; query_posts(array( 'post_type' => 'releases' ));
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'Opis nazwy'); ?>
<li <?php post_class(); ?>><a href="<?php the_permalink(); ?>"><?php if($key) { echo $key[0]; } else { the_title(); }; ?></a></li>
<?php
endwhile;
wp_reset_query();
?>
$key
is a single value, here set to release_artists
. It's purely for testing. If it works - feel free to define your own variables.
$key
是单个值,此处设置为release_artists
。它纯粹是为了测试。如果它有效 - 随意定义您自己的变量。
回答by Ryan
You can use get_post_metato pull in your fields as needed. Inside your loop, you can start with the following:
您可以根据需要使用get_post_meta拉入您的字段。在您的循环中,您可以从以下内容开始:
<?php
$release_artist = get_post_meta($post->ID, 'release_artist', true);
$release_title = get_post_meta($post->ID, 'release_title', true);
?>
<ul>
<li class="release_artist">
<?php echo $release_artist; ?>
</li>
<li class="release_title">
<?php echo $release_title; ?>
</li>
</ul>
回答by Theunis
From most of the documentation I've seen online, query_posts shouldn't be the go-to function for creating custom queries and loops. The following code snippet might be a good starting point. You should be able to use this inside or outside of the main loop of your themes template files.
从我在网上看到的大多数文档来看,query_posts 不应该是创建自定义查询和循环的首选函数。以下代码片段可能是一个很好的起点。您应该能够在主题模板文件的主循环内部或外部使用它。
$args = array(
'post_type' => 'release', //remember this is-case sensitive
'posts_per_page' => -1,
);
$releaseQuery = new WP_Query( $args );
if ( $releaseQuery->have_posts() ) :
while ( $releaseQuery->have_posts() ) :
$releaseQuery->the_post();
// Fetching the post ID for demonstration and for use later
$c_id = get_the_ID();
// After running the_post(), alot of the Wordpress functions (not all) can now be used without supplying the post ID.
echo get_the_title();
// You could also have used get_the_title($c_id);
// Then:
echo get_post_meta($c_id, 'release_title', true);
echo get_post_meta($c_id, 'release_artist', true);
endwhile;
endif;
// Return to the current page's main query
wp_reset_query();
// This should now display the page's title
the_title();
回答by user177468
You should use:
你应该使用:
<?php the_field('field_name') ?>
inside your loop. Hope it helps!
在你的循环内。希望能帮助到你!
回答by HunterGatherer
About your ID's question:
关于你身的问题:
Every item, like posts and pages in WordPress have an "ID", but they are not normally shown in the lists of them. There are a number of plugins that will add an "ID" column in your admin. Search Wordpress.org for "show ids" and pick one you like. Install it. Activate it. You'll see the ids.
每个项目,如 WordPress 中的帖子和页面都有一个“ID”,但它们通常不会显示在它们的列表中。有许多插件会在您的管理员中添加“ID”列。在 Wordpress.org 中搜索“show ids”并选择您喜欢的一个。安装它。激活它。你会看到ID。