Custom post type not using it’s own single-postname.php

I have a custom post type (that I’m using on a template page), the custom post type is called ‘headerhome’. When I post say a gallery there I’m trying to get it to redirect to ‘single-headerhome.php’, yet it always just goes to ‘single.php’.

The name of the custom post type itself is arbitrary, the name of the templage page it’s on is ‘front-page.php’.

Read More

I’m really confused, I’m not the best at WP yet so when something simple like this doesn’t work I have no idea how to fix it, even after pouring over the codex for an hour.

// Add new post type for homepage
add_action('init', 'frontpage_top_init');
function frontpage_top_init() 
{
    $args = array(
        'label' => __('Homepage'),
        'singular_label' => __('Homepage'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'supports' => array('title','editor'),
        'has_archive' => true
    ); 
    register_post_type('headerhome',$args);

}

Here’s my code on the front-page.php

EDIT:-

<?php 
/*
    Template Name: frontpage
*/
?>
<?php get_header(); ?>
<div id="main-content">
<div class="head-home">
      <?php
$portfolio_query = new WP_Query(array(
        'post_type' => 'headerhome',
        'showposts' => 1
        ) );
?>
      <?php while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>

       <h2><?php the_title(); ?></h2>  
      <?php the_content(); ?>

  <div style="clear:both"></div>

  <div class="embed-container">
      <?php the_field('showreel'); ?>

  </div> <!-- end div embed-container -->   

    </div> <!-- end div head-home -->               

    <div class="gallerydescription">
        <?php the_field ('gallery-description'); ?>
    </div> <!-- end div gallerydescription -->  

<?php the_field('image-1'); ?>
<div style="clear:both;"></div>

<?php endwhile; wp_reset_postdata();?>

</div> <!-- end div main-content -->

<?php get_footer(); ?>

No matter what I try, it always uses single.php and not single-headerhome.php.
If my understand of wordpress is correct, and it’s probably not – shouldn’t creating the template file single-headerhome.php automatically make wordpress use that for this post type?

Related posts

Leave a Reply

1 comment

  1. Have you flushed your permalinks?

    If not, visit your permalinks page in your dashboard, then, refresh the page you are trying to view.

    Let us know if that helps.

    Edit:

    Upon re-reading your question, actually, no – just because you create a single-post_type.php file does not mean WordPress will use that file with regards to your front-page.php template file.

    Your front-page.php file is what is rendering your post. This is your issue.

    If you want your single-post_type.php file to render the display of your post type within front-page.php then you can do the following;

    <?php
    $portfolio_query = new WP_Query(array(
    'post_type' => 'headerhome',
    'showposts' => 1
    ));
    ?>
    
    <?php while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>
    
    <?php get_template_part('single-post_type.php'); ?>
       
    <?php endwhile; wp_reset_postdata();?>
    

    But obviously make sure that single-post_type.php does not contain a loop/query within itself for if it does then you should remove the outer while/endwhile statements contained within front-page.php