Willkommen bei Schnipsel, Code Schnipsel zu Wordpress, PHP, HTML, CSS, jQuery und so weiter...

Jeder Kategorie ein eigenes Template zuweisen

04.2009 / von: David Hellmann / 2 Kommentare / Kategorie: Wordpress / Tags: , , ,

Dieses Funktion ermöglicht es, dass man für jede Kategorie eine eigene single.php anlegen. Es wird die Kategorie-ID ausgelesen und dann kann man ganz einfach eine neue single.php erstellen in folgender Form: single-kategorie-ID.php was dann so ausschaut: single-3.php - dies wäre dann das Template was für alle Artikel genutzt werden würde diese in der Kategorie 3 sind. Falls keine spezielle single.php für einen bestimme Kategorie vorhanden ist wird die Standard single.php genutzt. Das ganze am besten in die functions.php im Themefolder.

1
2
3
4
5
<?php
	add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { 
	if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) 
		return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));
?>

Statistik: read: 4588 / today: 2 / last: 08.02.2012
//

  1. 2 Kommetare • Eintrag schreiben

  2. David Hellmann →
    #1 Permalink Am 11. May 2009 um 14:58 Uhr

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
     
    <?php 
     
        // Hole die Kategorieübersicht, aber zeige sie noch nich an (echo = 0)
        $kategorien = wp_list_categories('title_li=&show_count=1&echo=0');
     
        // Array, Inhalt: Suche nach diesen Stringmustern...
        $array_suchen = array('(', ')');
        // ...und ersetze sie durch diese
        $array_ersetzen = array('<span class="kleineschrift">// ', ' Einträge</span>');
     
        // str_replace durchführen
        $kategorien = str_replace($array_suchen, $array_ersetzen, $kategorien);
     
        // Output
        echo $kategorien;
     
    ?>
  3. Mark
    #2 Permalink Am 15. June 2011 um 23:34 Uhr

    Hallo,
    Der Code scheint soweit zu funktionieren. Doch wenn ich im Backend auf “abmelden” klicke erhalte ich folgende Fehlermeldung:

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-login.php on line 354

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-login.php on line 366

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 720

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 721

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 722

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 723

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 724

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 725

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 728

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 729

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 730

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 731

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 734

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 735

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 736

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 737

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 897

    Wenn ich Beiträge aktualisieren möchte erhalte ich folgende Meldung:

    Warning: Cannot modify header information – headers already sent by (output started at /www/htdocs/w00deec4/wp-content/themes/telegraph/functions.php:7) in /www/htdocs/w00deec4/wp-includes/pluggable.php on line 897

    Was verursacht das Problem?
    Dankbar für jeden Tipp!

Kommentar schreiben

Code einfügen: <pre lang="php" line="1"> Code hier </pre>