开发者

How do you edit the "Posted To Category" part of a Wordpress post to 'linkify' the entire phrase instead of just the category?

Hey guys, I have a pretty basic question (even though I know the title sounds confusing) which is best explained as follows. This code is what I currently have:

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

Which generates this HTML code:

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

As you can see, the category is 'linkified' by this code, however I would like the entire开发者_如何转开发 phrase to be the anchor text for the link, like this:

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

How would I modify the original PHP code to include the "Posted to" text in the hyperlink's anchor text? Much appreciated! :)


You can use get_the_category() and get_category_link() functions as follows:

<?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> ';
?>

However you may like to consider what happens when a post is in more than one category: your original code will show a list of category links separated by commas (e.g. "Posted in foo, bar, qux"), whereas this code will show "Posted in [name]" for each category (e.g. "Posted in foo Posted in bar Posted in qux").

It should also be noted that this answer does not use WordPress's __() function to translate the resulting text where appropriate: you may like to consider how best to support non-English speakers.

Also, it's not necessary to close and reopen PHP tags after each contiguous command - just separate them with semicolons.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