1
2
3
4
五
6 |
<?php global $wp_query ; $postid = $wp_query ->post->ID; echo get_post_meta( $postid , 'Your-Custom-Field' , true); wp_reset_query(); ?> |
get_post_meta()
函数来获取和输出自定义字段数据。
不要忘记使用您的实际自定义字段更改Your-Custom-Field。
您可以自定义代码以满足您的需求。您还可以使用其他查询参数来获取和显示不同帖子和页面的自定义字段数据。
我们来看看另一个例子。这个使用WP_Query类,这是一种更好,更灵活的方式在WordPress主题文件中使用多个循环。
只需将此代码添加到您想要显示自定义字段的主题或子主题中。
1
2
3
4
五
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
$args = array ( // Post or Page ID 'p' => 231, ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query ->have_posts() ) { while ( $the_query ->have_posts() ) { $the_query ->the_post(); echo get_post_meta( get_the_ID(), 'Mood' , true); } /* Restore original Post Data */ wp_reset_postdata(); } else { echo 'Nothing found' ; } |