WordPress.org

Forums

[resolved] Can anyone tell me where I am wrong in my code (6 posts)

  1. malefactor
    Member
    Posted 2 years ago #

    Hello guys. I have some problem with my code.. I am trying to make code which gonna display the last post from each parent category listed in array.
    I mean, there is no error message but also there is no posts showing up.

    <?php
    wp_reset_query();
    
    $args = array( 'category__and' => array(3,4,5,6,7), 'posts_per_page' => 1);
    
    query_posts( $args );
    
    if (have_posts()) :
    echo '<h2>'.$cat->name.'</h2>';
    while (have_posts()) : the_post(); ?>		
    
    <!-- this area is for the display of your posts the way you want it -->
    <!-- i.e. title, exerpt(), etc. -->
    
    <?php endwhile; ?>
    <?php else :
    echo '<h2>'.$cat->name.'</h2>';
    endif; wp_reset_query;
    ?>

    I must solve this problem, I'm despaired.
    Thanks!

  2. jkovis
    Member
    Posted 2 years ago #

    Have you tried printing out $wp_query to see which query parameters WordPress is using?

    Add something like the following to your code, save, and then view source.

    $args = array( 'category__and' => array(3,4,5,6,7), 'posts_per_page' => 1);
    query_posts( $args );
    <!-- wp_query = <?php print_r($wp_query); ?> -->

    I'd also consider using WP_Query instead of query_posts since you won't have to mess with the original query

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    display the last post from each parent category listed in array

    the code will only show just one post that has all these categories at the same time;

    i.e. this post to show must have the categories 3,4,5,6,7 assigned to it.

    display the last post from each parent category listed in array

    does this include any posts in a child category?

    if so, you might need to run a foreach loop with the category ids
    example:

    $cat_array = array(3,4,5,6,7);
    foreach( $cat_array as $cat_id ) :
    /*THE QUERY AND LOOP HERE*/
    endforeach;

    and use
    'cat' => $cat_id in the query (to allow child cat posts)
    or
    'category__in' => $cat_id (for posts just in the cat)

    http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters

  4. malefactor
    Member
    Posted 2 years ago #

    Thanks guys appreciate. Almost there :) Everything is working fine, only the category name is missing. Latest Posts in ??? Category

    This is my code now.

    <?php
    wp_reset_query();
    
    $cat_array = array(3,4,5,6,7);
    foreach( $cat_array as $cat_id ) :
    
    $args = array(
    'posts_per_page' => 1,
    'category_name' => $cat_id->slug,
    'cat' => $cat_id,);
    
    query_posts($args); // reset to original
    
    if (have_posts()) :
    echo '<h2>Latest Posts in '.$cat_id->name.' Category</h2>';
    ?>
    
    <?php while (have_posts()) : the_post(); ?>		
    
    <div>
     <h2>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
     </h2>
    </div>
    
    <?php
      if ( is_category($vidcat) ) {  the_content();  }
      else { echo strip_tags(get_the_excerpt(), '<a><strong>'); }
    ?>
    
    <!-- this area is for the display of your posts the way you want it -->
    <!-- i.e. title, exerpt(), etc. -->
    
    <?php endwhile; ?>
    <?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>';?>
    <?php endif; wp_reset_query; ?>
    <?php endforeach; ?>
  5. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    $cat_id is really only the category id, not the category object.

    try:

    echo '<h2>Latest Posts in '. get_category($cat_id)->name .' Category</h2>';

    http://codex.wordpress.org/Function_Reference/get_category

  6. malefactor
    Member
    Posted 2 years ago #

    awesome! many thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic