WordPress.org

Ready to get started?Download WordPress

Forums

a simple, list of posts, of a category (6 posts)

  1. Elan42.com
    Member
    Posted 6 years ago #

    Hi...

    I need a list of posts (name, link, title) of a certain category, to do a menu, how can I obtain them?

    For what I know, there is not a wordpress tag for that task...

    p.s. I'm not in the loop.

  2. Kafkaesqui

    Posted 6 years ago #

    "p.s. I'm not in the loop."

    That is why you'll need to create a new loop:

    http://codex.wordpress.org/The_Loop#Multiple_Loops

    For a "simple list" of all posts in a category:

    <ul>
    <?php
    $my_query = new WP_Query('category_name=special-cat&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
  3. Elan42.com
    Member
    Posted 6 years ago #

    Ok... so that is the famous "mini-loop".

    Are there any better solutions in the stocks?

    Cause that is very painful for my website, cause I have to do that at least 6 times (6 are the main categories of my website).

    In the menu I want to put particular posts to link directly, like if they are pages.

    In the meantime,
    Thank you.

  4. Kafkaesqui

    Posted 6 years ago #

    First, try to explain in detail what results you are looking for in your first post to the forums. Nothing we like more than having to re-answer a question (over, and over, and over again).

    Anyway, this will create a loop of categories, which will then display the posts from each. We've changed to category ID numbers because it makes things a little easier here (trust me). Just edit the value of $cat_ids so it represents the categories you want displayed (separate them with commas as below):

    <?php
    $cat_ids = '1, 2, 3, 4, 5, 6';
    
    $my_cats = explode(',', $cat_ids);
    foreach( $my_cats as $my_cat ) :
    ?>
    <ul><li><h2><?php echo get_the_category_by_id($my_cat); ?></h2>
    <ul>
    <?php
    $my_query = new WP_Query('cat=' . $my_cat . '&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    </li></ul>
    <?php endforeach; ?>
  5. Elan42.com
    Member
    Posted 6 years ago #

    @Kafkaesqui: is it possible someway to extract only the post with a certain custom field?
    or, is it possible to exclude from the category the posts that are contained in a sub-category of the category itself?

  6. Kafkaesqui

    Posted 6 years ago #

    Nokao, both are certainly possible, but you'll have to dump the use of WP_Query() and build your own custom query to extract those posts from the database.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.