Sorry für dumme Frage, ich bin Neuling in Wordpress und PHP. Ich habe mit this tutorial einen benutzerdefinierten Beitragstyp erstellt. Die Kategorieseite funktioniert normal, aber die Single zeigt alle Beiträge aus der Kategorie an. Ich muss nur den aktuellen Beitrag in der single.php-Vorlage anzeigen. Wie kann ich das machen? Hier ist der Code meiner single.php Datei im Movie Reviews Plugin.
<?php
get_header(); ?>
<section id="content">
<div class="wrap-content blog-single">
<?php
$mypost = array( 'post_type' => 'movie_reviews', );
$loop = new WP_Query( $mypost );
?>
<?php if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title( '<h1>','</h1>' ); ?>
<div class="post-thumbnail">
<?php the_post_thumbnail(array(250, 250)); ?>
</div>
<div class="entry-content"><?php
the_content(); ?></div>
</article>
<?php endwhile; ?>
<?php endif; ?>
</div>
</section>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Und dieser Code definiert Vorlagendateien:
function include_template_function( $template_path ) {
if ( get_post_type() == 'movie_reviews' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'single-movie.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . '/single-movie.php';
}
} else {
if ( $theme_file = locate_template( array ( 'movie-category.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . '/movie-category.php';
}
}
}
return $template_path;
}
add_filter( 'template_include', 'include_template_function', 1 );
Deine single.php ist so kodiert, dass alle Beiträge wie Archiv angezeigt werden, sie muss modifiziert werden.
Versuchen Sie stattdessen Folgendes, um den aktuellen Beitrag abzurufen:
<?php get_header(); ?>
<section id="content">
<div class="wrap-content blog-single">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title( '<h1>','</h1>' ); ?>
<div class="post-thumbnail"><?php the_post_thumbnail(array(250, 250)); ?> </div>
<div class="entry-content"><?php the_content(); ?></div>
</article>
<?php endwhile; ?>
</div>
</section>
<?php get_footer(); ?>
Hier ist die Dokumentation dazu: https://codex.wordpress.org/Post_Type_Templates
single- {post_type} .php
Wenn Ihr benutzerdefinierter Beitragstyp 'Produkt' und/oder query_var = "Produkt" wäre, würde WordPress nach single-product.php suchen, um die Einzel- oder Permalink-Datei des Beitrags anzuzeigen.
Verwenden Sie das Argument posts_per_page
in Ihrer WP_Query und setzen Sie es auf 1
:
$mypost = array( 'post_type' => 'movie_reviews', 'posts_per_page' => 1);
$loop = new WP_Query( $mypost );
// ...stuff and things