WordPress.org

Ready to get started?Download WordPress

Forums

Category mysql query (4 posts)

  1. erindesign
    Member
    Posted 5 years ago #

    Hey all,

    Trying to get posts by category but this new version is a little messy.

    I cannot find the category relations to posts at all. I need to get say all the posts in a category. This is for the home page I need to get the latest post from each category and set it up with like the latest news.

    Thanks in advance,
    Erind

  2. MichaelH
    Member
    Posted 5 years ago #

    This will give you something to look at:

    <?php
    //get categories, cycle through categories and if a category has posts, then get one post in that category and display it
    $categories=get_categories();
    if ($categories) {
      foreach($categories as $category) {
        if ($category->count > 0) {
          echo 'Category <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> has ' . $category->count . ' post(s).
     ';
          $number_posts_per_category = 1; //set =0 to see all posts in a category
          $posts=get_posts('cat='. $category->term_id .'&showposts='.$number_posts_per_category);
          foreach($posts as $post) {
            setup_postdata($post); ?>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
            <?php the_content();
          }
        }
      }
    }
    ?>
  3. erindesign
    Member
    Posted 5 years ago #

    Thanks a lot, that works excellent.

    How would i limit the_content(); to say 50 characters?

  4. MichaelH
    Member
    Posted 5 years ago #

    Use <--more--> in your posts,

    or

    use the template tag, the_excerpt() instead of the_content.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags