WordPress.org

Forums

[resolved] Widget : Current category posts list... And the lost cat variable ! (10 posts)

  1. Gesatzs
    Member
    Posted 5 years ago #

    Hi all !
    I'm looking to create a sidebar widget to list all posts from the same category as current post. But let me explain the project first in few lines...

    Basically, I'm using WP to create a basic 5 section website, not a blog.

    The idea is to create a page for each category, each category representing a main section of the website. I affected a variable to each page (extra field), according to each category ID. So each page created is an entry point for the category to which it's associated thanks to the variable (each entry page hassame "ID" as its category).

    So in one static page, I get the page text, and below that all post excerpt from correspondent category. That is working. But then... In the sidebar, I want to show a link to all posts from this same category as current page... Am I being clear ? :)

    Ideally I trying to create widget... But ain't working. So far I'm using this code in the sidebar.php file :

    <?php
    	$categoryvariable=$cat; // assign the variable as current category
            $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
            query_posts($query); // run the query
    
                global $post;
                $myposts = get_posts('numberposts=&offset=0&category='.$categoryvariable);
                foreach($myposts as $post) :
                ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endforeach; ?>

    I'ts kind of working, but the variable is lost when viewing a post page. I guess the variable is not associated to the post, or I'm missing something... ???

    As I said, I'm also trying to get that into a widget, and I guess it's the same problem of missing variable... It's showing all posts by default...

    Maybe this widget/snippet exists already, or maybe you have a way to help me getting that working !!!

    Thanks for any hint, I'd greatly appreciate !
    Gesatzs

  2. MichaelH
    Member
    Posted 5 years ago #

    For a sidebar, list all posts in currently viewed category archive

    <?php
    if (is_category( )) {
      $cat = get_query_var('cat');
      $category = get_term_by('id',$cat, '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 for '.$category->name;
          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. Gesatzs
    Member
    Posted 5 years ago #

    Thanks.

    I'll don't yet get it, but it's the same problem with you code : It does no give anything when viewing a post page. is it normal ? I'll go through the code and try to understand the differences anyway thanks

  4. MichaelH
    Member
    Posted 5 years ago #

    when viewing a post page

    Sorry-the above code works when viewing a category archive. Are you wanting that to work when viewing a single post?

  5. Gesatzs
    Member
    Posted 5 years ago #

    yes. The sidebar should be consistent from the category-entry page, to the post page... and I'm stuck on that :)

  6. MichaelH
    Member
    Posted 5 years ago #

    So let me confirm that:

    When viewing a single post (after someone clicks on a post title) you want other posts to display in the sidebar where those 'other posts' belong to the same categories that are assigned to the single post being viewed.

    Is that correct?

  7. Gesatzs
    Member
    Posted 5 years ago #

    correct !

  8. MichaelH
    Member
    Posted 5 years ago #

    In a sidebar, if single post, for each category of that post, display 5 posts in that category, excluding the single post.

    <?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())
    ?>
  9. Gesatzs
    Member
    Posted 5 years ago #

    Looks great ! Let me work on that and try to understand it all :)
    THANKS A LOT

  10. Gesatzs
    Member
    Posted 5 years ago #

    Here is what I wanted, thanks to you !
    Not tested in all situations, but so far so good!
    Note I'm using a cat variable in each page, this variable the same as the relative category id.

    I guess It could use optimisation but it's a good start for me... :)
    THANKS AGAIN

    <?php  //// CREATING NEW WIDGET (template)
    function widget_myuniquewidget($args) {
        extract($args);
    ?>
            <?php echo $before_widget; ?>
                <?php echo $before_title
                    . $after_title; ?>
       <?php
    // FOR POST PAGE ONLY : SHOWS LIST OF ALL BUT CURRENT POST - POSTS WITHIN SAME CAT -->
    	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 '<li><h4>IF_SINGLE LOOP<br /> '. $category->name . '</h4></li><ul>';
    				while ($my_query->have_posts()) : $my_query->the_post(); ?>
    				  <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    				 <?php
    				endwhile;
    				echo '</ul>';
    			  } //if ($my_query)
    			} //foreach ($categories
    		  } //if ($categories)
    		  wp_reset_query();  // Restore global post data stomped by the_post().
    		} //if (is_single())
      // END //  FOR POST PAGE ONLY : SHOWS LIST OF ALL BUT CURRENT POST - POSTS WITHIN SAME CAT -->
      // START //   FOR ALL CATEGORY PAGE : SHOWS LIST OF ALL POSTS WITHIN SAME CURRENT CAT  -->
    if ( is_category() )  {
      $cat = get_query_var('cat');
      $category = get_term_by('id',$cat, '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 '<li><h4>IF_CATEGORY LOOP<br /> '.$category->name.'</h4></li><ul>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
           <?php
          endwhile;
    	    echo '</ul>';
        } //  fin  $categories
      wp_reset_query();  // Restore global post data stomped by the_post().
      }// Fin if is_category
    }
    if ( is_page() ) {
    
    		// PERSONAL SIDEBAR : CURRENTCATEGORY POST LIST
    		 //my_categoryposttitleloop($catpost);
    
    		$mykey_values = get_post_custom_values('cat');
    		  foreach ( $mykey_values as $key => $value ) {
    		   $catpost = $value;
    		   $category = get_term_by('id',$catpost, 'category');
    
    		  }
    
    		query_posts('cat='.$value );
    		    echo '<li><h4>IF_PAGE LOOP<br /> '.$category->name.'</h4></li><ul>';
    			 while (have_posts()) : the_post(); ?>
    			 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    			 <?php endwhile;
    
    		 ?>
    		 <?php
    
    		////////////////
    
    			 }
    
    ?>
            <?php echo $after_widget; ?>
    <?php
    }
    register_sidebar_widget('My Unique Widget','widget_myuniquewidget');
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic