July 26, 2008

Überschrift des Related-Post Plugins ändern

Das Related-Post Wordpress Plugin liefert zusätzliche Informationen für einen Beitrag. Allerdings kann der durch das Plugin erzeugte HMTL-Code nur durch eine Änderung am Plugin selber angepasst werden. Allerdings besteht eine weitere Möglichkeit das Rendering anzupassen: Reguläre Ausdrücke. Der folgende Code-Schnipsel zeigt die Veränderung der Überschrift, indem einfach das HTML-Markup darum ausgetauscht wird.
$related = wp_get_related_posts();

$find = '/<h3>(.*)<\/h3>(.*)/i';
$replace = '<h4 class="highlighted">$1</h4>$2';
echo preg_replace($find, $replace, $related);
The related posts plugin of wordpress generates nice additional information for a post. But to customize the generated HTML code you have to change the plugin code. Another solution is using a regular expression. Following this approach it is very easy to customize the header generated by the plugin. Here is the according PHP code snippet.
$related = wp_get_related_posts();

$find = '/<h3>(.*)<\/h3>(.*)/i';
$replace = '<h4 class="highlighted">$1</h4>$2';
echo preg_replace($find, $replace, $related);

It simply extract the header text and wrap it into other HTML tags.

No comments:

Post a Comment