編輯
這段代碼可以嗎?
它可以在虛擬網站上運行,但恐怕不會破壞實時網站。
如果用于鏈接,如果鏈接有大寫,會有問題嗎?
function shortcode_page_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'shortcode_page_title', );
function shortcode_title_first_word( ){
$title = get_the_title();
$title_words = explode(' ', $title);
return $title_words[0];
}
add_shortcode( 'title_first_word', 'shortcode_title_first_word', );
謝謝@ADyson,提供資源。
初始職位
我不會編碼 :( 我正在使用Sepster 的解決方案來獲取頁面標題。
function shortcode_page_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'shortcode_page_title', );
如何插入另一個步驟并僅選擇標題的第一個單詞?
我見過explode函式和陣列選擇,但我不知道如何實作它們。
謝謝!
uj5u.com熱心網友回復:
如果您仍然想使用短代碼作為在任何需要的地方插入標題的方式,那么您只需將上面的 2 個函陣列合起來并將其插入到 functions.php 檔案的底部(最好是在子主題上) .
function first_word_from_title( ){
$title = get_the_title(); // Retrieves the title
$title_words = explode(' ', $title); // Transforms the title into an array composed of each separate word
return $title_words[0]; // Returns the first element of the array
}
add_shortcode( 'page_title', 'first_word_from_title');
我認為您不必擔心上述代碼會導致崩潰,因為它僅在您插入短代碼時運行,而不是在每次頁面加載時運行。
您始終可以先在草稿帖子上對其進行測驗,然后查看是否出現錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/414203.html
標籤:
下一篇:PHP替換陣列中的字符
