Forums

add code for a specified page (4 posts)

  1. Dimitris The Greek Lover
    Member
    Posted 2 years ago #

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

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    <?php if is_page('4567'): ?>

    before that code, and then

    <?php endif; ?>

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

  3. David Gwyer
    Member
    Posted 2 years ago #

    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:

    http://codex.wordpress.org/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:

    http://wordpress.tv/2011/01/29/mark-jaquith-theme-plugin-security/

  4. Dimitris The Greek Lover
    Member
    Posted 2 years ago #

    thanx boys...
    i will give it a shot right now

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags