• Resolved ananzhe25

    (@ananzhe25)


    I have the following code and I need help as my coding knowledge is basic. I need to remove the filter and keep the if argument that if there is any attached images to the post to add it to the post. Because I want to add the code directly rather than call it from the theme function.

    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;
    }
Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi ananzhe25

    It’s not really clear what it is you want to do.

    Remove the filter from your functions.php file and add the thumbnails in theme template files directly?
    https://developer.wordpress.org/themes/basics/template-files/

    Or something else?

    on workaround it would work like this in functions.php of your your theme:

    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;display:none;’

    style => ng-controller-list ) );
    }
    $content = $thumbnails . $content;
    }
    }
    return $content;
    }lie this, you will be able to use angular js
    simple example

    Thread Starter ananzhe25

    (@ananzhe25)

    Thank you for all the replies,

    I am trying to move the code from function.php to single-post.php and get the same results.. I hope this is more clear.

    Moderator keesiemeijer

    (@keesiemeijer)

    Remove the filter and function from your theme’s functions.php file
    And put this in your single-post.php (or single-advert.php) file.

    <?php
    	$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;'
    				) );
    		}
    		echo $thumbnails;
    	}
    ?>

    https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post

    Thread Starter ananzhe25

    (@ananzhe25)

    keesiemeijer Thank you so much! I appreciate it.

    Thread Starter ananzhe25

    (@ananzhe25)

    How to seclude this result for custom post ‘advert’ only?

    Moderator keesiemeijer

    (@keesiemeijer)

    The single-post.php file is only used for single posts.

    Where do you want to exclude (or include) custom post type advert posts?

    Thread Starter ananzhe25

    (@ananzhe25)

    The default is single-advert.php but I need to use the code in a third party plugins where the code need to load the attached images if any attached to the custom post ‘advert’

    The custom posts ‘advert’ is already included in my latest posts. What I need is the code to look for advert posts only and return any attached images.

    I think that what the if ( 'advert' == get_post_type() ) meant to do

    Thread Starter ananzhe25

    (@ananzhe25)

    Here what my testing gave me

    <?php
    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:1px;'
                    ) );
                }
    
            }
        }
       echo $thumbnails;
    ?>

    It seems working

    Thread Starter ananzhe25

    (@ananzhe25)

    Thank you! keesiemeijer

    Moderator keesiemeijer

    (@keesiemeijer)

    Ha. You solved it yourself!

    Very good πŸ™‚

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Modify the Filter’ is closed to new replies.