根据你发布的代码,我想cl_guest
post meta包含一个post ID数组。基于这个假设,您可以这样做。
1) 将这些助手函数添加到主题的功能中。php文件
// custom template tag to render guests
function render_guests( array $guests ) {
if ( $guests ) {
// you could use foreach loop instead of array_walk
array_walk( $guests, \'render_guest\' );
}
}
// Helper function to get guest ids
function get_guest_ids( int $id ) {
$meta = get_post_meta( $id, \'cl_guest\', true);
return ( $meta && is_array( $meta ) ) ? array_map( \'intval\', $meta ) : array();
}
// helper function to render single guest
function render_guest( int $id ) {
// get meta if needed
$meta = get_guest_meta( $id );
$title = get_the_title( $id );
$thumbnail = get_guest_thumbnail( $id, $title, $title );
// modify output as needed
echo \'<div class="guest">\' . $thumbnail . $title . \'</div>\';
}
// helper function
function get_guest_meta( int $id ) {
return get_post_meta( $id, \'some_meta_key\', true );
}
// helper function
function get_guest_thumbnail( int $id, string $alt = \'\', string $title = \'\' ) {
return get_the_post_thumbnail(
$id,
\'thumbnail\',
array(
\'class\' => "img-responsive img-circle",
\'alt\' => esc_attr( $alt ),
\'title\' => esc_attr( $title )
)
);
}
2)将自定义tempalte标记添加到您要显示来宾的帖子模板中
<?php render_guests( get_guest_ids( get_the_ID() ) ); ?>
还有,你可能想知道为什么我要将代码拆分为多个函数。原因是我只是在练习一种新的编码风格,其中每个函数都应该做一件事
EDIT 1下面是如何分别渲染图像和标题+元数据。第一对新的助手函数。
function render_guest_images( array $ids ) {
if ( $ids ) {
array_walk( $ids, \'render_guest_image\' );
}
}
function render_guest_image( int $id ) {
$title = get_the_title( $id );
echo get_guest_thumbnail( $id, $title, $title );
}
function render_guest_infos( array $ids ) {
if ( $ids ) {
array_walk( $ids, \'render_guest_info\' );
}
}
function render_guest_info( int $id ) {
$title = get_the_title( $id );
$meta = get_guest_meta( $id ); // this could either return data that you render here or ready-to-use html that you just concatenate to the echo below
echo \'<div class="guest-info">\' . $title . \'</div>\';
}
// add this before you add the rendering functions on your post template
$guest_ids = get_guest_ids( get_the_ID() );
// somewhere on your post template
render_guest_images( $guest_ids );
// somewhere else on your post template
render_guest_infos( $guest_ids );
EDIT 2
没有单独的辅助对象和渲染函数
// before any loops
$guest_ids = get_post_meta( get_the_id(), \'cl_guest\', true);
$guest_ids = ( $guest_ids && is_array( $guest_ids ) ) ? array_map( \'intval\', $guest_ids ) : array();
$guest_posts = array();
if ( $guest_ids ) {
foreach ( $guest_ids as $guest_id ) {
if ( $post = get_post( $guest_id ) ) {
$guest_posts[] = $post;
}
}
}
// titles only
if ( $guest_posts ) {
foreach( $guest_posts as $guest_post ) {
echo esc_html( $guest_post->post_title );
}
}
// thumbnails
if ( $guest_posts ) {
foreach( $guest_posts as $guest_post ) {
echo get_the_post_thumbnail(
$guest_post->ID,
\'thumbnail\',
array(
\'class\' => "img-responsive img-circle",
\'alt\' => esc_attr( $guest_post->post_title ),
\'title\' => esc_attr( $guest_post->post_title )
)
);
}
}
// meta only
if ( $guest_posts ) {
foreach( $guest_posts as $guest_post ) {
$meta = get_post_meta( $guest_post->ID, \'some_meta_key\', true );
// do something with meta
}
}