是否可以从内容中删除库,但可以在其位置执行以下操作get_template_part(\'content-gallery\');
?
我有一段代码可以从帖子中删除库,但我正在插入内容库。php,它以我想要的方式获取和格式化库。这很好,但图库是在文章的末尾插入的。问题是,它需要插入到文章中的任何位置。我只是想快速修复一下。
功能。php-删除库
add_filter(\'the_content\', \'remove_gallery\');
function remove_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;
}
内容库。php-手动图库-调用图像并轻松格式化
$gallery = get_post_gallery( get_the_ID(), false );
$gallery_attachment_ids = explode( \',\', $gallery[\'ids\'] );
$img_attribs = wp_get_attachment_image_src($gallery_attachment_ids[0], \'gallery_main\' ); // returns an array
if( $img_attribs ) :
foreach($gallery_attachment_ids as $id) :
$img = wp_get_attachment_image_src($id, \'gallery_thumb\' );
$img_full = wp_get_attachment_image_src($id, \'gallery_main\' );
$img_alt = get_post_meta(get_the_ID(), \'_wp_attachment_image_alt\', true);
?>
<div class="item">
<img class="thumb img-responsive" data-url="<?php echo $img_full[0]; ?>" id="<?php the_ID(); ?>" src="<?php echo $img[0]; ?>" alt="<?php echo $img_alt; ?>" title="<?php echo $img_alt; ?>" />
</div>
<?php endforeach; ?>
这是在
page.php
(我需要删除手动调用
get_template_part()
并调用它的内部过滤函数。
<?php the_content(); ?>
<?php get_template_part(\'content-gallery\'); ?>
编辑
全过滤器功能
add_filter(\'the_content\', \'remove_gallery\');
function remove_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) {
ob_start;
get_template_part(\'content-gallery.php\');
$contentgallery = ob_get_contents();
ob_end_clean();
return substr_replace( $content, $contentgallery, $pos, strlen($shortcode[0]) );
}
}
}
}
return $content;
}