Support » Plugins and Hacks » Categories with Description and the lastest Posts: How To

  • Hello,

    May I trouble someone to share how to approach this issue.
    I would like to display Categories on my blog as follows:

    1. Category Name
    2. Category descrition limited to 50 characters
    3. 3 posts for each categorie with these attributes:

    • descending last 3 posts
    • limited to 30 characters
    • indented by 1em
    • a little graphic arrow called: catArrow.jpg proceeding each post

    Thank you in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Leave this example for you. Last two items ‘1em’ and ‘arrow’ are yours to play with 😉

    <?php
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 3,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category description 50 chars: ' . substr($category->description,0,50) .'</p> ';
            foreach($posts as $post) {
              setup_postdata($post);
              echo '<p>Post title 30 chars is: ' . substr($post->post_title,0,30) . '</p> ';
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    Thank you for your answer.—But, I think there was a misunderstanding. The lines I wrote in my question that I used the

  • feature are ATTRIBUTES of the “Post Items”. (sorry)
  • Here is what I am after if possible
    example:
    I have a category: “Fruits I like”
    Category Description: I like fruit because it is tasty and colorful, sweet and smelly.
    and three recent posts are: Apples, Grapes, Oranges

    I would like the category and posts to display as such:

    Fruits I like
    I like fruit because it is tasty and colorful, sweet and…
    ->Apples
    ->Grapes
    ->Oranges

    Thank you, I must go eat now that I made myself hungry. Too bad I really hate fruit LOL

Just a little rearrange of the output is all that’s necessary

use this for your category name and description:

echo ‘<p>’ . $cat->name .'</p>’;
echo ‘<p>’ . substr($category->description,0,50) . ‘</p>’;

and use this for your post title, assuming Apple is your post title:

echo ‘<p> -> ‘ . substr($post->post_title,0,30) . ‘</p> ‘;

I am looking to do the same type of thing. I want sub-categories to show in the sidebar under my main category like so:

Fruit I Like
– Apples
– Grapes
– Oranges

Fruit I Don’t Like
– Pears
– Peaches

Do I do it as per your instructions above? If so where do I make the changes?

HI Sylvia,

This is what worked for me. It is basically what MichaelH provided me with in this thread (Thanks Again!)
I put this code into my sidebar, then defined the classes catTitle and catDesc in the style sheet (style.css in most themes).
I used it in my friends blog here:
http://www.westcoastcoaching.com
She uses her categories more like headings with her last three posts showing. You can change that number in this line
‘showposts’ => 3,
This code also will reveal the description of the category as it is written in the admin (dashboard) under categories. In the example site above there is nothing written in the category description, but is there if you need it.
———————————– JPF

<?php
$cat_args=array(
‘orderby’ => ‘name’,
‘order’ => ‘ASC’
);

$categories=get_categories($cat_args);

foreach($categories as $category) {
$args=array(
‘showposts’ => 3,
‘category__in’ => array($category->term_id),
‘caller_get_posts’=>1
);
$posts=get_posts($args);
if ($posts) {
echo ‘<p>term_id.'” class=”catTitle”>’. substr($category->name,0,30) .’</p> ‘;
echo ‘<div class=”catDesc”>’. substr($category->description,0,100) .'</div> ‘;

foreach($posts as $post) {
setup_postdata($post);
echo ‘<p>ID.'” class=”catPosts”>’ . substr($post->post_title,0,45) . ‘</p> ‘;
} // foreach($posts
} // if ($posts
} // foreach($categories

?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Categories with Description and the lastest Posts: How To’ is closed to new replies.