0

To display an advert custom post type on the home page along with standard posts, I added it to the main WordPress loop:

/** Add "advert" post type to the main query **/
add_action( 'pre_get_posts', 'add_advert_post_type_to_query' );
function add_advert_post_type_to_query( $query ) {
  if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'advert' ) );
  return $query;
}

In the index.php of my theme (Twenty Seventeen) I see what template is used by loop to display the posts content:

        <?php
        if ( have_posts() ) :

            /* Start the Loop */
            while ( have_posts() ) : the_post();

                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( 'template-parts/post/content', get_post_format() );

            endwhile;

            the_posts_pagination( array(
                'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
                'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
                'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
            ) );

        else :

            get_template_part( 'template-parts/post/content', 'none' );

        endif;
        ?>

In that template (template-parts/post/content) I see that it is used only for the post type:

        if ( 'post' === get_post_type() ) :
            echo '<div class="entry-meta">';
                if ( is_single() ) :
                    twentyseventeen_posted_on();
                else :
                    echo twentyseventeen_time_link();
                    twentyseventeen_edit_link();
                endif;
            echo '</div><!-- .entry-meta -->';
        endif;

So, I don't understand what template is used to display my custom post type? Related to this I have a question: a) how to "force" the main loop to use the same template for my custom post type as for the post type and/or b) how to assign a custom template to my custom post type when displayed by the main loop on the home page?

UPDATE

Can this be used and how?

function get_template_part_custom($content_name) {
     global $post;
     //its paper type?
     if ($post->post_type == 'paper') {
         //get template-parts/content-paper.php
         get_template_part( 'template-parts/content', $content_name );
     }else{
         //fallback to the default
         get_template_part( 'template-parts/content', get_post_format($post) );
     }
}
1

You are checking the source code of TwentySeventeen's content.php selectively. The lines you quote are only about printing the publication time in case of a regular post. Directly below your lines the post/page title is printed regardless of post type.

Regarding a) you don't need to force anything, because as long as there is no specific template for this post type, WP will use the default post template anyway.

Regarding b) you will have to deal with this in a child theme, because that would mean expanding the functionality. You would modify content.php to include an extra test for post type and include formatting for that. In case you want to deal with this in a plugin, the link you give in the update contains a good lead.

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.