Question

So i made a blog with posts, and I used this code to display a shortcode gallery under the content in a post.

The problem is, now the "All Posts" Page has no posts in them. And I think its because the $content variable that i use.

So i used is_single() to only execute the add_content_after function in a single post page. But no gallery displays now.

Anybody know how i can fix this issue? Thanks

functions.php:

if ( is_single() ) {
    add_filter('the_content', 'add_content_after');

      function add_content_after($content) {

        global $post;

            if ( $post->post_status == 'publish' ) {
            $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id()
            ) );

            if ( $attachments ) {
                $atts = array();
                foreach ( $attachments as $attachment ) {
                    $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
                    $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );

                    $atts[] = $attachment->ID;                                  
                }                                                           
                    $shortcode =  do_shortcode('[av_gallery ids="' . implode(",", $atts) . '" type="slideshow" preview_size="large" crop_big_preview_thumbnail="avia-gallery-big-crop-thumb" thumb_size="portfolio" columns="4" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]');

                $fullcontent = $content . $shortcode;

                return $fullcontent;

            }
        } 
    }
}
Was it helpful?

Solution

Move is_single() condition to be inside the function, and you have to return the original content if the condition isn't satisfied.

Your code will look like this:

add_filter('the_content', 'add_content_after');

function add_content_after($content) {
    global $post;
    
    if ( is_single() ) {
        if ( $post->post_status == 'publish' ) {
            $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id()
            ) );

            if ( !empty($attachments) ) {
                $atts = array();
                foreach ( $attachments as $attachment ) {
                    $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
                    $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );

                    $atts[] = $attachment->ID;                                  
                }                                                           
                    $shortcode =  do_shortcode('[av_gallery ids="' . implode(",", $atts) . '" type="slideshow" preview_size="large" crop_big_preview_thumbnail="avia-gallery-big-crop-thumb" thumb_size="portfolio" columns="4" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]');

                $fullcontent = $content . $shortcode;

                return $fullcontent;

            }
        } 
    }
    
    return $content;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top