在图片发布有限的画廊中,我如何不将图片限制在单个发布中?

时间:2016-06-03 作者:Jason G.

我为一个主题创建了一个函数,该函数限制了画廊中显示的图像数量。这在Gallery post格式中。但是,我想限制所有页面上的图库图像,但单个帖子除外。

到目前为止,我的代码如下(用特定于所讨论的属性的部分进行了缩短):

function the_mx_limited_gallery( $attr ) {
    $post = get_post();
    $attachment_ids = the_mx_get_limited_gallery_ids();
    $link_image_to = the_mx_medialink_switcher(); // Customizer controls
    if( get_post_format() == \'gallery\' ) { // opens gallery post format check
    ...
    // setup shortcode attributes
    $atts = shortcode_atts( array(
        \'order\' => \'ASC\',
        \'orderby\' => \'menu_order ID\',
        \'id\' => $post ? $post->ID : 0,
        \'itemtag\' => \'figure\',
        \'icontag\' => \'div\',
        \'captiontag\' => \'figcaption\',
        \'columns\' => 3,
        \'include\' => $attachment_ids,
        \'size\' => \'gallery-thumb\',
        \'link\' => $link_image_to,

    ), $atts );

    $id = intval( $atts[\'id\'] );

    if ( ! empty( $atts[\'include\'] ) ) {
        $_attachments = get_posts( array( 
            \'include\' => $atts[\'include\'], 
            \'post_status\' => \'inherit\', 
            \'post_type\' => \'attachment\', 
            \'post_mime_type\' => \'image\', 
            \'order\' => $atts[\'order\'], 
            \'orderby\' => $atts[\'orderby\'],
        ) );

        $attachments = array();
        foreach ( $_attachments as $key => $val ) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    } elseif ( ! empty( $atts[\'exclude\'] ) ) {
        $attachments = get_children( array( 
            \'post_parent\' => $id, 
            \'exclude\' => $atts[\'exclude\'], 
            \'post_status\' => \'inherit\', 
            \'post_type\' => \'attachment\', 
            \'post_mime_type\' => \'image\', 
            \'order\' => $atts[\'order\'], 
            \'orderby\' => $atts[\'orderby\'] 
        ) );
    } else {
        $attachments = get_children( array( 
            \'post_parent\' => $id, 
            \'post_status\' => \'inherit\', 
            \'post_type\' => \'attachment\', 
            \'post_mime_type\' => \'image\', 
            \'order\' => $atts[\'order\'], 
            \'orderby\' => $atts[\'orderby\'] 
        ) );
    }

    ...

}
检索gallery ID(The\\u mx\\u get\\u limited\\u gallery\\u ID)的代码如下:

// Function for limiting Gallery ID length (in the Gallery post format).
// see http://www.webgurus.biz/how-to-limit-wordpress-gallery-thumbnails-in-the-loop/
function the_mx_get_limited_gallery_ids() {
    global $wpdb, $post;
        $ids = \'\';
        $counter = 0;
        $number_of_posts = 6;
        $args = array(
            \'post_type\' => \'attachment\',
            \'numberposts\' => 6,
            \'orderby\' => \'menu_order\',
            \'order\' => \'ASC\',
            \'post_status\' => null,
            \'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;
}
为了在前端显示,我尝试只使用筛选shortcode_atts_gallery 并显示为do_shortcode, 但在我的测试环境中,这会干扰Jetpack的库添加。我想让我的画廊和Jetpack画廊一起展出。

任何帮助都将不胜感激。

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

我没有找到答案,但找到了解决这个问题的方法。在里面the_mx_limited_gallery 函数,我添加了一个否定的is_single 检查该函数的大部分内容,因此基本上只有当您位于某个页面而不是单个帖子时,才会显示有限的帖子和自定义代码。

这不是我最初的打算,但它暂时起作用。以下是调整后的代码:

function the_mx_limited_gallery( $attr ) {
    $post = get_post();
    $attachment_ids = the_mx_get_limited_gallery_ids();
    $link_image_to = the_mx_medialink_switcher(); // Customizer controls
    $mx_colcount = the_mx_gal_colcount_switcher(); // Customizer controls
    if( get_post_format() == \'gallery\' ) { // opens gallery post format check

        if( !is_single() ) { // opens non single page if statement
        // setup shortcode attributes
        $atts = shortcode_atts( array(
        ...

        } // closes non single page if statement

    } // closes gallery post format
}
add_filter( \'post_gallery\', \'the_mx_limited_gallery\', 10, 1 );
我忘记了原始问题中的add\\u filter部分。

如果有人仍然可以找到一个只调整短代码属性的解决方案,我仍然愿意寻求建议或答案。现在,我将把这个标记为已回答。

相关推荐

Swap themes locally

我有一个问题,我的“添加新插件”按钮不起作用,它也不会出现在普通菜单上。我在线搜索,建议的解决方案是交换主题,以检查主题是否冲突或禁用了一些wp核心。我有这个网站已经在生产中,我想改变它本地到另一个主题只是为了测试。我该怎么做?我尽量避免发布登台环境,因为它非常耗时,我只需要调试这一个问题。提前谢谢。