admin管理员组文章数量:1300028
I use a gallery shortcode in my index.php (theme directory) to display 3 boxes of images. The images are fetched from my Media.
<div class="well well-clear" style="padding-bottom: 0; margin-bottom: 0;">
<?php echo do_shortcode('[gallery ids="165,166,167"]'); ?>
</div>
By default these images have a hyperlink that when I click on it, a page will open and show the bigger version of this image. I don't want this to happen. Is there any option that I need to modify so that it will not hyperlink?
I'm new to WordPress, please don't be rough on me.
I use a gallery shortcode in my index.php (theme directory) to display 3 boxes of images. The images are fetched from my Media.
<div class="well well-clear" style="padding-bottom: 0; margin-bottom: 0;">
<?php echo do_shortcode('[gallery ids="165,166,167"]'); ?>
</div>
By default these images have a hyperlink that when I click on it, a page will open and show the bigger version of this image. I don't want this to happen. Is there any option that I need to modify so that it will not hyperlink?
I'm new to WordPress, please don't be rough on me.
Share Improve this question edited Jan 20, 2014 at 12:25 Brad Dalton 6,9652 gold badges36 silver badges47 bronze badges asked Jan 19, 2014 at 12:08 Leandro GarciaLeandro Garcia 1131 silver badge4 bronze badges 1- Shortcodes usually should be part of the content and not the code. The whole point is that the author can edit them.... If you know in advance what images you want to be displayed why not to hard code the exact HTML that you want to get? – Mark Kaplun Commented Jan 19, 2014 at 12:46
2 Answers
Reset to default 3Update:
It looks like there exists an attribute link="none"
after all ;-)
For example:
[gallery link="none" ids="165,166,167"]
So we don't need to reinvent the wheel, like in my previous answers ;-)
Previous: Plugin to handle link="no"
in the gallery shortcode:
Here's a demo plugin to add the option to remove the image links in the gallery.
You can use the link="no"
option to remove the links, for example:
[gallery link="no" ids="165,166,167"]
Create the folder /wp-content/plugins/gallery-without-links/
and add the file gallery-without-links.php
to it, containing the following code:
<?php
/**
* Plugin Name: Gallery without links
* Plugin URI: http://wordpress.stackexchange/a/130349/26350
* Description: Gallery with the link='no' option to remove links.
*/
/**
* Init the WPSE_No_Gallery_Links class
*/
if( ! class_exists( 'WPSE_No_Gallery_Links' ) ):
add_action( 'init', array( 'WPSE_No_Gallery_Links', 'get_instance' ) );
class WPSE_No_Gallery_Links
{
static private $instance = NULL;
protected $nrofimgs = 0;
protected $counter = 0;
public function __construct()
{
add_filter( 'shortcode_atts_gallery', array( $this, 'shortcode_atts_gallery' ) );
}
static public function get_instance()
{
if ( NULL === self :: $instance )
self :: $instance = new self;
return self :: $instance;
}
public function wp_get_attachment_link( $link ){
$this->counter++;
if( $this->counter >= $this->nrofimgs )
{
$this->counter = 0;
remove_action( 'wp_get_attachment_link', array( $this, 'wp_get_attachment_link' ) );
}
return strip_tags( $link, '<img>' );
}
public function shortcode_atts_gallery( $atts ) {
if( 'no' === $atts['link'] )
{
if( isset( $atts['include'] ) )
{
$this->nrofimgs = count( explode( ',', $atts['include'] ) );
add_action( 'wp_get_attachment_link', array( $this, 'wp_get_attachment_link' ) );
}
}
return $atts;
}
} // end of class
endif;
Previous answer:
Here is one idea:
The gallery shortcode callback is using the wp_get_attachment_link()
function to generate the link to each image in the gallery. We can therefore use the wp_get_attachment_link
filter to strip out the <a>
tags.
You could then modify your code snippet to:
<div class="well well-clear" style="padding-bottom: 0; margin-bottom: 0;">
<?php
add_action( 'wp_get_attachment_link', 'custom_wp_get_attachment_link' );
echo do_shortcode('[gallery ids="165,166,167"]');
remove_action( 'wp_get_attachment_link', 'custom_wp_get_attachment_link' );
?>
</div>
where:
/**
* Strip all tags except the <img> tag
*/
function custom_wp_get_attachment_link( $link )
{
return strip_tags( $link, '<img>' );
}
I just had a similar problem, and I used CSS targeted at the ul applying pointer-events: none;
.
I'm using the gallery block and my current Wordpress version allows to introduce custom CSS directly on each block, so I did just that. No need to do CSS hunting.
本文标签: Remove hyperlink on gallery shortcode
版权声明:本文标题:Remove hyperlink on gallery shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741605045a2387924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论