最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - echo category description in single.php

programmeradmin4浏览0评论

i asked this on SO but got no where. i will try asking here.

this works fine in category.php <?php echo category_description(the_category_id()); ?>

but it does not work in single.php, just the category id shows up not the description.

any ideas, how to get this done?

thanks in advance

Edit:

in category: <?php echo strip_tags(category_description(4)); ?>

in single <?php echo category_description(the_category_id()); ?> cat id = 4

i know i'm not striping the tags..for single

i'm just trying to display the cat desc when in the single post page.

i asked this on SO but got no where. i will try asking here.

this works fine in category.php <?php echo category_description(the_category_id()); ?>

but it does not work in single.php, just the category id shows up not the description.

any ideas, how to get this done?

thanks in advance

Edit:

in category: <?php echo strip_tags(category_description(4)); ?>

in single <?php echo category_description(the_category_id()); ?> cat id = 4

i know i'm not striping the tags..for single

i'm just trying to display the cat desc when in the single post page.

Share Improve this question edited Dec 26, 2010 at 4:00 andrewk asked Dec 25, 2010 at 22:13 andrewkandrewk 1711 gold badge2 silver badges17 bronze badges 2
  • Not surprising you'd get no help for WordPress at SO; they are not WordPress enthusiasts, we are. :) Can you please provide some context for your question? Can you show the code from category.php where you are displaying the code where it works and where in single.php you want the code to work? Where you want to use it matters a bit. – MikeSchinkel Commented Dec 26, 2010 at 3:27
  • @Mike thanks, actually in both cases(single + cat) I am calling the function outside the loop..right before the loop starts... i edited the q a bit. – andrewk Commented Dec 26, 2010 at 3:57
Add a comment  | 

1 Answer 1

Reset to default 5

Try the code that follows the screenshot:


(source: mikeschinkel)

<?php
  $categories = get_the_category();
  foreach($categories as $key => $category) {
    $url = get_term_link((int)$category->term_id,'category');
    $categories[$key] =
      "<dt><a href=\"{$url}\">{$category->name}</a></dt>" .
      "<dd>{$category->category_description}</dd>";
  }
  echo "<dl>\n" . implode("\n",$categories) . "\n</dl>";
?>

Also, <?php echo category_description(the_category_id()); ?> doesn't do what you think it does. What follows will work on your category pages because it assumes the category ID for the category page:

<?php echo category_description(); ?>

FYI, the_category_id() will echo the value of the current category ID, it doesn't actually pass anything to category_description() is looks like was your assumption. Besides, the_category_ID() is deprecated so you wouldn't want to use it anyway. By the way, I'll bet you are seeing an errant number being displayed just before the category description is displayed?

发布评论

评论列表(0)

  1. 暂无评论