WordPress.org

Support

Support » How-To and Troubleshooting » Category mysql query

Category mysql query

  • 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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();
          }
        }
      }
    }
    ?>

    Thanks a lot, that works excellent.

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

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

    or

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Category mysql query’ is closed to new replies.
Skip to toolbar