簡體   English   中英

您如何編輯Wordpress帖子的“發布到類別”部分,以“鏈接”整個短語,而不只是類別?

[英]How do you edit the “Posted To Category” part of a Wordpress post to 'linkify' the entire phrase instead of just the category?

大家好,我有一個很基本的問題(即使我知道標題聽起來很混亂),最好按以下說明進行解釋。 這段代碼是我目前擁有的:

<?php if (!is_page()): ?><?php ob_start(); ?><?php printf(__('Posted to %s', 'kubrick'), get_the_category_list(', ')); ?>

生成以下HTML代碼的代碼:

Posted to <a href="http://www.example.com/category/" title="View all posts in Category" rel="category tag">Category</a>

如您所見,此代碼將類別“鏈接化”,但是我希望整個短語成為鏈接的錨文本,如下所示:

<a href="http://www.example.com/category/" title="View all posts in Category" rel="category tag">Posted to Category</a>

如何修改原始PHP代碼以在超鏈接的錨文本中包含“發布到”文本? 非常感激! :)

您可以按以下方式使用get_the_category()get_category_link()函數:

<?php
    foreach(get_the_category() as $category)
        echo '<a href="'.get_category_link($category->cat_ID).'" title="View all posts in Category" rel="category tag">Posted to '.$category->cat_name.'</a> ';
?>

但是,您可能想考慮當一個帖子屬於多個類別時會發生什么:您的原始代碼將顯示由逗號分隔的類別鏈接列表(例如“發布在foo,bar,qux中”),而此代碼將顯示“對於每個類別,都以[name]形式發布(例如,“以foo形式發布,以bar形式發布,以qux形式發布”)。

還應注意,此答案未在適當的地方使用WordPress的__()函數來轉換結果文本:您可能想考慮如何最好地支持非英語使用者。

另外,不必在每個連續的命令后關閉並重新打開PHP標記-只需用分號將它們分開即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM