• Resolved mathieus

    (@mathieus)


    Hi all,

    so i have the following code:

    <?php
        // get all the categories from the database
        $cats = get_categories(); 
    
            // loop through the categries
            foreach ($cats as $cat) {
                // setup the cateogory ID
                $cat_id= $cat->term_id;
                // Make a header for the cateogry
                echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
                // create a custom wordpress query
    query_posts("cat=$cat_id"); ?>
       <?php if (in_category($cat_id)) { ?>
    <?php  if (have_posts()) : while (have_posts()) : the_post();     ?>
                    <?php // create our link now that the post is setup ?>
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a><br>
              <?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
        <?php } else { ?>
    <?php } ?>
      <?php } // done the foreach statement ?>

    this code makes my category post show when i’m on that current category page. I wanted to add the following thing in the have posts loop (it should make Yes appear under EACH the post title on single post 84, and no everywhere else… :

    <?php if (is_single('84')) {  ?>
    Yes
    <?php } else { ?>
    no
    <?php } ?>

    But: is_single seems not to be working inside the loop. when i put it outside the loop it works in combination with a wp_reset_query prior to the code, but i really need it inside the loop.

    I dont know if this is proper code, but i’m learning as im doing it actually, so be easy on me ??

    thanks!

    edit:
    a screenshot perhaps!
    SCREENSHOT

Viewing 4 replies - 1 through 4 (of 4 total)
  • try instead:

    <?php if ( $post->ID == 84 ) { ?>

    or:

    <?php if ( get_the_ID() == 84 ) { ?>

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

    Thread Starter mathieus

    (@mathieus)

    Hi Alchymyth.

    yeah this seems to work. but not my whole script is doing what i thought it would.
    if i do this:
    <?php if ( $post->ID ) { ?>

    and i echo $post->ID, it echo’s the ID numbers of the single posts. I would like it to echo the current post i’m viewing at the moment, outside of that loop.. (for instance, i’m at p=84, all posts in the menu that dont equal the single post should say No, the one post that does equal the single post i’m on says yes…

    am i making some sense?

    I would like it to echo the current post i’m viewing at the moment, outside of that loop..

    can you post the full code of the template?

    are you using more than one loop?

    Thread Starter mathieus

    (@mathieus)

    Just made it work ??
    I did the following:

    made this outside the loop:
    <?php $test = get_the_ID(); ?>

    then called it inside:
    <?php if ( $post->ID == $test ) { ?>

    works perfectly.

    thanks for the tip on the post->ID!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘listing categories and posts of current category: is_single problem’ is closed to new replies.