实际上,任何 CPT 都有自己的 slug。与非自定义帖子类型(例如帖子、页面或附件)的方式相同(分别为 post、page 和 attachment)。
但请注意不要将自定义帖子类型与该类型的实际帖子混淆。 Wordpress 中的任何帖子(当然包括 CPT 的帖子)都有一个 ID。如果您想使用任何其他帖子/页面的 ACF 的 get_field 或 the_field 查询自定义字段值您必须使用 ID,如您的示例中所示:
<?php the_field('shop_content_box', $other_page); ?>
所以,如果你只知道$other_page_slug(我想知道你是如何单独获得 slug 的......) 你应该检索它的 Post 对象。见:Get a post by its slug。
<?php
function the_field_by_slug( $field, $slug, $cpt = 'post' ) {
$args = [
'name' => $slug,
'post_type' => $cpt,
'post_status' => 'publish',
'posts_per_page' => 1
];
$my_post = get_posts( $args );
if( $my_post ) {
the_field( $field, $my_post->ID );
}
}
the_field_by_slug( 'shop_content_box', $other_post_slug, $custom_post_type_slug );
?>