Skip to content

Kommentare und Fediverse-Reaktionen in WordPress zählen

Ich wollte für diese WordPress-Seite einfach eine Zahl ausgeben haben, die Blogkommentare und Reaktionen aus dem Fediverse zusammenfasst. So geht's: /** * Gesamte Interaktionen: Kommentare + Fediverse-Reaktionen * * @param int $post_id Post-ID (0 = aktueller Post) * @return int Gesamtzahl */ function get_total_interactions( $post_id = 0 ) { if ( ! $post_id ) {...

Too much information
Jun 12, 20261 min read

ch woll­te für die­se Word­Press-Sei­te ein­fach eine Zahl aus­ge­ben haben, die Blog­kom­men­ta­re und Reak­tio­nen aus dem Fedi­ver­se zusam­men­fasst. So geht’s:

/** * Gesamte Interaktionen: Kommentare + Fediverse-Reaktionen * * @param int $post_id Post-ID (0 = aktueller Post) * @return int Gesamtzahl */ function get_total_interactions( $post_id = 0 ) { if ( ! $post_id ) { $post_id = get_the_ID(); } if ( ! $post_id ) { return 0; } // 1. Alle Kommentare (inkl. Fediverse-Replies) $comment_count = (int) get_comments_number( $post_id ); // 2. Fediverse-Reaktionen (Likes, Reposts etc.) – über das ActivityPub-Plugin $reaction_count = 0; if ( class_exists( 'Activitypub\Comment' ) ) { $comment_types = Activitypub\Comment::get_comment_types(); foreach ( array_keys( $comment_types ) as $type ) { // Zählt nur Top-Level Reaktionen (keine Replies darauf) $reactions = get_comments( array( 'post_id' => $post_id, 'type' => $type, 'status' => 'approve', 'parent' => 0, // nur direkte Reaktionen 'count' => true, // nur zählen, keine Objekte laden ) ); $reaction_count += (int) $reactions; } } return $comment_count + $reaction_count;

}

Und zum Ein­bau­en in z.B. eine Sei­te:

<?php $total = get_total_interactions(); if ( $total > 0 ) : ?> <div class="total-interactions"> <strong><?php echo number_format_i18n( $total ); ?></strong> Interaktionen </div> <?php endif; ?>

Did you enjoy this article?

Recommend it — Standard Reader surfaces well-loved writing to more readers across the network.

Across the AtmosphereDiscussions