Sim*_*mon 4 variables wordpress gallery shortcode
尝试从帖子内容中删除图库短代码并保存在变量中以供模板中的其他位置使用.新的Wordpress图库工具非常适合选择他们想要的图像并分配字幕,希望使用它来创建图库,然后将其从前端的内容中拉出来.
所以这个小剪切工作正常,删除画廊和重新应用格式...但我想保存该画廊短代码.
$content = strip_shortcodes( get_the_content() );
$content = apply_filters('the_content', $content);
echo $content;
Run Code Online (Sandbox Code Playgroud)
希望保存短代码,以便将其解析为数组并用于在前端重新创建自定义库设置.我试图保存的这个短代码的一个例子是......
[gallery ids="1079,1073,1074,1075,1078"]
任何建议将不胜感激.
小智 6
从帖子内容中获取First Gallery短代码的功能:
// Return first gallery shortcode
function get_shortcode_gallery ( $post = 0 ) {
if ( $post = get_post($post) ) {
$post_gallery = get_post_gallery($post, false);
if ( ! empty($post_gallery) ) {
$shortcode = "[gallery";
foreach ( $post_gallery as $att => $val ) {
if ( $att !== 'src') {
if ( $att === 'size') $val = "full"; // Set custom attribute value
$shortcode .= " ". $att .'="'. $val .'"'; // Add attribute name and value ( attribute="value")
}
}
$shortcode .= "]";
return $shortcode;
}
}
}
// Example of how to use:
echo do_shortcode( get_shortcode_gallery() );
Run Code Online (Sandbox Code Playgroud)
从帖子内容中删除第一个库短代码的功能:
// Deletes first gallery shortcode and returns content
function strip_shortcode_gallery( $content ) {
preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
if ( ! empty( $matches ) ) {
foreach ( $matches as $shortcode ) {
if ( 'gallery' === $shortcode[2] ) {
$pos = strpos( $content, $shortcode[0] );
if ($pos !== false)
return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
}
}
}
return $content;
}
// Example of how to use:
$content = strip_shortcode_gallery( get_the_content() ); // Delete first gallery shortcode from post content
$content = str_replace( ']]>', ']]>', apply_filters( 'the_content', $content ) ); // Apply filter to achieve the same output that the_content() returns
echo $content;
Run Code Online (Sandbox Code Playgroud)