WordPress Track- und Pingbacks seperat listen, ohne Plugin

Die Zeiten ändern sich.

Dieser Beitrag scheint älter als 17 Jahre zu sein – eine lange Zeit im Internet. Der Inhalt ist vielleicht veraltet.

Will man die Track- und Pingbacks im Kommentarbereich extra darstellen so kann man mit dem Plugin Trackbacklist arbeiten und dabei hilft einem die Anleitung von Peruns Weblog.
Man kann aber auch das Theme bearbeiten und dabei die Template-Datei comments.php anpassen, in dem man mit WordPress-Template-Tags arbeitet.

Im folgenden ein kleines Beispiel, wie man die Trackbacks und Pingbacks zu einem Beitrag seperat auflisten kann. Da sind der Phantasie und Abfragemechanismen keine Grenzen gesetzt. Ansehen kann man das Ganze auf dieser aktuellen Website zum Beispiel unter diesem Link. Die Track- und Pingbacks befinden sich innerhalb der Kommentare, sehen aber anders aus und enthalten keinen Text. Aus der Liste wollte ich sie nicht entfernen, da dann die Kommentare mt Bezug auf eine Kommentarnummer nicht mehr stimmen. Um die Ping- und Trackbacks komplett auszulagern, habe ich am Ende ein Code-Beispiel für eine komplette comments.php hinterlegt.
Noch mehr Tipps und vorallem Diskussion gibt es bei Robert’s Artikel „Trackbacks von Kommentaren kennen“ in den Kommentaren.

Übrigens, wer noch ein wenig Nachhilfe bei dem Begriff Trackback benötigt, der findet eine tolle Erklärung bei Michael – Was sind Trackbacks? Erklärung im Sendung-mit-der-Maus-Stil

Zuerst fragen wir ab, ob es Kommentare zu Beitrag gibt:

if ($comments)

Nun bauen wir die foreach-Schleife aus, in dem wir eine weitere Abfrage einbauen:

if ((get_comment_type() == "trackback") || (get_comment_type() == "pingback"))

Der Tag get_comment_type übergibt uns, um was für einen Kommentar es sich handelt, und in dem Fall lassen wir alle Kommentare zu, die per Trackback oder Pingback kommen. Im Ganzen sieht der Code dann folgendermaßen aus.


<?php if ($comments) : ?>
	<ol class="trackbacklist">
	<?php foreach ($comments as $comment) : if ((get_comment_type() == "trackback") || (get_comment_type() == "pingback")) : ?>

		<li id="comment-<?php comment_ID() ?>" >
			<?php comment_type('Kommentar', 'Trackback', 'Pingback'); ?><small> von </small><cite><?php comment_author_link() ?></cite>
			<br />
			<small class="commentmetadata"><?php comment_date('j. F Y') ?> um <?php comment_time('H:i') ?> <?php edit_comment_link('· bearbeiten','',''); ?></small>

			<?php comment_text() ?>
		</li>

	<?php endif; endforeach; /* end for each comment */ ?>
	</ol>
 <?php endif; ?>

Damit nun der Kommentarbereich auch wirklich nur Kommentare aus dem Textfeld übernimmt, müssen wir die Abfrage dort noch erweitern, denn noch werden ja alle Kommentare, ob Track- oder Pingback dort dargestellt. Dazu erweitert man die Schleife.
Suche nach:

<?php foreach ($comments as $comment) : ?>

und ersetze mit:

<?php foreach ($comments as $comment) : if (get_comment_type() == "comment") : ?>

Außerdem müssen wir beim Schließen der foreach-Schleife die Abfrage beenden:
Suche nach:

<?php endforeach; ?>

und ersetze mit:

<?php endif; endforeach; ?>

Die Darstellung der Kommentaranzahl wird üblicherweise mit dem Tag comments_number() erstellt, die aber alle Kommentare zählt, egal ob Track-, Pingback oder Kommentar. Dagegen hilft nur eine weiter Abfrage – WP – Trackbacks und Kommentare getrennt zählen. In diesem Artikel erkläre ich dann diese Funktion näher.

We noch mehr Tipps zum Anpassen des Kommentarbereiches möchte, der findet drei weitere Möglichkeiten im Artikel WP – Kommentarbereich anpassen. Dort erläuter ich kurz, wie man einen Counter einbaut, die Kommentare des Beitragsautors hervorhebt und wie man Gravatare zu den Kommentarautoren darstellt.

Beispiel einer kompletten comments.php mit Track- und Pingback am Ende


