markdown 在wordpress中创建和使用自定义全局变量。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 在wordpress中创建和使用自定义全局变量。相关的知识,希望对你有一定的参考价值。

First create global variables (in functions.php or as a mu-plugin):

``` php
<?php

/*
 * CUSTOM GLOBAL VARIABLES
 */
function wtnerd_global_vars() {

	global $wtnerd;
	$wtnerd = array(

		'edition'  => get_query_var('category_name'),
		'channel'  => get_query_var('channel'),
		'tag'      => get_query_var('tag'),

	);

}
add_action( 'parse_query', 'wtnerd_global_vars' );
```

Why are we using an associative array variable `$wtnerd`? Because global variables need to be unique, and by keeping `$wtnerd` unique we can have simpler names for all the variables in its array.

By the way, the same can also be done like this:

``` php
<?php

/*
 * CUSTOM GLOBAL VARIABLES
 */
function wtnerd_global_vars() {

	global $wtnerd;
	$wtnerd['edition'] = get_query_var('category_name');
	$wtnerd['channel'] = get_query_var('channel');
	$wtnerd['tag']     = get_query_var('tag');

}
add_action( 'parse_query', 'wtnerd_global_vars' );
```

Then use `$GLOBALS[];` to call the variable elsewhere (another file):

``` php
<?php

if( $GLOBALS['wtnerd']['edition'] == uk ) {

	// Do something

}
```

***

If the function in which you are defining the global variables is not hooked into a filter or action, e.g. `add_action( 'parse_query', 'wtnerd_global_vars' );` as we are doing above, then you should do it as shown below.

In functions.php or mu-plugin:

``` php
<?php

/*
 * CUSTOM GLOBAL VARIABLES
 */
function wtnerd_global_vars() {

	global $wtnerd;
	$wtnerd = array(

		'edition'  => get_query_var('category_name'),
		'channel'  => get_query_var('channel'),
		'tag'      => get_query_var('tag'),

	);

}
```

Then, to call the variable elsewhere (another file), you need to manually initialize the function before you can use the variable:

``` php
<?php

wtnerd_global_vars();
if( $GLOBALS['wtnerd']['edition'] == uk ) {

	// Do something

}
```

以上是关于markdown 在wordpress中创建和使用自定义全局变量。的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 的核心数据中创建和使用带有查询的 NSPredicate?

在 CPP 中创建和使用 DLL

IAR模板--怎样在IARproject中创建和使用模板

在 C# 中创建和使用 Excel 文件 - Unity

在测试中创建和导入辅助函数,而不使用 py.test 在测试目录中创建包

在 Android 测试中创建和使用 gradle 或系统属性