• Resolved spewf

    (@spewf)


    I am trying to have a custom field showed on my recent comment code. Any ideas?

    <?php
    
    $image = get_post_meta($post->ID, 'photo', true);
    
    $comments = get_comments('number=30');
    
      foreach($comments as $comm) :
    
      $url = '<a href="'. get_permalink($comm->comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' . $comm->comment_author . '</a>';
    ?>
    <li>
    <?php echo get_avatar($comm->comment_author_email, 30); ?>
    <strong><?php echo $url; ?></strong>
    <p><?php echo $comm->comment_content; ?></p>
    
    <img src='<?php echo $image; ?>' height='100' width='180' style='border:3px double white;' />
    </li>
    <?php
      endforeach;
    ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • you can write a custom query:

    SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = '$comm->ID'

    Thread Starter spewf

    (@spewf)

    Would I do this instead of the $comments = get_comments ?

    I don’t understand =p
    the code that you gave in the first post works fine no?
    but it will always display the same image linked by the meta …

    Thread Starter spewf

    (@spewf)

    The code works but the image will not. I want it to show the custom field that is attached to the post.

    (I think ) Rahul Sonar was suggesting you to make an sql request to find the ID of the post, and, after, to use this ID to call your meta ^^

    Thread Starter spewf

    (@spewf)

    sql = "SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = '$comm->ID'";
    $image = get_post_meta($post->ID, 'photo', true);
    
    $comments = get_comments('number=30');

    Causes errors. Hmm, I am not seeing something here.

    I don’t think you can do sql request like that lol [maybe it works in wordpress, but this is the first time I see that thing]
    I’m using pdo to do my request, but try to look here : wpdb

    try to use the ID that you will find in your call of the photo instead $post->id

    I may be totally wrong about the solution… maybe Rahul Sonar was thinking about something else

    Moderator keesiemeijer

    (@keesiemeijer)

    Ive tried this somewhere once

    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, comment_content
    		FROM $wpdb->comments
    		LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
    		WHERE (post_status = 'publish' OR post_status = 'static') AND comment_approved= '1'
    		ORDER BY comment_date_gmt DESC
    		LIMIT 30";
    $comments = $wpdb->get_results($sql);

    you can use this in stead of $comments = get_comments
    the array $comments is populated with these values wich you can use in your function:

    [ID] => 11 //this is the post ID
    [post_title] => dolor
    [post_password] =>
    [comment_ID] => 12
    [comment_post_ID] => 11
    [comment_author] => admin
    [comment_date_gmt] => 2010-07-23 09:43:39
    [comment_approved] => 1
    [comment_type] =>
    [comment_content] => bla bla bla ...

    Moderator keesiemeijer

    (@keesiemeijer)

    I see now that $comments = get_comments('number=30'); also will give you the post ID [comment_post_ID] => 11
    Put this inside the foreach statement of your function:

    $image = get_post_meta($comm->comment_post_ID, 'photo', true);

    Thread Starter spewf

    (@spewf)

    Wow keesiemeijer, that worked pefectly. I just did your last idea and it worked. Here is the final code I have:

    <?php
     $comments = get_comments('number=30');
    
     foreach($comments as $comm) :
      $image = get_post_meta($comm->comment_post_ID, 'photo', true);
      $url = '<a href="'. get_permalink($comm->comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' . $comm->comment_author . '</a>';
    ?>
    <li>
    <?php echo get_avatar($comm->comment_author_email, 30); ?>
    <strong><?php echo $url; ?></strong>
    <p><?php echo $comm->comment_content; ?></p>
    
    <img src='<?php echo $image; ?>' height='100' width='180' style='border:3px double white;' />
    </li>
    <?php
      endforeach;
    ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Retrieving Post ID For Comments?’ is closed to new replies.