• I just want a simple thing…

    I want to add this code for “show last comments”

    <?php
          $number=5; // number of recent comments desired
          $post_id = get_the_ID();
          $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID=$post_id AND comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
       ?>
    
       <ul id="recentcomments">
          <?php if ($comments) :
             echo '<h2>' . sizeof($comments) . ' Recent Comments</h2>';
             foreach ( (array) $comments as $comment) :
                echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
             endforeach;
          endif;?>
       </ul> <!-- end of comments -->
    </div>

    but I want this to take effect only in a specified page… for example

    mysite.com/?page_id=4567

    Can anybody help me??

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php if is_page('4567'): ?>

    before that code, and then

    <?php endif; ?>

    after your code…then put all of it onto page.php

    Update: Oops yes Rev. Voodoo is right, but the same holds if it was a post as shown below. ??

    I think you need to wrap it with is_single() function. So you would have something like:

    <?php
    if ( is_single('4567') ) {
        // Add you code here..
    }
    ?>

    You can see more details here:

    https://codex.www.ads-software.com/Conditional_Tags#A_Single_Post_Page

    By the way, as a heads up. If you are doing any calls to the database directly then I would strongly recommend you follow best practices and use wrapper functions built-in to WordPress that handle all the sanitization for you.

    You can see an excellent video on this from Mark Jaquith:

    Thread Starter Dimitris Arkolakis

    (@meetsos)

    thanx boys…
    i will give it a shot right now

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add code for a specified page’ is closed to new replies.