Custom content template for custom post type

I am using the SmartMag theme with custom post types.

I want to create a custom template to display my custom post type fields and all the tutorials say that I need to create a new copy of ‘single.php’ and call it ‘single-new-post-type.php’.

My problem is that single.php contains a lot of feature of my template which I don’t want to “copy-paste”. Instead, I just want to create a new file which will ONLY contain the body of my custom post (the part that is actually different from the regular post content).

Any idea how I can do it elegantly?

My single.php file:

<?php

/**
* The single post template is selected based on your global Theem Settings or the post
* setting.
*
* Template files for the post layouts are as follows:
*
* Classic: Uses content.php
* Post Cover: partials/single/layout-cover.php
* Modern: partials/single/layout-modern.php
*/

$template = Bunyad::posts()->meta(‘layout_template’);
$partial = ‘partials/single/layout-‘ . $template;

if (!$template OR strstr($template, ‘classic’)) {
$template = ‘classic’;
$partial = ‘content’;
}

if ($template != ‘classic’) {
Bunyad::core()->add_body_class(‘post-layout-‘ . $template);
}

?>

<?php get_header(); ?>

<div class=”main wrap cf”>

<?php if (in_array($template, array(‘cover’))): // Cover layout is a special case – defines it’s own structure ?>

<?php get_template_part($partial); ?>

<?php else: ?>

<div class=”row”>

<div class=”col-8 main-content”>

<?php while (have_posts()) : the_post(); ?>

<?php

$panels = get_post_meta(get_the_ID(), ‘panels_data’, true);

if (!empty($panels) && !empty($panels[‘grid’])):

get_template_part(‘content’, ‘builder’);

else:

get_template_part($partial, ‘single’);

endif;
?>

<div class=”comments”>
<?php comments_template(”, true); ?>
</div>

<?php endwhile; // end of the loop. ?>

</div>

<?php Bunyad::core()->theme_sidebar(); ?>

</div> <!– .row –>

<?php endif; ?>

</div> <!– .main –>

<?php get_footer(); ?>

Here’s my layout-modern.php file (I am using the Modern layout of the theme):

<?php
/**
* Partial Template for Single Post “Modern Layout” – called from single.php
*/

$classes = get_post_class();

// Dynamic style enabled by default, unless overridden
$is_dynamic = (isset($is_dynamic) ? $is_dynamic : true);
?>

<article id=”post-<?php the_ID(); ?>” class=”<?php echo join(‘ ‘, $classes); ?>”>

<header class=”post-header-b cf”>

<div class=”category cf”>
<?php echo Bunyad::blocks()->cat_label(array(‘force_show’ => true)); ?>
</div>

<div class=”heading cf”>
<?php
$tag = ‘h1’;
?>

<<?php echo $tag; ?> class=”post-title”>
<?php if (!is_front_page() && is_singular()): the_title(); else: ?>

<a href=”<?php the_permalink(); ?>”>
<?php the_title(); ?></a>

<?php endif;?>
</<?php echo $tag; ?>>

</div>

<div class=”post-meta-b cf”>

<span class=”author-img”><?php echo get_avatar(get_the_author_meta(‘user_email’), 35); ?></span>

<span class=”posted-by”><?php _ex(‘By’, ‘Post Meta’, ‘bunyad’); ?>
<?php the_author_posts_link(); ?>
</span>

<span class=”posted-on”>
<time class=”post-date” datetime=”<?php echo esc_attr(get_the_time(DATE_W3C)); ?>”><?php echo esc_html(get_the_date()); ?></time>
</span>

<a href=”<?php comments_link(); ?>” class=”comments”><i class=”fa fa-comments-o”></i><?php comments_number(); ?></a>

</div>

<?php get_template_part(‘partials/single/social-share-b’)?>

<?php Bunyad::core()->partial(‘partials/single/featured’, array(‘caption_class’ => ‘wp-caption-text’)); ?>

</header><!– .post-header –>

<?php
// page builder for posts enabled?
$panels = get_post_meta(get_the_ID(), ‘panels_data’, true);
if (!empty($panels) && !empty($panels[‘grids’]) && is_singular() && !is_front_page()):
?>

<?php Bunyad::posts()->the_content(); ?>

<?php
else:

$post_classes = array(‘post-content’);

if ($is_dynamic) {
$post_classes[] = ‘post-dynamic’;
}

if (Bunyad::posts()->meta(‘content_slider’)) {
$post_classes[] = ‘post-slideshow’;
}

?>

<div <?php Bunyad::markup()->attribs(‘post-content-modern’, array(‘class’ => $post_classes)); ?>>

<?php
// get post body content
get_template_part(‘partials/single/post-content’);
?>

</div><!– .post-content –>

<?php
endif; // end page builder blocks test
?>

<?php
// add social share
get_template_part(‘partials/single/social-share’);
?>

</article>

<?php

// add next/previous
get_template_part(‘partials/single/post-navigation’);

// add author box
get_template_part(‘partials/single/author-box’);

// add related posts
get_template_part(‘partials/single/related-posts’);

Here’s the post-content.php file:

<?php
/**
* Partial template to get post body content on single page
*/
?>

<?php
// multi-page content slideshow post?
if (Bunyad::posts()->meta(‘content_slider’)):
get_template_part(‘partials/pagination-next’);
endif;

?>

<?php
// excerpts or main content?
if ((!is_page() && is_singular()) OR !Bunyad::options()->show_excerpts_classic OR Bunyad::posts()->meta(‘content_slider’)):

Bunyad::posts()->the_content();
else:
echo Bunyad::posts()->excerpt(null, Bunyad::options()->excerpt_length_classic, array(‘force_more’ => true));
endif;

?>

<?php
// multi-page content slideshow post – duplicated pagination at bottom
if (Bunyad::posts()->meta(‘content_slider’)):
Bunyad::core()->partial(‘partials/pagination-next’, array(‘position’ => ‘bottom’));
endif;

?>

<?php
// multi-page post – add numbered pagination
if (!Bunyad::posts()->meta(‘content_slider’)):

wp_link_pages(array(
‘before’ => ‘<div class=”main-pagination post-pagination”>’,
‘after’ => ‘</div>’,
‘link_before’ => ‘<span>’,
‘link_after’ => ‘</span>’));
endif;

?>

<?php if (is_single() && Bunyad::options()->show_tags): ?>
<div class=”tagcloud”><?php the_tags(”, ‘ ‘); ?></div>
<?php endif; ?>

Read more here:: Custom content template for custom post type

Leave a Reply

Your email address will not be published. Required fields are marked *