对于全局变量来说,这可能是一个很好的例子。只需确保在添加到数组之前以及在何时尝试输出内容之前将其设置为数组。在添加到内容之前,将数组转换为字符串。
此外,在添加到数组之前,请确保元数据存在,这样就不会添加空的版权值。
GLOBAL VERSION
function wpse_get_full_size_gallery_images($gallery, $post, $galleries) {
// reference the global variable
global $copyright;
// make sure it\'s set
if( ! isset($copyright)) $copyright = array();
// grab the IDS
$ids = explode(\',\', $galleries[ \'include\' ]);
foreach($ids as $id) {
// get the possible meta
$meta = get_post_meta($id, \'wiki_copyright\', true);
// add them to the copyright array
// make sure to only add if it exists
if( !empty($meta)) $copyright[] = $meta;
}
return $gallery;
}
add_filter(\'shortcode_atts_gallery\', \'wpse_get_full_size_gallery_images\', 10, 3);
function my_the_content_filter($content) {
// reference the global variable
global $copyright;
// add copyright array converted to string if we have content
if( ! empty($copyright)) $content .= implode(\',\', $copyright);
return $content;
}
add_filter(\'the_content\', \'my_the_content_filter\', 20);
STATIC VERSION
如果您不喜欢使用globals,无论出于何种原因,请考虑
static 作为替补
variable scope 在PHP中。
变量作用域的另一个重要特性是静态变量。静态变量仅存在于局部函数作用域中,但当程序执行离开此作用域时,它不会丢失其值。
由于持久变量绑定到单个函数作用域,我们需要在任何循环之外创建函数,然后重用它来添加值以及检索这些值。
如果传递新值;add it. 如果未传递值;get all values.
if( ! function_exists(\'copyrights\')) {
function copyrights($value = \'\') {
// only this function has access to $_copyrights
static $_copyrights;
// expressions can\'t be assigned as defaults
// but we still want to initialize the value
if( ! isset($_copyrights)) {
$_copyrights = array();
}
// if we\'re passing new values then add them
if( ! empty($value)) {
$_copyrights[] = $value;
return true; // success
}
else {
// otherwise return uniques
$unique_list = array_unique($_copyrights);
// convert to string before they leave
return implode(", ", $unique_list);
}
}
}
add_filter(\'shortcode_atts_gallery\', function($gallery, $post, $galleries) {
$ids = explode(\',\', $galleries[ \'include\' ]);
foreach($ids as $id)
if( ! empty($meta = get_post_meta($id, \'wiki_copyright\', true)))
copyrights($meta);
return $gallery;
}, 10, 3);
add_filter(\'the_content\', function($content) {
return $content . copyrights();
}, 20);
测试
copyrights ( 123 );
copyrights ( 223 );
copyrights ( 333 );
copyrights ( 123 );
copyrights ( 111 );
copyrights ( 111 );
copyrights ( 111 );
echo ( copyrights() ); // 123, 223, 333, 111
SINGLETON VERSION
实际上我从来没有用过globals,因为
Singleton Pattern 是我的首选方法。
其思想是将所有方法放在类的实例上,但只能通过静态方法访问它。通过这样做,该类确保只创建一个实例。
if( ! class_exists(\'Copyrights\')) {
class Copyrights {
private $copyrights;
private static $instance;
// PRIVATE CONSTRUCTOR
private function __construct() {
// initialize the array
$this->copyrights = array();
}
// STATIC SINGLETON
public static function getInstance() {
if(is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
// PUBLIC METHODS
public function toString() {
$unique_list = array_unique($this->copyrights);
return implode(", ", $unique_list);
}
public function add($value = \'\') {
if( ! empty($value)) {
$this->copyrights[] = $value;
}
}
}
}
add_filter(\'shortcode_atts_gallery\', function($gallery, $post, $galleries) {
$ids = explode(\',\', $galleries[ \'include\' ]);
foreach($ids as $id)
if( ! empty($meta = get_post_meta($id, \'wiki_copyright\', true)))
Copyrights::getInstance()->add($meta);
return $gallery;
}, 10, 3);
add_filter(\'the_content\', function($content) {
return $content . Copyrights::getInstance()->toString();
}, 20);
测试
Copyrights::getInstance()->add ( 123 );
Copyrights::getInstance()->add ( 223 );
Copyrights::getInstance()->add ( 333 );
Copyrights::getInstance()->add ( 123 );
Copyrights::getInstance()->add ( 111 );
Copyrights::getInstance()->add ( 111 );
Copyrights::getInstance()->add ( 111 );
echo Copyrights::getInstance()->toString(); // 123, 223, 333, 111
SINGLETON PER POST VERSION
也许是在
global
vs。
the sky is falling
上述解决方案(以及提议的备选方案)是否只针对一个职位。如果你想在一个循环中添加你的版权,那你就完了,因为所有的帖子都会被添加到一个数组中。
对于singleton类,添加每个ID的singleton很简单,可以说是最好的选择。
if( ! class_exists(\'Copyrights\')) {
class Copyrights {
private static $instance;
private $copyrights;
public $post_id;
// PRIVATE CONSTRUCTOR
private function __construct($post_id) {
// save the id
$this->post_id = $post_id;
// initialize the array
$this->copyrights = array();
}
// SINGLETON
public static function getInstance($post_id = \'NO ID\') {
if(is_null(self::$instance)) self::$instance = array();
if(is_null(self::$instance[ $post_id ])) {
self::$instance[ $post_id ] = new self($post_id);
}
return self::$instance[ $post_id ];
}
// PUBLIC METHODS
public function toString() {
$unique_list = array_unique($this->copyrights);
return implode(", ", $unique_list);
}
public function add($value = \'\') {
if( ! empty($value)) {
$this->copyrights[] = $value;
}
}
}
}
add_filter(\'shortcode_atts_gallery\', function($gallery, $post, $galleries) {
$ids = explode(\',\', $galleries[ \'include\' ]);
foreach($ids as $id)
if( ! empty($meta = get_post_meta($id, \'wiki_copyright\', true)))
Copyrights::getInstance($post->ID)->add($meta);
return $gallery;
}, 10, 3);
add_filter(\'the_content\', function($content) {
global $post;
return $content . Copyrights::getInstance($post->ID)->toString();
}, 20);
测试
如您所见,使用ID
钥匙使事情保持灵活。
Copyrights::getInstance( 1 )->add ( 123 );
Copyrights::getInstance( 1 )->add ( 223 );
Copyrights::getInstance( 2 )->add ( 333 );
Copyrights::getInstance( 2 )->add ( 123 );
Copyrights::getInstance( 2 )->add ( 111 );
Copyrights::getInstance( 2 )->add ( 111 );
Copyrights::getInstance( 2 )->add ( 111 );
Copyrights::getInstance()->add ( 111 );
Copyrights::getInstance()->add ( 444 );
Copyrights::getInstance()->add ( 555 );
Copyrights::getInstance()->add ( 888 );
echo Copyrights::getInstance( 1 )->toString(); // 123, 223
echo Copyrights::getInstance( 2 )->toString(); // 333, 123, 111
echo Copyrights::getInstance()->toString(); // 111, 444, 555, 888
当有人认为在
database 因为他们不懂PHP,所以现在是反思问题的好时机。
<小时>
UPDATE
我
solved 使用
global 变量,但重新思考问题您可以通过使用
the content 仅使用过滤器挂钩—使问题保持本地化。
在这种情况下,当the_content
运行,我们检查all galleries 并仅输出relevant data.
不需要全局、静态、单例、选项或额外函数。
最重要的是,这将在一个循环中工作,不会出现复杂情况。
add_filter(\'the_content\', \'add_copyright_to_content\');
function add_copyright_to_content($content) {
// pull all galleries from the post
$galleries = get_post_galleries(get_post(), false);
// no gallery? then let\'s get out of here
if(empty($galleries)) return $content;
$all = \'\';
// gather all ids
foreach($galleries as $gallery) $all .= $gallery[ \'ids\' ] . \',\';
// convert them to an array with unique ids
$all = array_unique(array_filter(explode(\',\', $all)));
// replace id for copyright info on the image meta
foreach($all as $key => $id) $all [ $key ] = get_post_meta($id, \'wiki_copyright\', true);
// return non-empty values to the end of the content
return $content . implode(\', \', array_filter($all));
}