web-dev-qa-db-ja.com

別の投稿で別のギャラリーを表示する

私は[multigallery]関数を使用していますが、別の投稿から添付ファイルを呼び出すためにこの関数を使用する方法があるかどうか疑問に思いました。

例えば、私がTaylor Swiftに関する記事を書いていて、マルチギャラリのショートコードを使って別のTaylor Swiftの記事からの添付ファイルを含めたい場合はどうしたらよいでしょうか。

ショートコードid=にidが投稿番号になるように[multigallery id=???]を追加して、その投稿から添付ファイルを取得するようにします。

これは私が現在function.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
Mihad Aiko

あなたの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です。

1
birgire