>本文演示瞭如何使用內置<more></more>
標籤將WordPress內容分為多個部分,並提供了用戶友好的HTML或快捷代碼的替代方案。 該方法避免了需要從內容作者那裡進行編碼的知識。
wordpress通常使用the_content()
輸出頁面/發佈內容。但是,將內容分為塊(例如,用於多列佈局)需要採取不同的方法。 get_the_content()
提供內容作為PHP變量,但識別分區點需要仔細考慮。 通用解決方案,例如在HTML標籤上分組(例如<h2></h2>
標題)或使用短代碼,當前限制:HTML標籤方法需要內容作者HTML知識和限制靈活性,而折疊代碼則負擔為編輯器以記住特定代碼。
<more></more>
在主題文件夾中實現,修改或創建文件。 添加以下PHP函數:
functions.php
接下來,在主題循環中找到wp-content/themes
>呼叫(可能在
// split content at the more tag and return an array function split_content() { global $more; $more = true; $content = preg_split('//i', get_the_content('more')); for($c = 0, $csize = count($content); $c < $csize; $c++) { $content[$c] = apply_filters('the_content', $content[$c]); } return $content; }
或the_content()
)。 評論single.php
,然後用調用page.php
替換。此函數返回一個數組,其中每個元素代表由index.php
archive.php
search.php
請記住將其適應您主題的特定結構。 這種方法提供了一種干淨,用戶友好的方法,用於在沒有復雜的編碼或插件依賴項的情況下對WordPress內容進行分配。 the_content()
>
split_content()
<more>
經常詢問有關將WordPress內容分為多個部分的問題(常見問題解答)
<?php //Example usage: (Adapt to your theme's structure) add_filter( 'the_content', 'my_custom_content' ); function my_custom_content( $content ) { return str_replace( 'Read the rest of this page »', '', $content ); } // split content into array $content = split_content(); // output first content section in column1 echo '<div>', array_shift($content), '</div>'; // output remaining content sections in column2 echo '<div>', implode($content), '</div>'; ?>
以上是如何將WordPress內容分為兩個或多列的詳細內容。更多資訊請關注PHP中文網其他相關文章!