• Resolved Tuukka Virtaperko

    (@complex-buttons)


    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>
Viewing 5 replies - 1 through 5 (of 5 total)
  • This tag may be used outside The Loop by passing a post id as the parameter.

    https://codex.www.ads-software.com/Function_Reference/get_the_category

    try:
    <?php foreach((get_the_category($post->ID)) as $category) { ?>

    Thread Starter Tuukka Virtaperko

    (@complex-buttons)

    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');

    Thread Starter Tuukka Virtaperko

    (@complex-buttons)

    Thank you! That did the trick. I did suspect that that line is at fault, but had no idea what to do with it. ??

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘single.php: how to list all articles of current category in sidebar’ is closed to new replies.