問題描述
有沒有辦法分割帖子內容和相簿的短程式碼。我想要將相簿顯示在我正常的內容之外,無論放置在何處或何處。我可以使用它來獲取短程式碼本身:
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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。