WordPress.org

Ready to get started?Download WordPress

Forums

Categories Template - Walker Class to display Categories then top 5 posts inside (2 posts)

  1. Rodeorat316
    Member
    Posted 2 years ago #

    Hi everyone.

    I am working with our web development team and we have hit a stumbling block. What we have is several category templates on our site. We also have categories who have sub-categories then sub-sub categories. What we are trying to do is on our sub-categories page, we would like to display the category name, then if possible display the top 5 most recent posts from that category, all inside a loop. The logic would look like this:

    Category 1:
    Post 1
    Post 2
    Post 3

    Category 2:
    Post 1
    Post 2
    Post 3

    I have done a TON of research before posting here, it looks like everyone is saying that creating a custom WALKER class is the only way to accomplish this. I have tried to impliment the following code:

    [please follow http://codex.wordpress.org/Forum_Welcome#Posting_Code for posting code]

    //USAGE:
    //wp_list_categories( array( 'title_li' => '', 'echo' => false , 'walker' => new PCWalker() ) ) .
    
    class PCWalker extends Walker_Category {
    
      function end_el(&$output, $page, $depth, $args) {
          global $wpdb;
          $output .= "called with: " . $page->term_id;
          $posts = $wpdb->get_results("select object_id as ID from wp_term_relationships r "
            . "join wp_posts p on r.object_id = p.ID where p.post_status = 'publish' and r.term_taxonomy_id = "
            . "(SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy = 'category' and term_id = " . $page->term_id . ")");
          if($posts) :
            $output .= '<ul class="posts">'; foreach($posts as $post) { $output .= '
    
    <li>'; $output .= '<a>ID) . '" href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a>'; $output .= '</li>
    '; } $output .= '
    ';
          endif;
          parent::end_el(&$output, $page, $depth, $args);
        }
    }

    There is a great article explaining the problem and how to fix it, I am just not sure how to impliment this on the category page:
    http://cselian.com/blog/tech/apps/wp-cats-n-posts-in-a-tree/

    I have added the Class to my functions.php file inside my theme, created the function, and I have the following query working inside MySQL, just not sure what is wrong:

    select object_id, p.post_status from uccx_term_relationships r
    join uccx_posts p on r.object_id = p.ID
     where p.post_status = 'publish' and  r.term_taxonomy_id =
     (SELECT term_taxonomy_id FROM uccx_term_taxonomy WHERE taxonomy = 'category' and term_id = 7)

    Any help would be greatly appreciated. We are on a tight deadline, and I have invested $$$ in this website.

    Thanks!

    Alex

  2. alxvallejo
    Member
    Posted 2 years ago #

    Hey Alex, Not saying I'm an expert in the Walker Class area but just wondering why you chose to override the end_el function as opposed to the start_el. I had some similar requirements and I could change the $output HTML with start_el.
    You are probably past this problem so just wanted to say hi.

Topic Closed

This topic has been closed to new replies.

About this Topic