WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Categories and sidebar (18 posts)

  1. hj
    Member
    Posted 4 years ago #

    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.

  2. MichaelH
    Member
    Posted 4 years ago #

    <?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().
    ?>
  3. hj
    Member
    Posted 4 years ago #

    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

  4. MichaelH
    Member
    Posted 4 years ago #

    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

  5. hj
    Member
    Posted 4 years ago #

    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

  6. MichaelH
    Member
    Posted 4 years ago #

    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/

  7. hj
    Member
    Posted 4 years ago #

    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...!

  8. MichaelH
    Member
    Posted 4 years ago #

    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.

  9. hj
    Member
    Posted 4 years ago #

    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.

  10. MichaelH
    Member
    Posted 4 years ago #

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

  11. hj
    Member
    Posted 4 years ago #

    Otto's PHP Code Widget you mean?...or Query Post widget?

  12. MichaelH
    Member
    Posted 4 years ago #

    Otto's PHP Code Widget

  13. hj
    Member
    Posted 4 years ago #

    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...

  14. MichaelH
    Member
    Posted 4 years ago #

    After this line:

    if ( is_page() ) {

    add

    global $posts;
  15. hj
    Member
    Posted 4 years ago #

    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?..!!

  16. MichaelH
    Member
    Posted 4 years ago #

    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...

  17. hj
    Member
    Posted 4 years ago #

    Thanks Michael, you've been a great help. Appreciated.

    I must admit I'm really surprised that this kind of functionality isn't built into WP (or widegts/plugins). Being able to call different categories or different bits of information into a "sidebar' is a fundamental part of most CMS or standalone blog scripts. I used pMachine quite a bit and you can have what you like where you like with impunity! (shame I was useless at styling it all...)

    So despite the variety of sidebar widgetry available, I feel there's still much fundamental stuff missing from WP. If it wants to be a true publishing platform, that is, not just a blog.

    Many thanks again.

    regards
    Hugh

  18. MichaelH
    Member
    Posted 4 years ago #

    Marking resolved.

    If that version, 2.7.1 is correct, upgrading is recommended.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.