如何在POST Text下显示图库快捷代码输出

时间:2021-03-16 作者:Sander1020

所以我在单曲中使用了一个图库的快捷码。php文件用于单个贴子页面。

问题是,库只显示在页面顶部或底部。我想在单篇文章页面的文本下方显示图库。

有没有办法指定快捷码输出的显示位置?谢谢

仅有一个的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-"]\');
        
    }
}

1 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

这个do_shortcode() 函数以字符串的形式返回快捷码输出,该字符串可以用the_content 滤器下面是一个如何做到这一点的示例,

// 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;
}
使用helper函数,我们可以跳过foreach循环并保存几行,因为自定义查询只返回快捷码所需的附件ID。有了这两个额外的参数,我们可以跳过一些处理,使查询速度更快一些。