• I’ve used <?php query_posts('showposts=8&amp;cat='.get_query_var('cat')); ?> to list posts outside the loop and it worked in archive.php, but now in single.php it doesn’t work… someone know how to make it work?

Viewing 9 replies - 1 through 9 (of 9 total)
  • What is it that you’re trying to do, exactly?

    A little clarification might help us give you an answer.

    Thread Starter Lucky-1

    (@lucky-1)

    I want to list some posts outside the loop in single post page, i’m using query_posts to that, and i want the posts to be the same category, in archives pages the code i’ve used works, but for single posts pages it doesnt work..
    maybe the cat='.get_query_var('cat') is wrong

    Don’t know where I first saw (or used this) but, it will display posts belonging to the categories of the current post without duplicating the current post

    <?php
    global $post;
    $cat_ID=array();
    $categories = get_the_category(); //get all categories for this post
      foreach($categories as $category) {
        array_push($cat_ID,$category->cat_ID);
      }
      $args = array(
      'orderby' => 'date',
      'order' => 'DESC',
    	'post_type' => 'post',
    	'numberposts' => 8,
    	'post__not_in' => array($post->ID),
    	'category__in' => $cat_ID
      ); // post__not_in will exclude the post we are displaying
        $cat_posts = get_posts($args);
        $out='';
          foreach($cat_posts as $cat_post) {
              $out .= '<li>';
              $out .=  '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></li>';
          }
        $out = '<ul class="cat_post">' . $out . '</ul>';
        echo $out;
    ?>

    Thread Starter Lucky-1

    (@lucky-1)

    im using this

    <?php
    		query_posts('showposts=8&amp;cat='.get_query_var('cat')); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="postt">
    			<a target="_blank" href="/link/out.php?link=gal&amp;s=80&amp;url=<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'mandigo'), the_title('', '', false)); ?>">
    <?php global $more; $more = FALSE; ?>
    <?php the_content(''); ?>
    <?php $more = TRUE; ?><b><?php the_title(); ?></b></a></div>
    <?php endwhile; endif; ?>

    but it dont get the category id correctly in single post page :/

    Well I don’t believe get_query_var('cat') will work in single.php.

    Thread Starter Lucky-1

    (@lucky-1)

    dont have anything simple like and that works?

    Not really, it’s more complex with a single post because you have to get the categories (there could be more than one) for the post.

    Thread Starter Lucky-1

    (@lucky-1)

    getting just the first category will work

    $categories = get_the_category(); //get all categories for this post
    echo ‘first category is ‘ . $categories[0]->cat_ID;

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to get cat ID in query_posts in single.php’ is closed to new replies.