Individuelle Inhalte auf der Beitragsseite anzeigen
Veröffentlicht: – Kommentar hinterlassen
Sobald du eine Seite als Beitragsseite definierst, listet sie automatisch alle Beiträge auf. Aber wie kannst du davor oder danach individuelle Inhalte anzeigen lassen?
Für die Beitragsseite verwendet das Theme entweder die home.php
oder alternativ die index.php
, um ihren Inhalt anzuzeigen. Und standardmäßig enthalten diese Dateien nur ein Basis-Template mit der Beitragsschleife (dem „Loop“):
<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php rh_primary_bottom(); ?>
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
Code-Sprache: PHP (php)
Hier gibt es keine Ausgabe für den Inhalt der eigentlichen Beitragsseite.
Inhalt der Beitragsseite bekommen
Um die Daten für diese Seite zu erhalten, musst du sie manuell über get_post
und setup_postdata
abrufen. Damit der Loop danach immer noch funktioniert, musst du sicherstellen, rewind_posts
zu verwenden:
$page_for_posts_id = get_option( 'page_for_posts' );
if ( $page_for_posts_id ) {
// if $post is not explicitly set, the home content won't be displayed
// if there are no blog posts
global $post;
$actual_post = $post;
$home_page = get_post( $page_for_posts_id );
$post = $home_page;
setup_postdata( $home_page );
the_content();
rewind_posts();
$post = $actual_post;
}
Code-Sprache: PHP (php)
Wenn zudem kein Beitrag vorhanden ist, würde der Inhalt der Beitragsseite standardmäßig nicht angezeigt werden, solange die globale Variable $post
leer ist. Deshalb muss diese Variable zuerst mit dem Objekt der Beitragsseite überschrieben und später wiederhergestellt werden.
Zusammenfassend
Die gesamte home.php
könnte dann so aussehen:
<?php
/**
* The home template file.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
$page_for_posts_id = get_option( 'page_for_posts' );
if ( $page_for_posts_id ) {
// if $post is not explicitly set, the home content won't be displayed
// if there are no blog posts
global $post;
$actual_post = $post;
$home_page = get_post( $page_for_posts_id );
$post = $home_page;
setup_postdata( $home_page );
the_content();
rewind_posts();
$post = $actual_post;
}
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php rh_primary_bottom(); ?>
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
Code-Sprache: PHP (php)
Wie du sehen kannst, wird der Inhalt der Beitragsseite vor dem Loop ausgegeben. Aber du kannst den Code auch unter dem Loop angeben, um so den Inhalt nach der Beitragsliste anzuzeigen.