在单独的帖子中显示不同的画廊

时间:2013-04-09 作者:Mihad Aiko

我正在使用[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\');

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

您可以尝试此功能,而不是multi_gallery_shortcode():

function multi_gallery_shortcode($atts, $content=null) {
    extract( shortcode_atts( array(
                \'pid\' => 0,
        ), $atts ) );

    //format the input
    $pid = intval($pid);

    // construct a post object dependent on the input value
    if($pid>0){
        // query a post object
        $pobj = get_post( $pid );
    }else{
        global $post;
        // current post object
        $pobj = &$post;
    }

    // construct gallery title
    $gallery_title = $pobj->post_title; // customize to your needs

    // construct gallery url
    $gallery_url = ""; // default first image gallery url
    $attributes = wp_get_attachment_image_src( get_first_gallery_image($pobj->ID),\'thumbnail\'); // customize  to your needs
    if(isset($attributes[0]))
        $gallery_url = $attributes[0];

    // format output:
    $before     = sprintf(\'<div class="gallery-before"><a href="%s">%s</a></div>\', $gallery_url , $gallery_title );
    $gallery_sc = sprintf(\'[gallery columns="1" order="ASC" orderby="menu_order" include="%s" link="gallery"]\',get_random_gallery_images($pobj->ID));
    $after      = sprintf(\'<span class="galNum">%d</span> Photos.\', get_total_attachments($pobj->ID));

    return $before.do_shortcode($gallery_sc).$after;
}
您在帖子/页面中这样使用它的位置:

[multigallery pid="1234"]
在哪里pid 是帖子id。

结束

相关推荐

子主题中的Comments.php更改不会显示在站点上

我正在尝试自定义评论中的评论模板。php和so对代码进行了一些更改,包括wp\\u list\\u comments()中的回调函数(我在functions.php中定义了该函数)所有这些更改在我的本地开发机器上都可以正常工作,但不知何故,当我上传评论时,我看不到任何更改。php和函数。php在我的主题文件夹的根目录中。我尝试过取消激活和重新激活所有插件,但仍然没有成功。现在我快发疯了。有人知道可能出了什么问题吗?