WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display All Posts in a child Category (12 posts)

  1. Mark Shirley
    Member
    Posted 1 year ago #

    Hello im using the code to display posts on a page but I need to display the child catogory

    <?php
    $featuredPosts = new WP_Query();
    $featuredPosts->query('showposts=5&cat=7');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
    
    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    <div class="meta">
    By <?php the_author() ?>
    </div>
    <div class="storycontent">
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>
  2. Mark Shirley
    Member
    Posted 1 year ago #

    added to post above

    If possible only display posts from a combination of categories or child categories.

  3. Mark Shirley
    Member
    Posted 1 year ago #

    This is the code im using it's fine but would love to show to posts based on a combination of categories.

    <?php
    $featuredPosts = new WP_Query();
    $featuredPosts->query('showposts=5&cat=9');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
    
    <div class="headline-style">
    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    </div>
    <div class="excerpt-content">
    <?php the_excerpt(); ?>
    </div>
    <h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
    <?php endwhile; ?>
  4. Mark Shirley
    Member
    Posted 1 year ago #

    Ok I can show all posts that have categories 9 and 7 but how do I show posts that have a combination of categories 9 and 7 only.
    - my code below:

    <?php $my_query = new WP_Query('showposts=10&cat=7,9'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <div id="wrapper">
    <?php the_title(); ?></a>
    
    <div class="excerpt-content">
    <?php the_excerpt(); ?>
    </div> 
    
    <h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
    </div>
    <?php endwhile; ?>
  5. bythegram
    Member
    Posted 1 year ago #

    See:
    http://codex.wordpress.org/Class_Reference/WP_Query

    Under "Multiple Category Handling"

    you can use 'cat__and' or 'cat__in' I believe. The difference as I understand it is that 'cat__in' will not show posts that are categorized with children of the cats used where as 'cat__and' will (meaning if a post is categorized as '3' who is a child of '2' and you query 'cat__in' => 2, the post will still show).

  6. Mark Shirley
    Member
    Posted 1 year ago #

    Thanks bythegram
    Just looked again at the codex my problem is I dont know how to integrate the code into my code above.
    I have a cat of records (7) that has a child cat of Jazz (9) how do I show just a combination of just records and jazz.

  7. bythegram
    Member
    Posted 1 year ago #

    I haven't tested it but I'd guess:

    first line of your code replace your existing query with:

    <?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__in' => array( 7, 9 )); ?>

    or

    <?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__and' => array( 7, 9 )); ?>

    should do the trick. I'm just not 100% which one. I'd go with 'cat__in' first, it seems to be more exact.

  8. Mark Shirley
    Member
    Posted 1 year ago #

    Thanks - tried adding your code just got white screen -Im just a cut and paste noob unfortunately.

  9. bythegram
    Member
    Posted 1 year ago #

    ok try this:

    <?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__in' => array( 7, 9 ) ) ); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <div id="wrapper">
    <?php the_title(); ?></a>
    
    <div class="excerpt-content">
    <?php the_excerpt(); ?>
    </div> 
    
    <h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
    </div>
    <?php endwhile; ?>

    I missed the closing ')' in the two queries posted originally. Sorry about that.

  10. Mark Shirley
    Member
    Posted 1 year ago #

    Thanks and thanks again it works with the "and" your a star I love wordpress cus of guys like you :-)

  11. Mark Shirley
    Member
    Posted 1 year ago #

    Complete code below:
    Thanks bythegram

    <?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__and' => array( 7, 9 ) ) ); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <div id="wrapper">
    <?php the_title(); ?></a>
    
    <div class="excerpt-content">
    <?php the_excerpt(); ?>
    </div> 
    
    <h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
    </div>
    <?php endwhile; ?>
  12. bythegram
    Member
    Posted 1 year ago #

    Glad it works and thanks for the awesome complement! Not to sound crazy but without all of us (including those who ask the questions) WP wouldn't be what it is. That's the beauty of open-source. Share the knowledge!

    Happy coding!

Topic Closed

This topic has been closed to new replies.

About this Topic