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 !