Creating post template for a Custom Post Type

The question:

I have a custom post type called ‘abstract’ which contains the following meta_input array items

 $abstract_details = array(
 'post_title' => $result->title,
meta_input' => array(
                'abstract_id' => $result->abstract_id,
                'title' => $result->title,
                'author' => $result->author,
                'writeup' => $result->writeup,
                'ref' => $result->ref,
            ),
            'post_type'   => 'abstract',
            'post_status' => 'publish',
        );

I want to display the contents of these items into their respective post, using something similar to below

<li style="list-style-type: none; text-align: justify;"><?php echo nl2br(esc_html(get_post_meta(get_the_ID(), 'writeup', true))) ?></li>
                    <br>
                    <?php $isAvailRef = get_post_meta(get_the_ID(), 'ref', true)?>
                    <?php $refTag = "References/Bibliography: "?>
                    <li style="list-style-type: none;"><b><?php if(!empty($isAvailRef)): echo $refTag; endif;?></b></li>
                    <li style="list-style-type: none;"><?php echo nl2br(esc_html($isAvailRef))?></li>
                    <br>
                    <li style="list-style-type: none;"><b>Student: </b><?php echo esc_html(get_post_meta(get_the_ID(), 'author', true)) ?></li>

I’m quite new to WordPress (and quite frankly coding in general) and am stuck on where to actually include the above to only be found within the contents of the post. I’ve seen some tutorials suggesting to create a content-abstract.php file and add the details in there, but doing so would also add the entire post into the excerpt area of the post.

So the question is, where do you include the HTML code to display the details of the CPT, as seen below:
Creating post template for a Custom Post Type

Without also having to show this content in the excerpt? (like below)

Creating post template for a Custom Post Type

In case it’s of any benefit, here are the contents of the default content.php file of the theme I am using

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php engage_mag_do_microdata('article'); ?>>
    <?php
    global $engage_mag_theme_options;
    $engage_mag_show_image = 1;
    if(is_singular()) {
        $engage_mag_show_image = $engage_mag_theme_options['engage-mag-single-page-featured-image'];
    }
    $engage_mag_show_content = $engage_mag_theme_options['engage-mag-content-show-from'];
    $engage_mag_thumbnail = (has_post_thumbnail() && ($engage_mag_show_image == 1)) ? 'engage-mag-has-thumbnail' : 'engage-mag-no-thumbnail';
    ?>
    <div class="engage-mag-content-container <?php echo $engage_mag_thumbnail; ?>">
        <?php
        if ($engage_mag_thumbnail == 'engage-mag-has-thumbnail'):
            ?>
            <div class="post-thumb">
                <?php
                engage_mag_post_formats(get_the_ID());
                engage_mag_post_thumbnail();
                ?>
            </div>
        <?php
        endif;
        ?>
        <div class="engage-mag-content-area">
            <header class="entry-header">

                <div class="post-meta">
                    <?php
                    engage_mag_list_category(get_the_ID());
                    ?>
                </div>
                <?php

                if (is_singular()) :
                    the_title('<h1 class="entry-title" ' . engage_mag_get_microdata("heading") . '>', '</h1>');
                else :
                    the_title('<h2 class="entry-title" ' . engage_mag_get_microdata("heading") . '><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h2>');
                endif;

                if ('post' === get_post_type()) :
                    ?>
                    <div class="entry-meta">
                        <?php
                        engage_mag_posted_on();
                        engage_mag_read_time_words_count(get_the_ID());
                        engage_mag_posted_by();
                        ?>
                    </div><!-- .entry-meta -->
                <?php endif; ?>
            </header><!-- .entry-header -->


            <div class="entry-content">
                <?php
                if (is_singular()) :
                    the_content();
                else :
                    if ($engage_mag_show_content == 'excerpt') {
                        the_excerpt();
                    } else {
                        the_content();
                    }
                endif;

                wp_link_pages(array(
                    'before' => '<div class="page-links">' . esc_html__('Pages:', 'engage-mag'),
                    'after' => '</div>',
                ));
                ?>

                <?php
                $engage_mag_read_more_text = $engage_mag_theme_options['engage-mag-read-more-text'];
                if ((!is_single()) && ($engage_mag_show_content == 'excerpt')) {
                    if (!empty($engage_mag_read_more_text)) { ?>
                        <p><a href="<?php the_permalink(); ?>" class="read-more-text">
                                <?php echo esc_html($engage_mag_read_more_text); ?>

                            </a></p>
                        <?php
                    }
                }
                ?>
            </div>
            <!-- .entry-content -->

            <footer class="entry-footer">
                <?php engage_mag_entry_footer(); ?>
            </footer><!-- .entry-footer -->

            <?php
            /**
             * engage_mag_social_sharing hook
             * @since 1.0.0
             *
             * @hooked engage_mag_constuct_social_sharing -  10
             */
            do_action('engage_mag_social_sharing', get_the_ID());
            ?>
        </div> <!-- .engage-mag-content-area -->
    </div> <!-- .engage-mag-content-container -->
</article><!-- #post-<?php the_ID(); ?> -->

Thank you in advance.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

WordPress Template Hierarchy is your solution. Create a template called single-abstract.php and copy the contents from single.php (Of course you’ll need to edit that template to use your HTML). WordPress now will use that template whenever the singular version displays on frontend.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment