Tuukka Virtaperko
Member
Posted 1 year ago #
I'm trying to list all articles of current category in the sidebar of single.php. Also the currently viewed article is higlighted in the list. The problem is that this code lists -all- articles instead of the articles that are in the same category as the article that is currently shown.
<ul>
<?php foreach((get_the_category()) as $category) { ?>
<?php $catVal = $category->cat_ID; }
$IDOutsideLoop = $post->ID;
global $post;
$myposts = get_posts('category=.$catVal&showposts=999&order=ASC');
foreach ($myposts as $post) { ?>
<li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
<a>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php }; ?>
</ul>
This tag may be used outside The Loop by passing a post id as the parameter.
http://codex.wordpress.org/Function_Reference/get_the_category
try:
<?php foreach((get_the_category($post->ID)) as $category) { ?>
Tuukka Virtaperko
Member
Posted 1 year ago #
Thank you, alchymyth. I replaced <?php foreach((get_the_category()) as $category) { ?> with that code. The code still works, but the behaviour of the menu did not change in any way.
i missed this one as well:
if you are using single quotes with the parameters, you need to use a different way to include variables;
instead:
$myposts = get_posts('category=.$catVal&showposts=999&order=ASC');
try:
$myposts = get_posts('category='.$catVal.'&numberposts=999&order=ASC');
Tuukka Virtaperko
Member
Posted 1 year ago #
Thank you! That did the trick. I did suspect that that line is at fault, but had no idea what to do with it. :)
bozo83
Member
Posted 10 months ago #
The code above is slightly wrong. Here's a cleaner version of it:
<ul>
<?php foreach((get_the_category($post->ID)) as $category) { ?>
<?php $catVal = $category->cat_ID; }
$IDOutsideLoop = $post->ID;
global $post;
$myposts = get_posts('category='.$catVal.'&numberposts=999&order=ASC');
foreach ($myposts as $post) { ?>
<li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
<a href="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php }; ?>
</ul>
Does anyone have any idea how I can make this link list look the same as the normal sidebar menu?! The one above is unformatted, so it looks different to the menu below it! Any one?! Cheers