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;
}
}
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0