• Is there any way of using $id or $post_id or something other than <?php the_ID(); ?> to get the post ID? I’m using DotComments (not a fan of WP’s comment setup), and the comment counter won’t work because the code involves one php module inside another:


    <?echo
    commentCount('<?php the_ID(); ?>');?>

    Thanks in advance for any help you can give.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Just store <?php the_ID(); ?> in your own variable.

    <?php
    $my_ID = the_ID();

    echo commentCount('$my_ID');
    ?>

    Thread Starter fuddes

    (@fuddes)

    Afraid not. It works if I substitute the actual post number, $my_ID = 142; but not with the_ID() in there. So I know it’s not a typing or coding error on my part. It seems like there just has to be another way.

    the_ID() echoes instead of returning a value, right? That is what gives an error.

    I haven’t tried it, but try
    $id = $post->ID;
    and let us know.

    Thread Starter fuddes

    (@fuddes)

    It works! Thank you so much. I’m sure many other DotComments users will find this thread very helpful.

    Slightly shorter:

    commentCount($post->ID); ?>

    Unfortunately ($post->ID) doesn’t work when you have embedded PHP inside a WordPress Page (I use the RunPHP plugin).

    So I delved into the source code for the function the_ID(), and it simply echoes the global variable called “$id”. This is why you cannot use the return value from the_ID() and put it into a variable, because the_ID() does not return any value (see PHP ‘echo’ function).

    So in my Page code when I need to get the page id, for example in listing sub-pages of my page, this is how I did it:

      <?php
      global $id; // the page id
      $cmd = ‘title_li=&sort_column=menu_order&child_of=’ . $id;
      wp_list_pages($cmd);
      ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘An alternative to <?php the_ID(); ?>’ is closed to new replies.