Display latest 10 galleries

时间:2013-01-01 作者:Mircea

我想创建一个自定义页面,该页面将显示20个最新的库/页,但仅显示库中的第一个图像,而不是所有图像。类似这样:http://www.autoblog.it/gallerie/

我做到了:

<?php
$args = array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => 10,
\'post_parent\' => $postid,
\'numberposts\' => 1,
\'paged\' => $paged,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
    echo the_attachment_link($attachment->ID, false, false, true )
    echo get_the_title();   }
}?>
它可以工作,但它所做的是显示最新的10幅图像,而不是最新的10个图库,所以每个图库中的第一幅图像。

请帮忙

非常感谢。

2 个回复
SO网友:fuxia

使用图库获取最新帖子很简单:使用字符串搜索帖子\'[gallery\'.

$posts = get_posts(
    array (
        \'s\'           => \'[gallery\'
    )
);
从每个库中获取第一个图像比较困难,因为您必须渲染gallery 短代码。我不会使用默认的处理程序,它比我们这里需要的要多得多。这会浪费太多时间。

一步一步…

主要功能让我们从一个函数开始,该函数使用两个参数检索最新画廊帖子列表:帖子数量和格式帮助器:

function t5_list_galleries( $num = 10, $formatter = \'t5_gallery_list_formatter\' )
{
    $posts = get_posts(
        array (
            \'s\'           => \'[gallery\',
            \'numberposts\' => (int) $num
        )
    );

    if ( ! $posts )
        return FALSE;

    $list = new T5_Gallery_Image_Extractor( $posts );

    $html = call_user_func( $formatter, $list->get_results() );
    return $html;
}
正如你所见,我们需要一门课T5_Gallery_Image_Extractor 构建列表和格式函数以创建标记。我没有将格式化程序放入gallery提取器以保持灵活性:您可以在小部件中或在具有不同格式化程序的短代码中使用该功能。

类T5\\u Gallery\\u Image\\u提取器类构造函数需要一个帖子列表。它不关心我们是如何得到这个列表的,所以我们可以用一个完全不同的主函数手动构建它。

在内部,它将替换gallery 使用简化版,只需从帖子中的第一个图库中获取第一幅图像。

公共方法get_results() 返回如下数组:

Array
(
    [0] => Array
        (
            [url] => http://single.wp/blog/gallery-2/
            [img] => <img width="150" height="150" src="http://content.wp/singlewp/uploads/2013/01/git-bash-150x150.png" class="attachment-thumbnail" alt="git-bash" />
            [title] => Gallery 2
        )

    [1] => Array
        (
            [url] => http://single.wp/blog/gallery-1/
            [img] => <img width="150" height="150" src="http://content.wp/singlewp/uploads/2013/01/hat-150x150.png" class="attachment-thumbnail" alt="hat" />
            [title] => Gallery 1
        )
)
因此,我们通过一个图库获取每篇文章的URL、图像标记和文章标题。

代码,抱歉,没有时间发表评论,maybelater.:)

class T5_Gallery_Image_Extractor
{
    protected $results             = array ();

    protected $current_post        = NULL;

    protected $old_gallery_handler = FALSE;

    public function __construct( $posts )
    {
        $this->replace_gallery_shortcode();

        foreach ( $posts as $post )
            $this->collect_first_image( $post );
    }

