首页 > 解决方案 > Wordpress 库函数在 echo 执行时不返回任何内容

问题描述

当一篇文章中的图像数量接近 60 时, post_gallery挂钩的自定义my_post_gallery函数不会返回图像。无法更精确地指定它,因为它因情况而异。仅此一点就很奇怪。

echo在任何情况下都会返回图像。

辅助函数似乎工作正常。

我怀疑是 php.ini 或 apache 问题?

function my_post_gallery($output, $attr) {
	global $post;
	
	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];
		}
	}
	
	
	if (empty($attachments)) return '';
	
	$output = "<div class=\"wrapper-gallery\">\n";
	
	foreach ($attachments as $id => $attachment) {
		$img = wp_prepare_attachment_for_js($id);
		if ($columns == '3') { $url = $img['sizes']['medium']['url']; };
		if ($columns == '2') { $url = $img['sizes']['large']['url']; };
		if ($columns == '1') { $url = $img['sizes']['large']['url']; };
		$alt = $img['title'];
		$posturl = urlencode(get_permalink());
		ob_start();
		the_subtitle();
		$subtitle = ob_get_clean();
		$pinalt = get_the_title() . ' - ' . $subtitle;
		
		$altreplace = '$2';
		$altneu = $url;
		$altneu = preg_replace("/(.*)\/(.+)(-(.*)x(.*))\.jpg/",$altreplace, $altneu);
		
		if ($altneu == $alt){
			$alt = $pinalt;
		}
		
		$newimageclass = "";
		if ($columns == '3') {
			if ($i <= 2) {
				$newimageclass = "firstrowimage lazyload"; 
			} else {
				$newimageclass = "lazyload";
			}
			$tmp = '<img src="'. $url .'" alt="'. $alt .'" class="'. $newimageclass .'"/>';
			$srcset = wr2x_picture_rewrite($tmp);	
			$addsrc = add_attributes($srcset, $url);
			$script = add_data_srcset($addsrc);
			$items .= $script;
			$columnscount = "threecolumns";
		} else if ($columns == '2') {
			if ($i <= 1) {
				$newimageclass = "firstrowimage lazyload"; 
			} else {
				$newimageclass = "lazyload";
			}
			$tmp = '<img src="'. $url .'" alt="'. $alt .'" class="'. $newimageclass .'"/>';
			$srcset = wr2x_picture_rewrite($tmp);	
			$addsrc = add_attributes($srcset, $url);
			$script = add_data_srcset($addsrc);
			$items .= $script;
			$columnscount = "twocolumns";
		} else {
			// 1 column
			if ($i == 0) {
				$newimageclass = "firstrowimage lazyload"; 
			} else {
				$newimageclass = "lazyload";
			}
			$tmp = '<img src="'. $url .'" alt="'. $alt .'" class="'. $newimageclass .'"/>';
			$srcset = wr2x_picture_rewrite($tmp);	
			$addsrc = add_attributes($srcset, $url);
			$script = add_data_srcset($addsrc);
			$items .= $script;
			$columnscount = "onecolumn";
		}
		$i++;
	}
	$output .= "<div class=\"{$columnscount}\">{$items}</div></div>\n";
	
  echo $output; //WORKS IN ANY CASE
  return $output; //WORKS UP TO ~60 ITEMS
}
add_filter('post_gallery', 'my_post_gallery', 10, 2);

辅助函数是:

function add_data_srcset ($input) {
	$match = '/(srcset)/';
	$replace = 'data-$1';
	$output = preg_replace($match, $replace, $input);
	return $output;
}
function add_attributes($srcset, $url){
	$match = '/(<img)/';
	$replace = '$1 src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== ';
	$output = preg_replace($match, $replace, $srcset);
	return $output;
}

标签: phpwordpress

解决方案


你想做什么?您想在页面上显示您的画廊吗?

我认为你不需要为此使用钩子。

您可以使用 add_action 调用模板中的函数来显示图像。如果您在函数中使用返回,只需在模板中调用。

add_action('display_post_gallery', 'my_post_gallery', 10, 2);

或者,您可以使用简码在页面构建器或经典编辑器中显示您的画廊。只需在任何地方调用 [display_post_gallery]。

add_shortcode('display_post_gallery', 'my_post_gallery');

接下来的事情是,为什么要使用 a return?您正在 $output 中创建 html,因此如果您想显示图库,只有“echo”有效。如果您在模板中回显 $output,则返回将起作用。


推荐阅读