我使用WordPress的,我在這篇文章的標題帶來的最後一個字。 WordPress的管理員不讓我的CSS標題或應用跨班級。使用</p> <pre><code><?php the_title('<h1>', '</h1>'); ?> </code></pre> <p>我想要的目標在標題的最後一個字,以不同的CSS適用於它瞄準的文章標題
回答
這是利用原有WP的東西,基本上我認爲你是最簡單的格式要求的東西:
<?php
$post_title = get_the_title();
$title_as_array = explode(' ', $post_title);
$last_word = array_pop($title_as_array);
$last_word_with_span = '<span class="whatever">' . $last_word . '</span>';
array_push($title_as_array,$last_word_with_span);
$modified_title = implode(' ', $title_as_array);
echo $modified_title;
?>
試用這在this PHP sandbox。你可以看到我用沙箱中的標題替換了一串字(「這是我的標題」) - 它應該像get_the_title()
那樣工作。
這是按「執行代碼」後發生的情況,以防您沒有看到。 PS:爲了解釋,基本上這是:1)獲得標題作爲字符串,2)將其轉換爲數組,3)獲得數組中的最後一項,4)添加span html,5)回到標題數組的末尾,6)再次將數組轉換爲字符串,7)將其打印出來。
var sliced = $('h1').text().split(' ');
var lastword = sliced.pop();
console.log(lastword)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>Im the Last Word</h1>
嘗試這種方式..然後split()
的H1使用pop()
不知道這是工作或沒有它顯示了整個句子 – user3550879
檢查您的控制檯將只顯示'word'使用Firefox和它的最後一個字 – guradio
顯示句子 – user3550879
待辦事項拆你的文字,並通過採用分體式採取硬道理獲得最後一個字,並且必須替換爲文本在那附近。使用.html(function())
像這樣:
var text = $('h1').text().split(' ');
var lstWord = text.pop();
// find the h1 with above word
$("h1:contains('" + lstWord + "')").html(function(i, old) {
// replace the old content(specified words)
// with span tag around that
return old.replace(lstWord, '<span class="myColor">'+lstWord+'</span>');
});
將這個替換整個< ?php the_title ...我不想將它應用於所有h1標籤。只是在帖子標題 – user3550879
這個代碼將取代具有特定搜索詞的所有'h1'標籤。所以,你怎麼知道選擇哪個'h1',除非你知道它們只出現在這個例子的哪個位置:https://jsfiddle.net/norlihazmeyGhazali/ws34491x/ –
還是有點困惑,但是如果我把它放在旁邊PHP和不在< head >它只適用於職位標題? – user3550879
- 1. PHP - > preg_match_all爲以下結構<h6>我的標題</h6>一些文字... <h6>另一個標題</h6>更多的文字
- 2. 添加錨 '<a></a>' 我的標題使它dissapear
- 3. 使用>>,>,> |,||,| <,<,<<,<<
- 4. 標題中的圖例是否有效? <h1><legend>標題</legend></h1>
- 5. 標題中的按鈕是否有效? <h1><button>標題</button></h1>
- 6. Android XML標籤名爲eat-comment,它有什麼用?</p> <pre><code><eat-comment/> </code></pre> <p>它使用的是:
- 7. <a></a><a></a>標籤
- 8. HTML標記:<<< HTML
- 9. <img>標籤內<a>標籤只適用於IE
- 10. jQuery CSS - 不要選擇<img>標籤<a>標籤
- 11. XmlDocument - 我想要<Item></Item>而不是<Item/>
- 12. <script><asp:repeater>中的標記
- 13. <meta name =「title」>標籤和<title></title>標籤
- 14. 添加<span> +</ span>標籤<a>標籤
- 15. <th>的列標題
- 16. 覆蓋<title>標題</title>通過標題
- 17. <a>標籤裏面的span屬性是什麼意思?</p> <pre><code><div id="div-1"> <a href="next" span id="next">hidfoli</a> </div> </code></pre> <p>我想知道什麼呢<code>span</code>內<code>a</code>標籤的意思是:
- 18. 用<HTML>和<BODY>標籤
- 19. 的MySQL/PHP:<code>id</code>,<code>name</code>,<code>category</code>和一個<code>ratings</code>表列:<code>id</code>,<code>item</code>,<code>rating</code>如何使用JOIN語句
- 20. 從標籤解析的Html Agility Pack中的選項&lt;&lt;&lt;&lt;&lt;
- 21. Flexbox的和<header>,<main>,並且<footer>標籤
- 22. 的Rails搞亂我的代碼以「<<<<<<< HEAD」
- 23. 使用</p> <pre><code>form_widget(form.client) </code></pre> <p>我想打印標籤使用Symfony和form_widget()
- 24. Drupal的不加載位於</p> <pre><code>sites/default/files/js </code></pre> <p>OR</p> <pre><code>sites/default/files/css </code></pre> <p>JS/CSS文件默認/文件/
- 25. 使<b></b><i></i><u>在文本框</u>和<br>
- 26. 多<html><body></html></body>在同一個文件
- 27. 不匹配 '運算符<<' 中的std ::運營商<< [隨着_Traits =標準:: char_traits <char>]
- 28. XSD:</p> <pre><code><xs:complexType name="type1"> <xs:sequence> <any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/> </xs:sequence> </xs:complexType> </code></pre> <p>現在提供目標另一個XSD架構:在一個擴展
- 29. 我使用</p> <pre><code>$('div:first', $('#' + id)).css({ 'color': 'red' }) </code></pre> <p>改變我的div的顏色我jQuery.css不起作用
- 30. 的Perl:除了給出一個文件</p> <pre><code>input [7:0] di, input AEN, </code></pre> <p>我想打印</p> <pre><code>di AEN </code></pre> <p>爲輸出模式
所以我把整個PHP,而不是隻使用<?php the_title ... – user3550879
是的,應該這樣做。如果您只需要在特定頁面上執行此操作,則可以使用它的條件標籤(https://codex.wordpress.org/Conditional_Tags)。 – tempranova