web-dev-qa-db-fra.com

Afficher les commentaires des auteurs sur la page de profil

J'essaie d'afficher les commentaires des auteurs sur leur page de profil (author.php), mais les deux codes que j'ai essayés semblent afficher les commentaires de chacun. De plus, le second code est supposé relier le commentaire spécifique, mais au lieu de cela, il ne fait rien. De plus, l'ID de commentaires est déjà ajouté à la sortie des commentaires et il est bien imprimé. Toute aide est grandement appréciée.

// Method 1
<ul class="authpcom">
<?php
$author_email = get_the_author_meta( 'user_email' ); 

$args = array(
    'author_email' => $author_email
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo('<a href=" ' . get_permalink($comment->post_ID) . ' " rel="external nofollow" title=" ' . $title . ' ">' .$title . '</a><br />' . $comment->comment_date . '<br /><li>' . $comment->comment_content . '</li>');
endforeach;
?>
</ul>


// Method 2
<?php   $comments = get_comments(); ?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li><p><strong><?php
        $title = get_the_title($comment->comment_post_ID);
        echo get_avatar( $comment, '45' );
echo strip_tags($comment->comment_author); ?></strong>&nbsp;commented on <a href="<?php echo get_permalink($comment->comment_post_ID); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)</p></li>
<?php }  ?>
</ul>

Utilisé sur les divs de sortie de commentaires -

$comment->comment_ID

objet (WP_User) # 345 (7) {["données"] => objet (stdClass) # 344 (10) {["ID"] => chaîne (1) "2" ["utilisateur_login"] => chaîne ( 6) "agent1" ["user_pass"] => chaîne (34) "$ P $ BXUSPFSBfmyIrjZ2YUnbIs1GwjkdH50" ["nom_utilisateur"] => chaîne (6) "agent1" ["utilisateur_email"] => chaîne (19) "agent1 @ homekast.com "[" user_url "] => string (0)" "[" user_registered "] => string (19)" 2015-07-25 10:33:27 "[" user_activation_key "] => string (0 ) "" ["user_status"] => string (1) "0" ["display_name"] => string (9) "Jean Paul"} ["ID"] => int (2) ["caps"] = > array (1) {["agent"] => bool (true)} ["cap_key"] => string (15) "tr_capabilities" ["roles"] => array (1) {[0] => chaîne (5) "agent"} ["allcaps"] => array (2) {["read"] => bool (vrai) ["agent"] => bool (vrai)} ["filtre"] => NULL }

5
Rich

Ce que vous devez utiliser ici est la fonction WP_Comment_Query () .

Ainsi, sur la page author.php, vous pouvez facilement obtenir les informations sur l'auteur et l'ID comme suit:

// get author info
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

// set ID
$user_id = $curauth->ID;

Ensuite, nous ajoutons l'ID utilisateur dans le tableau des arguments de requête:

$args = array(
    'user_id' => $user_id, // comments by this user only
    'status' => 'approve',
    'post_status' => 'publish',
    'post_type' => 'post'
);

Et finalement, nous avons trouvé les arguments dans la wp_comment_query():

// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo '<p>' . $comment->comment_content . '</p>';
    }
} else {
    echo 'No comments found.';
}

En complément bonus , j'ai étudié le fonctionnement de la pagination avec wp_comment_query() il n'y a pas si longtemps et je propose une bonne solution ici . C'était un peu un violon-niddle pour le faire fonctionner.

MODIFIER:

Un meilleur moyen d'obtenir l'identifiant de l'auteur est simplement avec (props @Pieter):

$user_id = get_queried_object_id();
5
Christine Cooper

Première méthode, il vous manque le deuxième paramètre pour get_the_author_meta, qui est l'ID de l'auteur.

Deuxième méthode, vous utilisez des variables non définies. Vérifiez ce code, vous devriez obtenir ce que vous voulez.

// Method 1
<ul class="authpcom">
    <?php
        $queried_object = get_queried_object();
        $author_email = get_the_author_meta( 'user_email', $queried_object->ID ); 

        $args = array(
            'author_email' => $author_email
        );
        $comments = get_comments($args);
        foreach($comments as $comment) :
            echo '<a href=" ' . get_permalink( $comment->comment_post_ID ) . ' " rel="external nofollow" title=" ' . get_the_title( $comment->comment_post_ID ) . ' ">' . get_the_title( $comment->comment_post_ID ) . '</a><br />' . $comment->comment_date . '<br /><li>' . $comment->comment_content . '</li>';
        endforeach;
    ?>
</ul>


// Method 2
<?php
    $comments = get_comments();
?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li>
    <p>
        <strong>
        <?php
            echo get_avatar( $comment->comment_author_email, '45' );
            echo strip_tags($comment->comment_author);
        ?>
        </strong>
        &nbsp;commented on <a href="<?php echo get_permalink( $comment->comment_post_ID ); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo get_the_title( $comment->comment_post_ID ); ?>"> <?php echo get_the_title( $comment->comment_post_ID ); ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)
    </p>
</li>
<?php }  ?>
</ul>

EDIT: (09/05/2015)

// Method 1
<ul class="authpcom">
    <?php
        $authorID = get_queried_object_id();
        $author_email = get_the_author_meta( 'user_email', $authorID ); 

        $args = array(
            'user_id' => $authorID,
        );
        $comments = get_comments($args);
        if ( $comments ) {
            foreach($comments as $comment) {
                echo '<li><a href="' . get_permalink( $comment->comment_post_ID ) . '" rel="external nofollow" title="' . get_the_title( $comment->comment_post_ID ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a><br>' . $comment->comment_date . '<br>' . $comment->comment_content . '</li>';
            }
        } else {
            echo '<li>No Comments from this Author</li>';
        }
    ?>
</ul>
1
Abhik

Vous pouvez y parvenir en utilisant le plugin ultimate member . Il semble être capable de faire beaucoup de choses avec les profils utilisateur, mais semble également être approprié pour afficher les commentaires de l'utilisateur sur la page de profil.

Afficher les publications et les commentaires des auteurs sur les profils des utilisateurs

0
tillinberlin