問題描述
有沒有辦法分割帖子內容和圖庫的短代碼。我想要將圖庫顯示在我正常的內容之外,無論放置在何處或何處。我可以使用它來獲取短代碼本身:
if(has_shortcode(get_the_content(), 'gallery')){
$pattern = get_shortcode_regex();
preg_match("/$pattern/s", get_the_content(), $matches);
echo do_shortcode($matches[0]);
}
但是如果畫廊的短碼不是第一個實例,這不行。有沒有辦法完全拆分我的內容和圖庫?
編輯:我有一個 semi-solution,但它似乎是一個漫長的方式去做。它首先抓住了帖子中的第一個短代碼 (需要修復,因為我只想要”Gallery” 短代碼),然後從內容中刪除所有的快捷碼 (再次,不是我真正想做的)
<?php if(has_shortcode(get_the_content(), 'gallery')) : ?>
<?php
$pattern = get_shortcode_regex();
preg_match("/$pattern/s", get_the_content(), $matches);
?>
<div id="content">
<?php echo strip_shortcodes(get_the_content()); ?>
</div>
<div id="gallery">
<?php echo do_shortcode($matches[0]); ?>
</div>
<?php endif; ?>
編輯#2 – 好的,我已經能夠在文章中只獲取圖庫短信。我還添加了一個過濾器來刪除圖庫短代碼 the_content() – 問題是它不一定會刪除短代碼,因為它發佈,但它不允許我運行”do_shortcode()”
的 functions.php
function remove_gallery($content) {
global $post;
if($post->post_type == 'artcpt')
remove_shortcode('gallery', $content);
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
循環
<?php preg_match('/]+]/', get_the_content(), $matches); ?>
<div id="content">
<?php the_content(); ?>
</div>
<div id="gallery">
<?php echo do_shortcode($matches[0]); ?>
</div>
在循環中,它將返回我的短代碼兩次 (我在一個頁面上,應該循環兩次 – 所以它不運行 do_shortcode()) 。不知道為什麼
最佳解決方案
開放給任何人誰可以簡化這一點,但這裏是我想出了對我有用的。
第一件事是第一件事 – 一旦循環開始,就得到畫廊,使用 get_post_gallery()
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) :
the_post();
$gallery = get_post_gallery();
$content = strip_shortcode_gallery( get_the_content() );
$content = str_replace( ']]>', ']]>', apply_filters( 'the_content', $content ) );
?>
<div id="content">
<?php echo $content; ?>
</div> <!-- id="content" -->
<div id="gallery">
<?php echo $gallery; ?>
</div> <!-- id="gallery" -->
<?php endwhile; ?>
<?php endif; ?>
strip_shortcode_gallery()函數 – functions.php
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( false !== $pos ) {
return substr_replace( $content, '', $pos, strlen( $shortcode[0] ) );
}
}
}
}
return $content;
}
資源:
堆棧溢出:
我原本打算什麼,沒有按預期工作:
次佳解決方案
核心短碼正則表達式
基本上我們可以用正則表達式來實現,實際上即使是由 get_shortcode_regex()由核心提供的正則表達式。
首先,我們需要抓取短代碼標籤並構建一個正則表達式。核心功能 get_shortcode_regex()不幸的是沒有機會拋出一個參數,所以我們會留下一個正則表達式,匹配每個短代碼,這是不需要的,因為我們只想要定位短代碼。
// Get all tags as Array
$tags = $GLOBALS['shortcode_tags'];
// remove the gallery-shortcode; 'gallery' is the key
unset( $tags['gallery'] );
// retrieve all shortcodes (but not 'gallery') separated by a vertical pipe char/the "or" Regex char
$tags = join( '|', array_map(
'preg_quote',
array_keys( $GLOBALS['shortcode_tags'] )
) );
趕上所有的畫廊
接下來,我們需要一個可以捕獲所有畫廊的正則表達式。因此,我們將調用 preg_match_all(),因為它將使用 0 索引 (其餘部分匹配並且可以忽略) 返回庫中短碼的所有匹配數組。
$pattern = get_shortcode_regex();
preg_match_all( "/$pattern/s", get_the_content(), $galleries );
現在 $galleries[0]擁有一系列圖庫短碼標籤。
內容沒有畫廊
接下來,我們需要做的是從內容中刪除所有短碼。我們將再次使用相同的正則表達式,並在 get_the_content()上運行它。當然,我們應用 the_content 過濾器,因為在渲染時可以通過回調添加短代碼。
$content = preg_replace_callback(
"/$pattern/s",
'strip_shortcode_tag',
apply_filters( 'the_content', get_the_content() )
);
$content 變量現在保存我們的內容。
示例回調來更改內容
或者:您如何將內容分割成畫廊和其他帖子
在回調期間,我們可以輕鬆地將內容替換為我們的新內容:
$tags = $GLOBALS['shortcode_tags'];
unset( $tags['gallery'] );
$tags = join( '|', array_map(
'preg_quote',
array_keys( $GLOBALS['shortcode_tags'] )
) );
$pattern = get_shortcode_regex();
preg_match_all( "/{$pattern}/s", get_the_content(), $galleries );
echo $galleries;
echo "<hr>";
echo preg_replace_callback(
"/{$pattern}/s",
'strip_shortcode_tag',
apply_filters( 'the_content', get_the_content() )
);
這將首先添加所有畫廊,然後添加我們的內容沒有畫廊,兩者都用水平規則分隔。這只是一個起點。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。