有一个插件用于定义multiple featured images
您还可以显示每个帖子的附件(上载到帖子的图像将被视为“附加到”该帖子。因为这涉及到另一个查询,而且可能不会经常更改。我开发了一个函数,可以获取附件并使用瞬态API保存它们。
//Get attachments array for specific post/page
function kia_get_attachments($post_id = NULL){
global $post;
$post_id = ( NULL === $post_id) ? $post->ID : $post_id;
if ( false === ( $attachments_array = get_transient( \'kia_post_attachments\' ) ) || !isset($attachments_array[$post_id] ) ) {
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'post_parent\' => $post_id,
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'post_mime_type\' => array(\'image\'),
);
$attachments = get_children($args);
$attachments_array[$post_id] = $attachments;
set_transient( \'kia_post_attachments\', $attachments_array );
}
return $attachments_array[$post_id];
}
// refresh transient on save
function kia_delete_attachment_transient($post_id) {
$attachments_array = get_transient( \'kia_post_attachments\' );
if(!isset($attachments_array[$post_id])) return $post_id;
//if this post was in the transient, then delete it from the transient
unset($attachments_array[$post_id]);
set_transient( \'kia_post_attachments\', $attachments_array );
}
add_action( \'save_post\', \'kia_delete_attachment_transient\' );
然后在上面的代码中替换整个
<aside class="side_bar">
循环的一部分包括以下内容:
$attachments = kia_get_attachments();
if( !empty ($attachments) ) : ?>
<aside class="side_bar">
<ul>
<?php
foreach ( $attachments as $attachment ) :
echo \'<li>\' . wp_get_attachment_image( $attachment->ID ) . \'</li>\';
endforeach;
?>
</ul>
</aside>
<?php endif;
我讨厌“附件”和处理如何将图像附加到帖子上,但这是目前我所知道的最好的方式。