Support » Fixing WordPress » Categories and sidebar

  • Resolved hj

    (@hj)


    Hi, I want to do something quite simple – I want to display different categories in the sidebar according to which page I’m on.

    For example, on the Expeditions page I want to display only post titles from the expeditions category. On the Explorations page I want to display only post titles from the explorations category.

    This doesn’t strike me as rocket science, but I’m going round in circles trying to find a plugin to do it.

    Note that I only want to display the post titles in a category…not part of the story, too. Just the title which when clicked goes to the full story.

    Thanks.

Viewing 15 replies - 1 through 15 (of 17 total)
  • <?php
    // on a page, get category  that has slug equal
    // to the page slug, display posts in that category
    if ( is_page() ) {
      $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
      $category = get_term_by('slug',$page_name, 'category');
      if ($category) {
        $args=array(
          'category__in' => array($category->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts';
          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;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Hi Michael…

    I’m afraid I’m not a coder….what is that? and where would I put it?

    Is there a widget or plugin I can use?

    regards

    Here two possibilities:

    1. Put the code in your theme’s sidebar.php. Review Stepping Into Templates and
    Template Hierarchy.
    2. Consider downloading and installing Otto’s PHP Code Widget. Then put the code in one of those widgets

    OK, thanks for the tips 🙂

    Guess there’s no widget that does this directly, then? OK, will have a go…although I’m rubbish with code!

    cheers

    Well the query posts widget might possibly help but not sure about configuring it to display based on page name.

    http://wordpress.org/extend/plugins/query-posts/

    I notice in your code you have the commnets “on a page, get category that has slug equal to the page slug, display posts in that category”

    I’m wanting to show the post titles only, and in the sidebar.

    Basically my site is a load of static pages with dynamic content listed (by category) in the sidebar.

    At least, that’s what I want…!

    Yes that displays just the posts titles.

    Post content would be displayed using the template tag, the_content().

    Note that unless you are using a plugin, Pages are not categorized.

    OK.

    I’m using a theme called Snowblind and its basic sidebar.php code is as follows:

    <!-- begin sidebar -->
    		<div id="sidebar">
    				<?php 	/* Widgetized sidebar, if you have the plugin installed. */
    					if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(__('First Sidebar', "snowblind")) ) : ?>
    					<div class="side-widget">
                                <h2><?php _e('Categories', "snowblind"); ?></h2>
                                <ul>
    							<?php wp_list_categories('title_li=&depth=1'); ?>
                                </ul>
    					</div>
                        <div class="side-widget">
                               <h2><?php _e('Calendar', "snowblind"); ?></h2>
                                    <?php get_calendar(); ?>
                        </div>
    
                        <div class="side-widget">
                        	<h2><?php _e('Popular Posts', "snowblind"); ?></h2>
                            <ul><?php echo popularPosts(5); ?></ul>
                        </div>
          			<?php endif; ?>
    		</div>
    <!-- end sidebar -->

    Do I replace a load of that code with your code? Or do I add your code in somewhere?

    I’ve looked at the Pages page in admin but can’t see anywhere that you can select a specific template or sidebar file foro a particular page.

    If you are using Widgets, do the widget thing suggested above.

    Otto’s PHP Code Widget you mean?…or Query Post widget?

    Otto’s PHP Code Widget

    I put your code into Otto’s widget. Invoking it in the sidebar got rid of the basic stuff that was there before (calendar, categories etc) but didn’t display anything?

    I think I’m matching pages and categories…..for instance I’ve got a page called Expeditions, and also a category called Expeditions whose slug is expeditions

    Is that right?

    That’s how I’m set up, anyway, but nothing showing yet…

    After this line:

    if ( is_page() ) {

    add

    global $posts;

    That did it! Excellent!

    Two final queries, then…

    1. For this to work the category ‘slug’ must match the name of the page?…in all instances?

    2. On my Home page, which actually uses a static page called The Mulu Caves Project, I want to display a category called General News. We’re in trouble here, right?..!!

    after

    $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
    if ($page_name == 'the-mulu-caves-project') {
    $page_name = 'general-news';
    }

    Or something like that…

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Categories and sidebar’ is closed to new replies.