@Ibraheem, you don’t have to use global $post
, since it is in global. To check post type you can use get_post_type()
.
Using the_post_thumbnail
is not properly implemented in this case, instead use get_the_post_thumbnail
. Note: you can’t use has_post_thumbnail
as tag condition if you are not set featured image.
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;
}
To get all attachment in post, you can implement with the following way:
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;
}
You can add another tag condition such as is_home
, is_single
, etc. and tweak thumb arguments in code that suit with your need.