WordPress.org

Ready to get started?Download WordPress

Forums

An alternative to <?php the_ID(); ?> (7 posts)

  1. fuddes
    Member
    Posted 9 years ago #

    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.

  2. Avenir
    Member
    Posted 9 years ago #

    Just store <?php the_ID(); ?> in your own variable.

    <?php
    $my_ID = the_ID();

    echo commentCount('$my_ID');
    ?>

  3. fuddes
    Member
    Posted 9 years ago #

    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.

  4. lyklev
    Member
    Posted 9 years ago #

    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.

  5. fuddes
    Member
    Posted 9 years ago #

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

  6. samsm
    Member
    Posted 9 years ago #

    Slightly shorter:

    commentCount($post->ID); ?>

  7. shazron
    Member
    Posted 9 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.