替代图库默认设置链接

时间:2013-09-23 作者:Brian Wayne

有没有办法控制库默认的“链接到”设置?使用image_default_link_type a中的过滤器functions.php 该文件可以在帖子中插入单个图像,但似乎对图库没有任何影响。。。

6 个回复
最合适的回答,由SO网友:Eugene Manuilov 整理而成

不幸的是,没有法律手段来控制它。但有一种肮脏的方式。。。如果选择此路线,则需要:

克隆标准gallery_shortcode 函数为添加默认值$attr[\'link\'] 选项将克隆的函数挂钩到post_gallery 筛选最终结果如下所示:

add_filter( \'post_gallery\', \'wpse8170_gallery_shortcode\', 10, 2 );
function wpse8170_gallery_shortcode( $output, $attr ) {
    $post = get_post();

    static $instance = 0;
    $instance++;

    // override default link settings
    if ( empty(  $attr[\'link\'] ) ) {
        $attr[\'link\'] = \'none\'; // set your default value here
    }

    if ( !empty( $attr[\'ids\'] ) ) {
        // \'ids\' is explicitly ordered, unless you specify otherwise.
        if ( empty( $attr[\'orderby\'] ) )
            $attr[\'orderby\'] = \'post__in\';
        $attr[\'include\'] = $attr[\'ids\'];
    }

    // We\'re trusting author input, so let\'s at least make sure it looks like a valid orderby statement
    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 ? $post->ID : 0,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'include\'    => \'\',
        \'exclude\'    => \'\'
    ), $attr, \'gallery\'));

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

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

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

    if ( empty($attachments) )
        return \'\';

    if ( is_feed() ) {
        $output = "\\n";
        foreach ( $attachments as $att_id => $attachment )
            $output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
        return $output;
    }

    $itemtag = tag_escape($itemtag);
    $captiontag = tag_escape($captiontag);
    $icontag = tag_escape($icontag);
    $valid_tags = wp_kses_allowed_html( \'post\' );
    if ( ! isset( $valid_tags[ $itemtag ] ) )
        $itemtag = \'dl\';
    if ( ! isset( $valid_tags[ $captiontag ] ) )
        $captiontag = \'dd\';
    if ( ! isset( $valid_tags[ $icontag ] ) )
        $icontag = \'dt\';

    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    $float = is_rtl() ? \'right\' : \'left\';

    $selector = "gallery-{$instance}";

    $gallery_style = $gallery_div = \'\';
    if ( apply_filters( \'use_default_gallery_style\', true ) )
        $gallery_style = "
        <style type=\'text/css\'>
            #{$selector} {
                margin: auto;
            }
            #{$selector} .gallery-item {
                float: {$float};
                margin-top: 10px;
                text-align: center;
                width: {$itemwidth}%;
            }
            #{$selector} img {
                border: 2px solid #cfcfcf;
            }
            #{$selector} .gallery-caption {
                margin-left: 0;
            }
            /* see gallery_shortcode() in wp-includes/media.php */
        </style>";
    $size_class = sanitize_html_class( $size );
    $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
    $output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );

    $i = 0;
    foreach ( $attachments as $id => $attachment ) {
        if ( ! empty( $attr[\'link\'] ) && \'file\' === $attr[\'link\'] )
            $image_output = wp_get_attachment_link( $id, $size, false, false );
        elseif ( ! empty( $attr[\'link\'] ) && \'none\' === $attr[\'link\'] )
            $image_output = wp_get_attachment_image( $id, $size, false );
        else
            $image_output = wp_get_attachment_link( $id, $size, true, false );

        $image_meta  = wp_get_attachment_metadata( $id );

        $orientation = \'\';
        if ( isset( $image_meta[\'height\'], $image_meta[\'width\'] ) )
            $orientation = ( $image_meta[\'height\'] > $image_meta[\'width\'] ) ? \'portrait\' : \'landscape\';

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "
            <{$icontag} class=\'gallery-icon {$orientation}\'>
                $image_output
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class=\'wp-caption-text gallery-caption\'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'<br style="clear: both" />\';
    }

    $output .= "
            <br style=\'clear: both;\' />
        </div>\\n";

    return $output;
}

SO网友:giraff

现在有一个更简单的解决方案:

function my_gallery_default_type_set_link( $settings ) {
    $settings[\'galleryDefaults\'][\'link\'] = \'file\';
    return $settings;
}
add_filter( \'media_view_settings\', \'my_gallery_default_type_set_link\');
他们在WP 4.0中添加了一个过滤器来自定义此默认值(以及与新媒体上载弹出窗口相关的其他值)(trac).

SO网友:gabssnake

老问题,但仍然获得搜索流量,所以对于其他人来说:

您可以通过过滤库短代码属性来完成所描述的内容。如果要更改默认图像大小、列等,也很有用。

function gallery_should_link_to_files($out, $pairs, $atts)
{
    $atts = shortcode_atts( array( 
    \'link\' => \'file\' 
    ), $atts );
    $out[\'link\'] = $atts[\'link\'];
    return $out;
}
add_filter(\'shortcode_atts_gallery\', \'gallery_should_link_to_files\', 10, 3);
编辑2020年1月:正如@felwithe在评论中指出的,此代码将在wp-includes/shortcodes.php, 例如它在当前WordPress中仍然有效!

该过滤器在法典中的记录似乎很差:http://codex.wordpress.org/Function_Reference/shortcode_atts_gallery

尽管如此,2013年初添加了修改“链接”属性的选项:https://core.trac.wordpress.org/changeset/25665/trunk

SO网友:MarkJ

Tomas的回答实际上包含了更改默认链接的关键(对于Gutenberg)。不影响已创建的库。

我实现它的最佳方式是创建所需块(库)的新编辑变体,并将其设置为默认值。这样,旧的图库就被新的编辑图库所取代。

通过在主题的js文件夹中创建js文件来注册新变体:

// Gallery block
wp.blocks.registerBlockVariation(
   \'core/gallery\', {
      isDefault: true,
      attributes: {
        linkTo: \'media\',
      }
   }
);
// Image block
wp.blocks.registerBlockVariation(
   \'core/image\', {
      isDefault: true,
      attributes: {
        linkDestination: \'media\',
      }
   }
);
将其放入功能中。php

function new_gallery() {
    wp_enqueue_script(\'new-gallery\', get_template_directory_uri() . \'/js/gallery-var.js\',
        array( \'wp-blocks\', \'wp-dom-ready\', \'wp-edit-post\' )
    );
}
add_action( \'enqueue_block_editor_assets\', \'new_gallery\' );
可能也有一种php方法可以做到这一点,但出于某种原因,文档中只提到了register\\u block\\u样式https://developer.wordpress.org/block-editor/developers/filters/block-filters/

我开始越来越喜欢古腾堡:)

SO网友:Tomas

这是我在WordPress 5.0(古腾堡)上的工作示例!

function gallery_template_to_posts() {
    $post_type_object = get_post_type_object( \'post\' );
    $post_type_object->template = array(
        array( \'core/gallery\', array(
            \'linkTo\' => \'media\',
        ) ),
    );
}
add_action( \'init\', \'gallery_template_to_posts\' );

SO网友:user47952

这是事实。如上所述,在侧栏中,它将显示为库链接到“附件页”。但是,一旦发布帖子并单击图像,您会注意到它实际上链接到媒体文件。

我将这段代码更进一步,创建了一个插件。这是我希望保持特定于站点而不是特定于主题的功能。

为了创建插件,我按照this page. 我创建了一个名为gallery-link-to-file.phpwp-content/plugins 目录然后我将上面的代码粘贴到其中并进行了更改$attr[\'link\'] = \'none\';$attr[\'link\'] = \'file\';

然后我激活了插件,它就像一个魔咒。

结束

相关推荐

Adding Relevant Post Images

我想在每个帖子页面的末尾添加相关帖子with an image 在我新创建的网站中http://www.techberita.com但作为一个初学者,这对我来说是一个很大的问题,因为我不能做到这一点,因为我对PHP和wordpress没有太多的知识。我搜索了一些插件,但没有找到一个好的插件。我希望很快得到答复。最好使用php代码。