<?php
/*********************************************************************
comments-template mit counter, trackback, Hervorhebung des Autors 
und Trennung Kommentare zu Track- und Pingback
**********************************************************************/
?>

<div id="comments">
<?php // Do not delete these lines
	if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
		die ('Bitte diese Seite nicht direkt laden. Danke !');

				if (!empty($post->post_password)) { // if there's a password
						if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
				?>
				
				<p class="nocomments">Dieser Beitrag ist passwortgesch&#252;tzt. Bitte gebe das Passwort an um die Kommentare zu sehen.</p><p>
				
				<?php
				return;
						}
				}

		/* alternating varibale for comment background */
		$oddcomment = 'alt';
?>

<?php if ($comments) : ?>
	<h2 class="twost"><?php comments_number('0 Kommentare', '1 Kommentar', '% Kommentare' );?> zu &#8222;<?php the_title(); ?>&#8220;</h2>

	<ol class="commentlist">

	<?php $gravatar_default = "ADRESSE_ZUM_DEFAULTBILD"; ?>

	<?php $count = 0; foreach ($comments as $comment) : if (get_comment_type() == "comment") : $count++; ?>

		<li id="comment-<?php comment_ID() ?>"
			<?php global $comment;
				if ( ($comment->comment_author_email == get_the_author_email()) && ($comment->user_id != 0) ) {
						echo " class=\"autor_kommentar\"";
				} else {
						echo " class=\"" . $oddcomment . "\"";
				}
			?>>
			<p class="comment-number"><a href="#comment-<?php comment_ID() ?>"><?php echo $count; ?></a></p>

			<?php $gravatar_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . md5(get_comment_author_email()) . "&amp;default=" . urlencode($gravatar_default) . "&amp;size=30"; ?>
			<img class="gravatar" src="<?php echo $gravatar_url ?/>" title="<?php comment_author(); ?>'s Gravatar" alt="id" />
	
			<?php comment_type('Kommentar', 'Trackback', 'Pingback'); ?><small> von </small><cite><?php comment_author_link() ?></cite>
			<?php if ($comment->comment_approved == '0') : ?>
			<em>Dein Kommentar muss noch moderiert werden.</em>
			<?php endif; ?>
			<br />
			<small class="commentmetadata"><?php comment_date('j. F Y') ?> um <?php comment_time('H:i') ?> <?php edit_comment_link('&middot; bearbeiten','',''); ?></small>

			<?php comment_text() ?>
		</li>

	<?php /* Changes every other comment to a different class */  
		if ('alt' == $oddcomment) $oddcomment = '';
		else $oddcomment = 'alt';
	?>

	<?php endif; endforeach; /* end for each comment */ ?>
	</ol>

 <?php else : // this is displayed if there are no comments so far ?>

	<?php if ('open' == $post->comment_status) : ?> 
		<!-- If comments are open, but there are no comments. -->
		
	 <?php else : // comments are closed ?>

		</p><p class="nocomments">Kommentare sind gechlossen.</p>
		
	<?php endif; ?>
<?php endif; ?>

<?php if ('open' == $post->comment_status) : ?>

<h2 class="threest" id="writecomment">Kommentar schreiben</h2>

<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>Du musst <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">angemeldet</a> sein um einen Kommentar zu schreiben.</p>
<?php else : ?>

	<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
		
		<?php if ( $user_ID ) : ?>	
			<p>Angemeldet als <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Abmelden &raquo;</a></p>	
		<?php else : ?>
	
			<p>
				<input type="text" name="author" id="author" value="<?php echo $comment_author; ?/>" size="20" tabindex="1" />
				<label for="author">Name <?php if ($req) echo "<small>Pflichtfeld"; ?></label>
			</p>
			<p>
				<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?/>" size="20" tabindex="2" />
				<label for="email">eMail (bleibt versteckt) <?php if ($req) echo "<small>Pflichtfeld"; ?></label>
			</p>	
			<p>
				<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?/>" size="20" tabindex="3" />
				<label for="url">Website</label>
			</p>
			<?php if ( function_exists('math_comment_spam_protection') ) { $mcsp_info = math_comment_spam_protection();?>
			<p>
				<input type="text" name="mcspvalue" id="mcspvalue" value="" size="20" tabindex="4" />
				<label for="mcspvalue">Spamschutz: <?php echo $mcsp_info['operand1'] . ' + ' . $mcsp_info['operand2'] . ' = ?' ?> <small>Pflichtfeld</small></label>
				<input type="hidden" name="mcspinfo" value="<?php echo $mcsp_info['result']; ?/>" />
			</p>
			<?php } ?>
				
		<?php endif; ?>
			<p class="textfeld" >Textfeld &raquo; <a href="javascript:change_textbox_size(-80);" title="Textfeld verkleinern" >kleiner</a> &middot; <a href="javascript:change_textbox_size(80);" title="Textfeld vergr&ouml;&szlig;ern" >gr&ouml;&szlig;er</a></p>
			<p>
				<textarea name="comment" id="comment" cols="30" rows="5" tabindex="4"></textarea>
			</p>
			<p>
				<input name="submit" type="submit" id="submit" tabindex="6" value="Kommentar absenden" />
				<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?/>" />
			</p>
		<?php do_action('comment_form', $post->ID); ?>
	</form>
	<?php endif; // If registration required and not logged in ?>

