将图像计数添加到多图库

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

好的,我正在使用[多画廊]功能,它工作得很好,但我想补充一点。

这是我在函数中用于multigallery的代码。php文件

function get_random_gallery_images(){ 
global $wpdb,$post; 
$ids = ""; 
$counter = 0; 
$number_of_posts = 6; 
$args = array( 
\'post_type\' => \'attachment\', 
\'numberposts\' => 6, 
\'post_status\' => null, 
\'orderby\' => \'rand\', 
\'post_parent\' => $post->ID 
); 
$attachments = get_posts($args); 
if ($attachments) { 
foreach ($attachments as $attachment) {
if ($counter != 0) { 
$ids .= \',\'.$attachment->ID; 
} 
else { 
$ids .= $attachment->ID; 
} 
$counter++; 
} 
} 
return $ids; }
function multi_gallery_shortcode()
{
$attachment_ids = get_random_gallery_images();
return do_shortcode(\'[gallery columns="6" order="ASC" orderby="menu_order"      include="\'.$attachment_ids.\'" link="gallery"]\');
}
add_shortcode(\'multigallery\', \'multi_gallery_shortcode\');
下面是它的外观

enter image description here

它工作得很好,但我想添加一些函数

enter image description here

我想用css ass将照片计数添加到短代码中,并在缩略图上方添加标题,但问题是,我不知道在哪里添加代码或<div> 代码,有人能帮我解决这个问题吗。

这是一个我必须调用照片数量的代码,但是我应该在哪里添加它,以便它显示在[multigallery]快捷代码中。

<?php
$attachments = get_children(array(\'post_parent\'=>$post->ID));
$nbImg = count($attachments);
echo \'<span class="galNum">\'.$nbImg.\'</span> Photos.\';
?>

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

你可以试试

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

    //format 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;
}
add_shortcode(\'multigallery\', \'multi_gallery_shortcode\');
其中,三个辅助函数是:

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 ));
}
最后

function get_first_gallery_image($post_id){ 
    global $wpdb; 
    $id = 0; 
    $args = array( 
        \'post_type\' => \'attachment\', 
        \'post_mime_type\' => \'image\', 
        \'posts_per_page\' => 1, 
        \'post_status\' => \'any\', 
        \'orderby\' => \'date\', 
        \'order\' => \'DESC\', 
        \'post_parent\' => $post_id, 
    ); 
    $attachments = get_posts($args); 
    if (isset($attachments[0])) { 
        $id = $attachments[0]->ID; 
    } 
    return $id; 
}

结束

相关推荐

Wp-config.php文件权限

我已经在本地Ubuntu机器上安装了Wordpress,带有标准的LAMP配置和phpMyAdmin。安装目录为localhost:///var/www/blog.在安装的第二步,在我进入MySQL配置后,出现以下消息:Sorry, but I can’t write the wp-config.php file. 当前权限为:-rw-r--r-- 1 adam adam 2.0K Oct 23 2010 wp-links-opml.php -rw-r--r-- 1 adam adam