如何在网站上显示CPT UI插件的数据?

how to show the data of CPT UI plugin on website?

我使用 CPT UI 添加了一些 post 分类法。我在CPTUI里填了两个post数据来练习。现在我想在页面上显示这些 post。我必须编写的所有代码。

为了引入自定义 fields/post 元数据,您需要在模板文件中的 WordPress 循环 (https://codex.wordpress.org/The_Loop) 中编写一些代码。

The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.

例如

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        //
        // Post Content here
        //
    } // end while
} // end if

对于所有 post 元:

$meta = get_post_meta( get_the_ID() ); echo '<pre>'; print_r( $meta ); echo '</pre>';

或者对于单个值:

$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );

有关 WordPress Codex 中的更多信息,请参阅下文:

https://codex.wordpress.org/Custom_Fields

https://developer.wordpress.org/reference/functions/get_post_meta/

您可以使用 Wp_Query 以及您使用 CPT Ui 插件创建的 post 名称来显示那些 post。像,例如我创建了一个名为 school 的 post,然后显示所有 post 学校类型的代码如下:

$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
    $query->the_post();
    echo $query->ID; // it will print the ID of post
endwhile;

希望这会解决问题..