Show one post from certain category
-
I have created a template based on single.php in TwentyEleven, using the following code to show the latest post from category 5:
<?php // Display blog posts on any page $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=1&cat=5'); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <?php get_template_part( 'content', 'single' ); ?>
Works great!
I tried to transplant that code into a new template based on singular.php in TwentyTwenty and it does not work – I get an error of ‘unexpected end of page’. I have checked repeatedly for missing
}
;
and?>
but everything is in its place.When adding the code, I removed
<?php if ( have_posts() ) { while ( have_posts() ) { the_post(); get_template_part( 'template-parts/content', get_post_type() ); } } ?>
But if I re-add the ‘if’ statement and the corresponding braces, I get an error of ‘unexpected }’ so I am really lost!
Can someone please explain what I am doing wrong, what I am missing, or if there is a better way to achieve what I want?
*Note that yes I did change the old code to reflect the proper path to content.php in TwentyTwenty. And yes, I am using a child theme.
- The topic ‘Show one post from certain category’ is closed to new replies.