WordPress.org

Ready to get started?Download WordPress

Forums

List post titles in a certain category (19 posts)

  1. wynderlon
    Member
    Posted 8 years ago #

    I think I'm going blind, because I cannot find the answer to this and yet it seems fairly simple. In my sidebar, I want to make a "Photography" sub-header, then underneath it list all the titles of the posts within the "Photography" category. Is there a way to do this? Thanks. :)

  2. Kafkaesqui

    Posted 8 years ago #

    In a list:

    <ul><li><h2>Photography</h2>
    <ul>
    <?php
    query_posts('category_name=Photography&showposts=-1'); while(have_posts()) : the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    </li></ul>

    Reference:
    http://codex.wordpress.org/Template_Tags/query_posts

    It's possible this might cause conflicts with an existing post loop on the page, so this is an alternative:

    <ul><li><h2>Photography</h2>
    <ul>
    <?php
    $photog_posts = new WP_Query('category_name=Photography&showposts=-1'); while($photog_posts->have_posts()) : $photog_posts->the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    </li></ul>

    Reference:
    http://codex.wordpress.org/Function_Reference/WP_Query

    And one more option (assumes "Photography" has the category ID 10):

    <ul><li><h2>Photography</h2>
    <ul>
    <?php
    $photog_posts = get_posts('category=10&numberposts=-1'); foreach($photog_posts as $post) : setup_postdata($post);
    ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </li></ul>

    Reference:
    http://codex.wordpress.org/Template_Tags/get_posts

    Options, baby...

  3. wynderlon
    Member
    Posted 8 years ago #

    Perfect, thanks! I was just missing a few odds and ends in the code. :)

  4. katiebird
    Member
    Posted 8 years ago #

    Hi,

    I'm interested in something very similar to this. I am currently maintaining pages that list links to each article in a particular category.

    These are 'special categories' that lots of my visitors like to check regularly. And I don't want the whole post listed on the page -- just the title with a link to the article.

    But, I don't have permalinks enabled (I'm afraid of destroying my site by attempting it.)

    So, I don't think these solutions work for me (or I'm doing something else wrong?)

    Is there a way to implement a list like this by using something besides the permalink?

    Or possibly a permalinks for dummies instruction sheet :) If they are absolutely required??

    Any help would be wildly appreciated.

  5. moshu
    Member
    Posted 8 years ago #

    You are mixing two very different things...

    What is commonly known as "permalinks" - more exactly the "nice permalinks" - has nothing to do with the template tag called the_permalink.
    Using the template tag (see details here: http://codex.wordpress.org/Template_Tags/the_permalink ) is just a way to tell the WP engine to take you to the "single post view" of that post.

    Whether the 'visible' permanent link to that single post has a form in your address bar like:
    example.com/?p=123
    or
    example.com/archive/this-is-my-post
    ..is completely irrelevant.

    Which means, you should use the code above, and before posting theoretical questions you can always test it ;)

  6. katiebird
    Member
    Posted 8 years ago #

    "Which means, you should use the code above, and before posting theoretical questions you can always test it ;)"

    I did. I tested all 3 examples and poked around for a couple of hours trying to get them to work before posting my question. And also tried a couple of alternative samples too.

    I guess I should have said that explicitly. But the fact is I've never asked a question without trying to solve the problem myself.

    ;)

  7. katiebird
    Member
    Posted 8 years ago #

    Oh, I guess you're saying that the code should be working for me. So, I must just be doing something wrong.

    And I guess I'll have to keep poking at it.

    I do appreciate your help.

  8. chinaski
    Member
    Posted 7 years ago #

    I was searching in the WP support for help in listing post titles in a category and found this wonderfull tip.

    Tested and works like a charm. However, my only question is, how to replace "a particular category" for "the current category"?

    get_posts('category=10&numberposts=-1'); foreach($photog_posts as $post) : setup_postdata($post);

    this line gets post for the category #10, but what I need is to get posts for the current category, because i'm using it in the archive.php general template.

    thanx in advance.

    Federico.

  9. Kafkaesqui

    Posted 7 years ago #

    Federico, $cat is a global variable in WordPress which holds the current category ID -- on a category query/page. So:

    get_posts("category=$cat&numberposts=-1");

    should do what you're after.

  10. chinaski
    Member
    Posted 7 years ago #

    Yes, that works! ;)
    Thanks x 1000.

    Federico.

  11. Dash
    Member
    Posted 7 years ago #

    Federico & Kafka,

    I tried to do the same thing, using
    get_posts("category=$cat&numberposts=-1");

    but I got a wordpress database error that read "Unknown column '$cat' in 'where clause'"

    Any ideas on why this is the case? Thanks a bunch.
    Dan

  12. thopunk
    Member
    Posted 7 years ago #

    how can i sort this code from OLDEST to NEWEST.

  13. minter
    Member
    Posted 7 years ago #

    I've used this code in a sidebar, and am getting odd results. While I'm getting an li entry for each post in the category I've chosen, the title and link are for the most recent post on the front page. It seems like the setup_postdata isn't taking the variable defined in the foreach, instead getting the other post. Thoughts?

    Here's the code:

    <ul><li><h2>IE Missions</h2>
    <ul>
    <?php
    $mission_posts = get_posts('category=6&numberposts=-1'); foreach($mission_posts as $mypost) : setup_postdata($mypost);
    ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </li></ul>
  14. LeaseahB
    Member
    Posted 7 years ago #

    Kafkaesqui,

    Just wanted to say many, many thanks! I did what you listed here and it works perfectly. I was banging my head trying to go through the documentation trying to figure out what I was doing wrong. I'm so thankful I came across your answer here!

    Thank you! Thank you! Thank you!
    Lisa B.

  15. syncbox
    Member
    Posted 7 years ago #

    Is there any way to use the code samples (to list a specific category's titles or titles and content) above but have it generate only 1 or 2 posts randomly?

    I've tried using the custom query plugin but it doesn't seem to actually limit to a specific category, despite what the plugin author says.

    I basically would like to show 2 or 3 posts (title and content) from a specific category on my homepage.

    I've also looked at various randomizing plugins, but they don't allow me to choose the specific category.

    help greatly appreciated!

  16. maciek773
    Member
    Posted 7 years ago #

    $cat doesnt work for me. when i add id works well. code (category.php):

    <ul>
    
    <?php
    $photog_posts = get_posts("category=24&numberposts=-1"); foreach($photog_posts as $post) : setup_postdata($post);
    ?>
    	<li>
    
    		<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    
    		<?php the_excerpt_reloaded(60, '<a><img><p/>', 'excerpt', TRUE, 'Czytaj dalej...
    ', FALSE, 1, TRUE); ?>
    
    	</li>
    
    <?php endforeach; ?>
    
    </ul>
  17. maciek773
    Member
    Posted 7 years ago #

    any1?

  18. archasek
    Member
    Posted 7 years ago #

    help i need that too ;) im beggin, please :)

  19. LeaseahB
    Member
    Posted 7 years ago #

    <?php
    $photog_posts = get_posts("category=24&numberposts=-1"); foreach($photog_posts as $post) : setup_postdata($post);
    ?>

    I think it should be single quotes around the category and numberposts statement like this:

    <?php
    $photog_posts = get_posts('category=24&numberposts=-1'); foreach($photog_posts as $post) : setup_postdata($post);
    ?>

    Try that. That's what is working for me.

    On another note, is there a way to reverse the order of the list? from oldest to newest?

    Lisa B.

Topic Closed

This topic has been closed to new replies.

About this Topic