我有以下自定义函数来替换gallery_shortcode
核心功能。它从一开始就起作用了。
The problem 我无法从其中的库快捷码中获取自定义键/值。我在核心函数中接收它们,但它们不会传递到我的自定义函数中。我真的无法调查到底出了什么问题,或者他们在哪里失踪了,因为我没有得到任何print_r
的输出$attr
在我的自定义函数中。有什么想法吗?
注意:我为从核心到自定义函数的所有更改添加了注释
function modified_post_gallery( $attr )
{
// I can\'t get any output from the following
echo \'<pre>\';
print_r( $attr );
echo \'</pre>\';
// But: i get the expected output if i place the same line inside the core function:
/root/wp-include/media.php line 759 and get the actual output including all custom key/values.
global $post, $wp_locale;
static $instance = 0;
$instance++;
$output = \'\';
// 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 result from user input & defaults
extract( shortcode_atts( array(
\'order\' => \'ASC\'
,\'orderby\' => \'menu_order ID\'
,\'id\' => $post->ID
,\'itemtag\' => \'span\' // CHANGED
,\'icontag\' => \'span\' // CHANGED
,\'captiontag\' => \'span\' // CHANGED
,\'columns\' => 3
,\'size\' => \'thumbnail\'
,\'include\' => \'\'
,\'exclude\' => \'\'
,\'class\' => \'\' // ADDED
,\'test\'
), $attr ) );
$id = intval( $id );
if ( \'RAND\' == $order )
$orderby = \'none\';
if ( !empty( $include ) )
{
$include = preg_replace( \'/[^0-9,]+/\', \'\', $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 ) )
{
$exclude = preg_replace( \'/[^0-9,]+/\', \'\', $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
) );
}
// No attachments, so abort
if ( empty( $attachments ) )
return \'\';
// RSS/Atom
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 );
$columns = intval( $columns );
$itemwidth = $columns > 0 ? floor( 100/$columns ) : 100;
$float = is_rtl() ? \'right\' : \'left\';
$selector = \'gallery-\'.$instance;
$gallery_div = \'\';
$size_class = sanitize_html_class( $size );
$gallery_div = \'<div id="\'.$selector.\'" class="gallery galleryid-\'.$id.\' gallery-columns-\'.$columns.\' gallery-size-\'.$size_class.\'">\';
$i = 0;
foreach ( $attachments as $id => $attachment )
{
$class = $columns > 0 && ++$i % $columns == 0 ? \' last\' : \'\'; // ADDED for the single item/image
$link = isset( $attr[\'link\'] ) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false );
$output .= \'<\'.$itemtag.\' class="gallery-item \'.$class.\'">\'; // ADDED class
$output .= \'<\'.$icontag.\' class="gallery-icon">\'.$link.\'</\'.$icontag.\'>\';
if ( $captiontag && trim($attachment->post_excerpt) )
{
$output .= \'<\'.$captiontag.\' class="wp-caption-text gallery-caption">\';
$output .= wptexturize( $attachment->post_excerpt );
$output .= \'</\'.$captiontag.\'>\';
}
$output .= \'</\'.$itemtag.\'>\';
}
$output .= \'\'."\\n";
return $output;
}
add_filter( \'post_gallery\', \'modified_post_gallery\', 10, 2 );