1

Sorry for silly question, I'm newbie in Wordpress and PHP. I created a custom post type using this tutorial. The category page is working normally, but the single is displaying all posts from category. I need to display only current post on single.php template. How can I do this? Here is the code of my single.php file in 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(); ?>

And this code defines template files:

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 );
0

Your single.php is coded in a way that show all posts like archive, it needs to be modified.

To fetch current post, try something like that instead:

<?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(); ?>
0

Try using the posts_per_page argument in your WP_Query and setting it to 1:

$mypost = array( 'post_type' => 'movie_reviews', 'posts_per_page' => 1);
$loop = new WP_Query( $mypost );
// ...stuff and things
  • Thanks, but it display only first post. I need to display current post. For example in site.com/movie-reviews/first-review/ must display "first-review" post content. – A. Korolev Sep 19 '16 at 23:35
0

Here is the documentation for it: https://codex.wordpress.org/Post_Type_Templates

single-{post_type}.php

If your custom post type were 'product', and/or query_var = "product", WordPress would look for single-product.php to display the single or permalink of the post.

  • Thanks, but that's not what I need. The single post template defines correctly, but it display all posts from category. I need to display only current post. For example in site.com/movie-reviews/first-review/ must display only "first-review" post content, but now displaying all posts from "movie-reviews". – A. Korolev Sep 19 '16 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.