    public function get_results()
    {
        remove_shortcode( \'gallery\' );

        if ( $this->old_gallery_handler )
            add_shortcode( \'gallery\', $this->old_gallery_handler );

        return $this->results;
    }

    protected function collect_first_image( $post )
    {
        $this->current_post = $post;
        do_shortcode( $post->post_content );
        $this->current_post = NULL;
    }

    protected function replace_gallery_shortcode()
    {
        // replace the old gallery shortcode
        if ( isset( $GLOBALS[\'shortcode_tags\'][\'gallery\'] ) )
            $this->old_gallery_handler = $GLOBALS[\'shortcode_tags\'][\'gallery\'];

        remove_shortcode( \'gallery\' );
        add_shortcode( \'gallery\', array ( $this, \'gallery_shortcode\' ) );
    }

    public function gallery_shortcode( $attr )
    {
        static $processed_posts = array ();

        $post = $this->current_post;

        if ( in_array( $post->ID, $processed_posts) )
            return; // no need to run through all galleries of a post.

        $processed_posts[] = $post->ID;

        if ( ! empty ( $attr[\'ids\'] ) )
        {
            if ( empty ( $attr[\'orderby\'] ) )
                $attr[\'orderby\'] = \'post__in\';

            $attr[\'include\'] = $attr[\'ids\'];
        }

        if ( isset ( $attr[\'orderby\'] ) )
        {
            $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
            if ( ! $attr[\'orderby\'] )
                unset( $attr[\'orderby\'] );
        }


        extract(shortcode_atts(array(
            \'order\'      => \'ASC\',
            \'orderby\'    => \'menu_order ID\',
            \'id\'         => $post->ID,
            \'include\'    => \'\',
            \'exclude\'    => \'\'
        ), $attr));

        $id = intval($id);

        if ( \'RAND\' == $order )
            $orderby = \'none\';

        $att_args = array (
            \'post_status\'    => \'inherit\',
            \'post_type\'      => \'attachment\',
            \'post_mime_type\' => \'image\',
            \'order\'          => $order,
            \'orderby\'        => $orderby,
            \'numberposts\'    => 1
        );

        if ( ! empty ( $include ) )
        {
            $att_args[\'include\'] = $include;
            $attachments         = get_posts( $att_args );
        }
        else
        {
            $att_args[\'post_parent\'] = $post->ID;

            if ( ! empty ( $exclude ) )
                $att_args[\'exclude\'] = $exclude;

            $attachments = get_children( $att_args );
        }
        $this->results[] = array (
            \'url\'   => get_permalink( $post->ID ),
            \'img\'   => wp_get_attachment_image( current( $attachments )->ID ),
            \'title\' => $post_title = esc_attr( $post->post_title )
        );
    }
}
格式化程序非常简单:只需将上面的数组转换为HTML列表即可。

function t5_gallery_list_formatter( $list )
{
    $output = \'<ul class="t5-gallery-list">\';

    foreach ( $list as $item )
        $output .= sprintf(
            \'<li><a href="%1$s" title="%2$s">%3$s</a></li>\',
            $item[\'url\'],
            $item[\'title\'],
            $item[\'img\']
        );

    return "$output</ul>";
}
现在,我们可以在任何需要的地方使用主功能:

print t5_list_galleries();
结果是一个很好的列表,其中包含了最新的十篇文章和一个图库,第一个图库中的第一幅图像被用作链接文本。

enter image description here

视觉格式由样式表决定:

.t5-gallery-list {
    /* here be dragon rules */
}

SO网友:Mircea

嗨,谢谢你的密码。。。

我还编写了一个简短的代码,,

这是:

<?php
wp_reset_query();
global $wpdb;
$posts = $wpdb->get_results
("
 SELECT *
 FROM $wpdb->posts 
 WHERE
   post_status = \'publish\'
   AND
    ID IN (
SELECT DISTINCT post_parent
FROM $wpdb->posts
WHERE
  post_parent > 0
AND
  post_type = \'attachment\'
AND
  post_mime_type IN (\'image/jpeg\', \'image/png\')
  )
   ORDER BY post_date DESC LIMIT 0, 10
 ");

  foreach($posts as $post) :
  setup_postdata($post);
 ?>
<div>

 </span>
 <div class="slider-image">
   <?php
   $images = get_children(array(
  \'post_parent\' => get_the_id(),
  \'post_type\' => \'attachment\',
  \'post_mime_type\' => \'image\',
  \'orderby\' => \'ID\',
  \'order\' => \'ASC\'
));
$ids = array_keys($images);
?>

<div style="height:180px; width:150px; border-bottom:solid 1px #f17d0b; float:left; margin-right:5px; margin-left:5px; margin-top:20px; overflow:hidden;" ><?php 
echo the_attachment_link($ids[0],false, false, true);

  ?><div style=" font-size:11px; color:#666; font-family:Arial, Helvetica, sans-serif;">             <?php echo get_the_title();    ?> </div></div>
 </div>
 </div>
 <?php
  endforeach;
 wp_reset_query();
 ?>
这实际上非常有效。现在唯一的问题是我如何分页???

非常感谢你。

结束

相关推荐

如何在所有其他筛选器/插件等之后筛选‘post_Gallery’

我正在尝试重新格式化库输出,更改默认列和链接设置,以及从库图像中排除特色图像。我可以使用*add\\u filter(\'post\\u gallery\'…)*在函数中。php,但这似乎覆盖了内核和插件以前对此过滤器的任何调用。m0r7if3r请求的覆盖示例:function test1($content, $attr) { return $content.\'Test One \';} add_filter(\'post_gallery\', \'test1\', 10, 2); fu