PHP Wordpress 2.7+图库样式修复

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Wordpress 2.7+图库样式修复相关的知识,希望对你有一定的参考价值。

// Registers our function to filter default gallery shortcode
remove_shortcode('gallery');
add_shortcode('gallery', 'sandbox_gallery');

// Function to filter the default gallery shortcode
function sandbox_gallery($attr) {
	global $post;

	static $instance = 0;
	$instance++;

	// 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->ID,
		'itemtag'    => 'dl',
		'icontag'    => 'dt',
		'captiontag' => 'dd',
		'columns'    => 3,
		'size'       => 'thumbnail',
		'include'    => '',
		'exclude'    => ''
		), $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) );
	}

	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;
	}

	// check to see if tags have been set to false. If they are the defaults or have been set to a string value use that as the tag.
	if ($itemtag) $itemtag = tag_escape($itemtag);
	if ($captiontag) $captiontag = tag_escape($captiontag);
	if ($icontag) $icontag = tag_escape($icontag);
	$columns = intval($columns);

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

	$output = "<div id='$selector' class='gallery galleryid-{$id}'>\n";

	$i = 0;
	foreach ( $attachments as $id => $attachment ) {
	  ++$i;
		$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

		if ($itemtag) {
			$output .= '<'.$itemtag.' class="gallery-item ';
		  if( $columns > 0 && $i % $columns == 0 ) $output .= " last";
		  $output .= '">';
		}
		if ($icontag) $output .= "\n\t<" .$icontag. ">\t";
		$output .=  "\n\t".$link;
		if ($icontag) $output .= "\n\t</".$icontag.">";
		// if the attachment has a caption set
		if ( trim($attachment->post_excerpt) ) {
		  if ($captiontag) $output .= "\n<" .$captiontag. ">\n\t";
		  $output .= wptexturize($attachment->post_excerpt);
		  if ($captiontag) $output .= "\n</" .$captiontag. ">" . "<!-- end caption -->\n";
		}
		if ($itemtag) $output .= "\n</".$itemtag ."><!-- end itemtag -->\n";
		if ( $columns > 0 && $i % $columns == 0 ) $output .= "\n";
	}

	$output .= "</div><!-- end gallery -->\n";

	return $output;
}

以上是关于PHP Wordpress 2.7+图库样式修复的主要内容,如果未能解决你的问题,请参考以下文章

PHP 删除WordPress标准图库样式

php 创建自定义wordpress图库输出

php 创建自定义wordpress图库输出

php 创建自定义wordpress图库输出

PHP 带有自定义帖子类型和自定义分类的WordPress图库页面

php 修复Envira主题在Envira图库图像上的悬停操作