admin管理员组文章数量:1302407
so I have a shortcode for a gallery I'm using in the Single.php file for the single post pages.
The problem is that the gallery only shows either at the top of the page, or at the bottom. I would like to show the gallery right under the text on the Single Post Page.
Is there a way to specify where the shortcode output displays? Thanks
Single.php:
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;
}
$aantal = count($atts);
echo 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="' . implode(",", $aantal) . '" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]');
}
}
so I have a shortcode for a gallery I'm using in the Single.php file for the single post pages.
The problem is that the gallery only shows either at the top of the page, or at the bottom. I would like to show the gallery right under the text on the Single Post Page.
Is there a way to specify where the shortcode output displays? Thanks
Single.php:
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;
}
$aantal = count($atts);
echo 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="' . implode(",", $aantal) . '" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]');
}
}
Share
Improve this question
asked Mar 16, 2021 at 11:09
Sander1020Sander1020
153 bronze badges
2
- Either you need to overwrite the single.php file of your current active template OR you need to find a hook after the post/text in your theme and add an action to it, which spits out the shortcode. – user3135691 Commented Mar 16, 2021 at 11:31
- Ah Thanks for the reply! I'm now using the hook method. Do you know how I can get the Gallery code in a variable so i can return it after the content? I'm using this as a reference: njengah/the_content_wordpress-add-to-end-of-content – Sander1020 Commented Mar 16, 2021 at 13:15
1 Answer
Reset to default 0The do_shortcode() function returns the shortcode output as a string, which can be appenend to the post content with the_content filter. Here's an example how to do that,
// add filter with late priority
add_filter('the_content', 'my_post_attachments_gallery', 99);
function my_post_attachments_gallery($content) {
// modify only published posts
if (
is_singular( 'post') &&
'publish' === get_post_status()
) {
// get attachemnt ids
$attachment_ids = my_post_attachment_ids();
if ( $attachment_ids ) {
// do_shortcode returns the shortcode output as a string
// which we can append to the content
$content .= do_shortcode('[av_gallery ids="' . implode(",", $attachment_ids) . '" type="slideshow" preview_size="large" crop_big_preview_thumbnail="avia-gallery-big-crop-thumb" thumb_size="portfolio" columns="' . count($attachment_ids) . '" imagelink="lightbox" lazyload="avia_lazyload" av_uid="av-jgesnq4m" custom_class="av-gallery-style-"]');
}
}
// return content back to the_content filter
return $content;
}
// helper function
function my_post_attachment_ids( $post = null ) {
$post = get_post($post);
$query = new WP_Query(array(
'post_type' => 'attachment',
'posts_per_page' => 500,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id($post),
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'fields' => 'ids',
));
return $query->posts;
}
With the helper function we can skip the foreach loop and save a few lines as the custom query returns just the attachment IDs, which the shortcode needs. And with the couple extra parameters we can skip some processing and make it the query a little faster.
本文标签: How can i display gallery shortcode output under Post text
版权声明:本文标题:How can i display gallery shortcode output under Post text 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741670261a2391560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论