WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how can specify a current category??? (10 posts)

  1. Yukon Cornelius
    Member
    Posted 4 years ago #

    Hi,
    I've recently created a featured post slider that pulls recent posts from a featured category, with the following code:

    <?php
                $featured_posts = get_posts('numberposts=50&category=8');  
    
                foreach( $featured_posts as $post ) {
                   $custom_image = get_post_custom_values('featured_image', $post->ID);
                   $image = $custom_image[0] ? $custom_image[0] : get_bloginfo("template_directory")."/images/no-featured-image.jpg";
                   printf('<li><a href="%s"title="%s"><img src="%s" alt="%s" /></a></li>', get_permalink($post->ID), $post->post_title, $image, $post->post_title);
                }
                ?>

    I would like to use the same framework for my category archives template, so that the slider will output the featured posts from whichever category archive I'm in.
    In other words, if i'm in the archive for category id 7, I'll see the recent posts for that category in the slider. If I'm in cat. 11, I'll see the recent posts for 11, etc.

    Of course, I don't know all the ids for future categories. I just want whatever category archive is opened to display the recent posts for that post in the slider.

    Can anyone point me in the right direction to specify "whatever category is the current category"???

    Many thanks,

    Jimmy

  2. vtxyzzy
    Member
    Posted 4 years ago #

    If you google 'wordpress get current category', you will find several suggestions.

  3. Digital Raindrops
    Member
    Posted 4 years ago #

    Wow 50 post titles and images in the slider, does it not slow the page down??

    This should work in the archive loop, try a variant of this from Codex Multiple Loop

    Run the loop once for the slider and then for the posts, no need to find that category as the dataset is already filtered.

    The problem there might be is the number of posts returned and page navigation, so you may need a custom query to return the last xx posts to the slider.

    Multiple Loops Example 1

    In order to loop through the same query a second time, call rewind_posts(). This will reset the loop counter and allow you to do another loop.

    <?php rewind_posts(); ?>
    
    <?php while (have_posts()) : the_post(); ?>
       <!-- Do stuff... -->
    <?php endwhile; ?>

    Loop once to load the slider return and once to load the page posts.

    HTH

    David

  4. chinmoy29
    Member
    Posted 4 years ago #

    $catName = single_title('',false);
    $catID = get_cat_ID($catName);
    
    echo $catID;
  5. Digital Raindrops
    Member
    Posted 4 years ago #

    Read the topic again, not sure about the question, the post says about a post slider, so is this loading a slider with a list underneath, the archive page already returns a pre-filtered dataset based on selection, so is it not just.

    <?php while (have_posts()) : the_post();
      $custom_image = get_post_custom_values('featured_image', $post->ID);
      $image = $custom_image[0] ? $custom_image[0] : get_bloginfo("template_directory")."/images/no-featured-image.jpg";
      printf('<li><a href="%s"title="%s"><img src="%s" alt="%s" /></a></li>', get_permalink($post->ID), $post->post_title, $image, $post->post_title);
    endwhile; ?> 
    
    <?php rewind_posts(); ?>
    
    <?php while (have_posts()) : the_post(); ?>
       <!-- Do stuff... Output Excerpt() or something -->
    <?php endwhile; ?>

    Bit confused??

    David

  6. Yukon Cornelius
    Member
    Posted 4 years ago #

    Thanks for the advice guys!

    David, I currently have the above code in my main index page outside of the loop. The loop then spits out the post excerpts below, as you'd expect. On the main index it works fine, but of course, when I put it in the archives.php, the content in the slider determined by the category specified, rather than pulling the 5 (sorry for the typo previously) most recent posts in whatever archive.

    I tried putting it inside the archives.php loop, as you suggested, but it is not pulling the posts without the top line of code.

    I'm guessing there's no way to replace 'category=8' with category=x, where x = the category or archive we are currently in???

    I appreciate your help.

    Jimmy

  7. chinmoy29
    Member
    Posted 4 years ago #

    $catName = single_cat_title('',false);
    $catID = get_cat_ID($catName);
    $featured_posts = get_posts('numberposts=50&category=$catID');

    Try this once.

  8. Yukon Cornelius
    Member
    Posted 4 years ago #

    Hi,

    After a lot of googling, I figured out the magic i need is '$cat' So for anyone interested, just replace 'category=8' with 'category=$cat'

    David: Peace and respect. I'm sure the answer was in your brain, I just had a little trouble explaining the question.

    J

  9. Yukon Cornelius
    Member
    Posted 4 years ago #

    hey Chimnoy,

    forgot to refresh. appreciate your help!

    j

  10. chinmoy29
    Member
    Posted 4 years ago #

    you're welcome.:)

    plz RESOLVED it

Topic Closed

This topic has been closed to new replies.

About this Topic