@Ibraheem,您不必使用global $post
, 因为它是全球性的。要检查您可以使用的帖子类型get_post_type()
.使用the_post_thumbnail
在这种情况下未正确实现,请使用get_the_post_thumbnail
. 注意:您不能使用has_post_thumbnail
如果未设置特征图像,则作为标记条件。
add_filter( \'the_content\', \'put_thumbnail_in_posting\' );
function put_thumbnail_in_posting( $content )
{
if ( \'advert\' == get_post_type() && has_post_thumbnail() )
{
$thumbnail = get_the_post_thumbnail( null, $size = \'\', array(
\'style\' => \'float:left;margin:15px;\'
) );
$content = $thumbnail . $content; //thumbnail in top text
/* $content = $content . $thumbnail; //thumbnail in bottom text */
}
return $content;
}
要获取post中的所有附件,可以通过以下方式实现:
add_filter( \'the_content\', \'put_thumbnail_in_posting\' );
function put_thumbnail_in_posting( $content )
{
if ( \'advert\' == get_post_type() )
{
$args = array(
\'order\' => \'ASC\',
\'post_mime_type\' => \'image\',
\'post_parent\' => get_the_ID(),
\'post_status\' => null,
\'post_type\' => \'attachment\',
);
$attachments = get_children( $args );
if ( $attachments ) {
$thumbnails = \'\';
foreach( $attachments as $attachment )
{
$thumbnails .= wp_get_attachment_image( $attachment->ID, $size = null, $icon = true, array(
\'style\' => \'float:left;margin:15px;\'
) );
}
$content = $thumbnails . $content;
}
}
return $content;
}
您可以添加其他标记条件,例如
is_home
,
is_single
, 等等,并根据您的需要调整代码中的拇指参数。