我正在尝试获取与列为排除的[库]快捷码关联的所有图像id。例如:如果我的帖子[gallery exclude="1,2,3"]
我想得到一个这样的变量echo $excludes;
后果1,2,3
感谢您提供的任何帮助。
获取属性/部分图库快捷代码
3 个回复
最合适的回答,由SO网友:sosukeinu 整理而成
我花了一段时间才找到一个适合我的解决方案,但由于我所寻找的只是与某个属性相关联的附件id的分隔列表,如exclude
或hide
, 这对我很有用:
# Grab the list of "hide" attribute
$regex_pattern = get_shortcode_regex();
preg_match (\'/\'.$regex_pattern.\'/s\', $post->post_content, $regex_matches);
if ($regex_matches[2] == \'gallery\') :
$attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
$attribureStr = str_replace (\'"\', \'\', $attribureStr);
// Parse the attributes
$defaults = array (
\'hide\' => \'1\',
);
$attributes = wp_parse_args ($attribureStr, $defaults);
if (isset ($attributes["hide"])) :
$excludeID = get_post_thumbnail_id() . \',\' . $attributes["hide"];
else :
$excludeID = get_post_thumbnail_id();
endif;
endif;
SO网友:kaiser
这项任务并不像乍一看那么容易。主要问题是,您可以定义自己的[gallery]
快捷编码并简单地覆盖默认值。事实上,这正是一些主题所做的(怪你们这些最早的作者!)。在这种情况下,只需post_gallery
不会在所有情况下都起作用。
的缺点post_gallery
的缺点the_content
如果要切换到另一个过滤器并获取原始输出,则必须(另一次)处理Regex。这很慢,而且大多数情况下都不简单。那么现在该怎么办呢?容易的与交互global $shortcode_tags
. 它将回调作为第二个参数,因此实际上不太难确定我们是哪种情况。然后我们可以简单地按需切换。这样,我们在可靠性和性能之间取得了良好的平衡。
构建一个基本插件abstract
类(必须extend
为了工作)。child类中需要定义三件事:
$part
- 要检索的零件$type
- 您需要的匹配类型。有效的是digit/alphanumeric/alpha
process_atts()
- 处理输出的方法-无论您想对结果做什么,只需通过FTP将其上载到插件文件夹并激活即可。
处理一项任务,获得一个子项,在这里您可以看到实际的处理插件。首先,它将自己静态地挂接到<?php /** Plugin Name: (#70451) »kaiser« Get Gallery attributes (Base) */ if ( ! class_exists( \'wpse70451_extract_gallery_atts\' ) ) { abstract class wpse70451_extract_gallery_atts { public $atts = array(); public $error; // Must get defined in extending class public $part; public $type; public static function init() { is_null( self :: $instance ) AND self :: $instance = new self; return self :: $instance; } public function __construct() { ! isset( $this->part ) AND new WP_Error(); add_action( \'loop_start\', array( $this, \'error_handler\' ) ); // The value of the array is the callback fn name // If it\'s the default, then we\'re on the safe side, // as the core fn is no pluggable and can\'t get overridden. if ( \'gallery_shortcode\' === $GLOBALS[\'shortcode_tags\'] ) { add_filter( \'post_gallery\', array( $this, \'get_gallery_atts\' ), 0, 2 ); } // Else we have to go with a slower regex else { add_filter( \'the_content\', array( $this, \'get_gallery_ids\' ), 0 ); } } public function error_handler() { if ( ! in_array( $this->type ,array( \'digit\' ,\'alphanumeric\' ,\'alpha\' ) ) OR ! empty( $this->type ) ) return new WP_Error( \'invalid_type\' ,__( \'Invalid type set.\', \'wpse70451_textdomain\' ) ,__FILE__ ); } public function __toString() { $is_error = $this->error; if ( ! is_wp_error( $is_error ) ) return; // No error message for Guests or Subscribers // Assuming that no one has activated caching plugins when debugging // and not set WP_DEBUG to TRUE on a live site if ( ! is_user_logged_in() AND ! current_user_can( \'edit_posts\' ) AND ( ! defined( \'WP_DEBUG\' ) OR ! WP_DEBUG ) ) return \'\'; // Error output for development return "{$is_error->get_error_message( \'invalid_type\' )}: {$is_error->get_error_data()}"; } public function get_gallery_ids( $content ) { $pattern = get_shortcode_regex( $content ); preg_match_all( "/{$pattern}/s", $content, $matches ); $atts = explode( " ", array_shift( $matches[3] ) ); foreach ( $atts as $att ) { if ( strstr( $att, $this->part ) ) break; } preg_match_all( $this->get_regex( $this->type ), trim( $att ), $atts ); $this->atts = array_filter( $atts ); return $content; } // Build pattern public function get_regex( $type ) { switch ( $type ) { case \'digit\' : $pattern_atts = \'/(\\d*)/\'; break; case \'alphanumeric\' : $pattern_atts = \'/([A-Za-z0-9]*)/\'; break; case \'alpha\' : $pattern_atts = \'/([A-Za-z]*)/\'; break; default : // Add a method name `get_pattern()` to the extending class // to work with a custom regex pattern. if ( method_exists( $this, \'get_pattern\' ) ) { $pattern_atts = $this->get_pattern(); break; } $pattern_atts = $this->get_regex( \'alphanumeric\' ); break; } return $pattern_atts; } public function get_gallery_atts( $content, $atts ) { $this->atts = $atts[ $this->part ]; // Allow overrides to trigger // at this point we already got what we need return $content; } // Must get defined in extending class public abstract function process_atts() {} } // END Class } // endif;
init
钩住然后跑动父母__construct()
方法检索属性。然后需要定义要检索的属性(请参见类属性
$part
和$type
, 已默认为您要求的内容)。你需要做的最后两个决定是
我想对结果做什么?看见
process_atts()
__construct() 在哪里process_atts()
被钩住了如果需要自定义正则表达式,只需添加一个名为
get_regex()
到您的扩展类和return
您的自定义图案。然后设置$type
收件人和空字符串\'\'
你已经准备好了。
更改您需要的内容,然后再次更改:只需通过FTP将此内容上载到您的插件文件夹并激活即可。<?php /** Plugin Name: (#70451) »kaiser« Get Gallery excludes */ if ( ! class_exists( \'wpse70451_extract_gallery_excludes\' ) ) { add_action( \'init\', array( \'wpse70451_extract_gallery_excludes\', \'init\' ) ); final class wpse70451_extract_gallery_excludes extends wpse70451_extract_gallery_atts { public static $instance; public $part = \'exclude\'; public $type = \'digit\'; public static function init() { is_null( self :: $instance ) AND self :: $instance = new self; return self :: $instance; } public function __construct() { parent :: __construct(); // Example hook: `loop_end` add_action( \'loop_end\', array( $this, \'process_atts\' ) ); } public function process_atts() { $markup = \'\'; // Do something with $this->atts; return print $markup; } } // END Class } // endif;
SO网友:Mridul Aggarwal
如果还需要在同一页上显示内容,那么使用post_gallery
滤器那么你就不需要任何正则表达式了
add_filter(\'post_gallery\', \'gallery_shortcode_excludes\', 10, 2);
function gallery_shortcode_excludes($content, $attr) {
$excludes = $attr[\'excludes\'];
// maybe also save it in some global/class/static variable
// return empty string to let wordpress continue to the shortcode output
return \'\';
}
结束