<?php endif; // if you delete this the sky will fall on your head ?>

<?php if ($comments) : ?>
	<ol class="trackbacklist">

	<?php foreach ($comments as $comment) : if ((get_comment_type() == "trackback") || (get_comment_type() == "pingback")) : $count++?>

		<li id="comment-<?php comment_ID() ?>" class="<?php echo $oddcomment; ?>">
			<p class="comment-number"><a href="#comment-<?php comment_ID() ?>"><?php echo $count; ?></a></p>
			<?php comment_type('Kommentar', 'Trackback', 'Pingback'); ?><small> von </small><cite><?php comment_author_link() ?></cite>
			<br />
			<small class="commentmetadata"><?php comment_date('j. F Y') ?> um <?php comment_time('H:i') ?> <?php edit_comment_link('&middot; bearbeiten','',''); ?></small>

			<?php //comment_text() ?>
		</li>

	<?php /* Changes every other comment to a different class */  
		if ('alt' == $oddcomment) $oddcomment = '';
		else $oddcomment = 'alt';
	?>

	<?php endif; endforeach; /* end for each comment */ ?>
	</ol>
 <?php else : // this is displayed if there are no comments so far ?>

	<?php if ('open' == $post->comment_status) : ?> 
		<!-- If comments are open, but there are no comments. -->
		
	 <?php else : // comments are closed ?>

		<p class="nocomments">Trackpacks/Pingbacks sind gechlossen.</p>
		
	<?php endif; ?>
<?php endif; ?>

</div>

Von Frank Bültge

bueltge.de [by:ltge.de] wird von Frank Bültge geführt, administriert und gestaltet. Alle Inhalte sind persönlich von mir ausgewählt und erstellt, nach bestem Gewissen und Können, was die Möglichkeit von Fehlern nicht ausschließt.

26 Kommentare

  1. Pingback: BloggingTom
  2. Hallo Frank,
    Das ist hier super erklärt aber mir bleibt eine Frage offen, da ich leider nicht so ein Ass in PHP und so bin.
    Wo wird der 1. große Abschnitt PHP Code den du hier aufgeführt hast eingebaut.

    Ich könnte auch diene ferige Comments.php verwenden, aber ich selber habe bei mir schon viele Sachen drinne, deshalb würde ich es leiebr versuchen bei mir miteinzubauen.

    Lieben Gruß David

    P.S. Dein Code Tag geht leider nicht.

  3. Die Prüfung

    <?php if ($comments) : ?> leitet es ein und die sollte es auch in deiner comments.php geben.
    Um Code zu hinterlegen, einfach die Tags bei aktivem JS nutzen oder <code lang="php"> </code>
    LG frank
  4. Habe mir deinen Link soeben angesehen, aber ich kann keine Fragezeichen erkennen? Ansonsten sieht es doch gut aus – ic hwürde max. noch ein margin um die Gravatare setzen, so dass sie nicht so eng am Text kleben.

  5. Hallo Frank, aiuch das sollte nun behoeb sein. Dankschön.

    Das mit den Fragzeichen liegt an meiner Kodierung. Ich habe UTF-8.
    Darf ich fragen was du hast ? Denn wenn sie bei dir nicht angezeigt werden… ist ja schon ein weing merkwürdig.

    LG David

  6. Hallo Frank!
    Wirklich schöne Anleitung. Habe meine Comments.php direkt umgebaut und siehe da alles funktioniert. Das ist bei mir nicht immer so. 😉
    Viele Grüße
    Michael

  7. Pingback: datenschmutz.net

Kommentare sind geschlossen.