PHP Wordpress:在一个会话变量中返回所有自定义字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Wordpress:在一个会话变量中返回所有自定义字段相关的知识,希望对你有一定的参考价值。

/**
* Usage: Add all_my_customs(); to your page somewhere (header.php is best)
* Usage: if you would like to get the postmeta from a specific page use, all_my_customs('7');
* To access the value <?php echo $_SESSION['pc']['your_custom_name']; ?>
*/

function all_my_customs($id = 0){

	//if a post id is not provided grab the page currently being presented 
	if ($id == 0) 
	{
		global $wp_query;
		$content_array = $wp_query->get_queried_object(); 
		$id = $content_array->ID;
	}

	//grab the customs in their raw form from wp
	$raw = get_post_custom($id);
	//if the raw data is an array run a loop
	if (is_array($raw)) 
	{
		foreach($raw as $key => $value)
		{
			//if the number of items returned for the cf is greater than one (an array of items)
			if (count($value) > 1)
			{
				//retain all
				foreach($value as $v)
				{
					$return[$key][] = $v;	
				}
			}
			//else add the first item
			else
			{
				$return[$key] = $value[0];
			}
		}
		
	}
	//and returns the session array.
	$_SESSION['pc'] = $return;
	return $_SESSION['pc'];
}

以上是关于PHP Wordpress:在一个会话变量中返回所有自定义字段的主要内容,如果未能解决你的问题,请参考以下文章

php会话变量的安全性如何[重复]

使用会话变量在 PHP 中创建一个简单的购物篮

PHP会话不适用于多个WordPress页面

在 wordpress 中提交表单后会话丢失

php 为Wordpress启动会话

如何将数组存储到php中的会话变量中