• Resolved travellers

    (@travellers)


    Lots of widgets about for categories, but I can’t find one that suits my wants – I’m looking for a sidebar widget that acts like a ‘similar posts’ widget, i.e. displays a list of all posts in the CURRENT category only. I tried a few widgets that suggested they may help, but in each of them it seemed I can only edit which category to show up in the widget no matter where its displayed, rather than display all posts in category ‘thingies’ when viewing a ‘thingies’ post, and at the same time view all posts in ‘wotsits’ when the current page is displaying a post from the ‘wotsits’ category.
    Is there anything that can help?

Viewing 6 replies - 1 through 6 (of 6 total)
  • displays a list of all posts in the CURRENT category only

    Assuming you mean all posts in the category or categories contained on the single post being viewed

    <?php
    if ( is_single() ) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
            'post__not_in' => array($post->ID),
            'posts_per_page'=>5,
            'caller_get_posts'=>1
          );
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo '<h2>More Posts from '. $category->category_description . '</h2>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
    Thread Starter travellers

    (@travellers)

    Many thanks for that, works great. Would be nice to capture that in a genuine widget though rather than hardcode it into the sidebar, because that would allow me to place it neatly around other widgets used (and of course I’ll have to remember to put it back following WP upgrades!).

    Consider downloading and installing Otto’s PHP Code Widget, then put the code in one of those widgets.

    Thread Starter travellers

    (@travellers)

    Shoulda realised there would be a plugin for creating a widget from raw php – d’oh. Thanks for pointing that out, I’ll wrap it in there and hey presto, a ‘current category sidebar widget’ :o)

    Also believe the Exec-PHP plugin supports PHP in a text widget. Marking resolved.

    Thread Starter travellers

    (@travellers)

    Have encountered a problem with this extremely helpful chunk of php – it doesn’t abbreviate itself if it gets too ‘chunky’. If a post title is too long for the space I have available in my sidebar, it currently wraps to the next line, but I’d prefer it that the widget only displayed the first 30 characters of the title instead.
    I was also playing with a snippet of code that I’d hoped would highlight the entry in that list of the post I’m currently viewing, but I can’t make it work. Infuriatingly, it looks like it SHOULD be easy!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘CURRENT Category widget?’ is closed to new replies.