此解决方案将添加data-fancybox="group"
到默认生成的库链接[gallery]
短代码。无论主题是否HTML5 theme support 是否为库启用。
该解决方案通过使用post_gallery
筛选以访问库快捷码的输出。然后,使用PHP的DOMDocument()
和DOMXpath()
.
add_filter( \'post_gallery\', \'wpse_gallery_shortcode\', 10, 3 );
/**
* Filters the default gallery shortcode output.
*
* @see gallery_shortcode()
*
* @param string $output The gallery output. Default empty.
* @param array $attr Attributes of the gallery shortcode.
* @param int $instance Unique numeric ID of this gallery shortcode instance.
*/
function wpse_gallery_shortcode( $output, $attr, $instance ) {
// Temporarily remove our filter to prevent infinite loop.
remove_filter( \'post_gallery\', \'wpse_gallery_shortcode\', 10, 3 );
// Use WordPress\' native function for generating gallery output.
$gallery_html = gallery_shortcode( $attr );
// Create an instance of DOMDocument.
$dom = new \\DOMDocument();
// Supress errors due to malformed HTML.
// See http://stackoverflow.com/a/17559716/3059883
$libxml_previous_state = libxml_use_internal_errors( true );
// Populate $dom with $gallery_html, making sure to handle UTF-8, otherwise
// problems will occur with UTF-8 characters.
// Also, make sure that the doctype and HTML tags are not added to our HTML fragment. http://stackoverflow.com/a/22490902/3059883
$dom->loadHTML( mb_convert_encoding( $gallery_html, \'HTML-ENTITIES\', \'UTF-8\' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
// Restore previous state of libxml_use_internal_errors() now that we\'re done.
// Again, see http://stackoverflow.com/a/17559716/3059883
libxml_use_internal_errors( $libxml_previous_state );
// Create an instance of DOMXpath.
$xpath = new \\DOMXpath( $dom );
// Match elements with the class gallery-icon (these can be <div> or <dt> depending on whether the theme has support for HTML5.
// See http://stackoverflow.com/a/26126336/3059883
$gallery_icons = $xpath->query( "//*[contains(concat(\' \', normalize-space(@class), \' \'), \' gallery-icon \')]" );
// Iterate over the the gallery icons.
foreach ( $gallery_icons as $gallery_icon ) {
// Find the <a> tags and add our custom attribute and value.
$gallery_icon_child_node_link = $xpath->query( "//a", $gallery_icon );
foreach ( $gallery_icon_child_node_link as $node_link ) {
$gallery_icon_data_fancy_box = $dom->createAttribute( \'data-fancybox\' );
$gallery_icon_data_fancy_box->value = \'group\';
$node_link->appendChild( $gallery_icon_data_fancy_box );
}
}
// Save the updated HTML.
$gallery_html = $dom->saveHTML();
// Add our filter back so that it will run the next time that the gallery shortcode is used.
add_filter( \'post_gallery\', \'wpse_gallery_shortcode\', 10, 3 );
// Return the modified HTML.
return $gallery_html;
}
这段代码有点冗长,主要是因为它用
DOMDocument()
.
您还可以考虑对这些类似问题的答案进行调整: