我正在使用[multigallery]
函数,我想知道是否有一种方法可以使用该函数调用其他帖子的附件。
例如,如果我正在写一篇关于泰勒·斯威夫特的文章,并且我想使用多画廊快捷代码包含另一篇泰勒·斯威夫特文章的附件,我会怎么做。
通过添加id=
到短代码[multigallery id=???]
其中id将是帖子编号,它将从该帖子中获取附件。
这是我目前在函数中使用的代码。php文件
function multi_gallery_shortcode(){
global $post;
$before = sprintf(\'<div class="gallery-before">%s</div>\',$post->post_title);
$gallery_sc = sprintf(\'<div id="gallery-1">[gallery columns="6" order="ASC" orderby="menu_order" include="%s" link="gallery"]</div>\',get_random_gallery_images($post->ID));
$after = sprintf(\'<div class="gallery-after"><span class="galNum">%d</span> Photos</div><div style="clear:both;"></div>\', get_total_attachments($post->ID));
return $before . do_shortcode($gallery_sc) . $after;
}
function get_random_gallery_images($post_id){
global $wpdb;
$ids = "";
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'posts_per_page\' => 6,
\'post_status\' => \'any\',
\'orderby\' => \'rand\',
\'post_parent\' => $post_id,
);
$attachments = get_posts($args);
if ($attachments) {
$tmp=array();
foreach ($attachments as $attachment) {
$tmp[] = $attachment->ID;
}
$ids=implode(",",$tmp);
}
return $ids;
}
function get_total_attachments($post_id){
$args=array(
\'post_parent\' => $post_id,
\'post_mime_type\' => \'image\',
\'post_type\' => \'attachment\',
);
return count(get_children( $args ));
}
add_shortcode(\'multigallery\', \'multi_gallery_shortcode\');