使用WP shorcode读取数组

时间:2017-09-16 23:50:47

标签: php arrays wordpress shortcode

我正在尝试编写WP短代码来读取价格表,该价目表被组织为二维数组。短缺有2个属性 - 列和行 - 并且应该返回价格表数组中列/行位置的数字

使用下面的草案代码,短代码会从数组中返回看似随机的值...

然而,当我插入实际整数而不是$ atts ['column']和$ atts ['line']时,所有工作和shortocde都会从数组中返回正确的值。有什么想法吗?

// Add Shortcode
function price_shortcode( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'column' => '',
			'line' => '',
		),
		$atts,
		'price'
	);

	// Return content of price list at specified location
	ini_set("auto_detect_line_endings", true);
	
	$PriceListArray = array();
	if (($handle = fopen("wp-content/themes/twentythirteen-child/pa/PriceList.csv", "r")) !== FALSE) {
	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	        $PriceListArray[] = $data;
	    }
	    fclose($handle);
	}
	
	ini_set("auto_detect_line_endings", false);
	
	
	return $PriceListArray[$atts['column']][$atts['line']];
	
	

}
add_shortcode( 'price', 'price_shortcode' );

1 个答案:

答案 0 :(得分:0)

OOP我发现了错误...... 在读取数组时: return $ PriceListArray [$ atts ['column']] [$ atts ['line']];

属性的顺序应该是行然后列,而不是相反的..

我会删除这个问题,但无法弄清楚这是否可行......