So, my parent-theme uses a single.php calling a content-single.php with the function get_template_part().
The code for single.php:
get_header(); ?>
<div class="container">
<main id="main" class="postItem" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php codex_coder_content_nav( 'nav-below' ); ?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
<!--<?php get_sidebar(); ?>
I am using a custom post_type named "ourNews". Following wordpress documents, I created two files: single-ourNews.php and content-single-ourNews.php
With this, in my "single-ourNews.php" I changed the following line:
<?php get_template_part( 'content', 'single-ourNews' ); ?>
But it keeps loading the file "content-single.php". What am I doing wrong?
Other question is: How can I put a image with relative path on this custom template? I created a folder "img" and I was calling using:
But says that the image was not found. I read a little and some places said that I could not use relative path, but why not, if the theme uses? I'm confused.
Wordpress is still loading single.php that loads content-single.php
Your custom post is not ourNews. This is probably the label but not the slug. The slug is always lowercase and separated by a -. Try renaming your file to single-our-news.php.
Also, every time you register a custom post type, you should reset your permalinks options back to default and then back to what you want it to be.
For the relative path, you should not use it. You should use .'/img/name-of-image.jpg
When using get_template_part
, the first parameter defines the base template name (eg content
, which loads content.php
) and the second parameter loads a suffixed version if available. Therefore your call
get_template_part('content', 'single-ourNews');
is looking for a file named content-single-ourNews.php
first, then falls back to content.php
in case the first one is not available. I'm not sure whether the get_template_part
function converts the suffix parameter to something like single-ournews
or single-our-news
before appending it to the first parameter, be sure to test a few variants of that.
I'm not 100% sure if the function behaves differently or not in child themes and parent themes. One option is to override the parent's single.php
in the child theme and modify it directly with
if ($post->post-type === 'ourNews') {
get_template_part('content', 'single-ourNews');
}
else {
get_template_part('content');
}
Lastly, WordPress will look for a file single-[cptslug].php
before loading a template for a custom post type's single view.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.