Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wordpress template page for custom posts

Featured Replies

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 'homepage.php'.

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.

  • Author

Could you post your register_post_type() call?

 

 

// 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')
);
register_post_type('Headerhome',$args);
}

  • Author

What is the post type slug? (sorry, I'm new to WP)

Is it local or live? The other thing I'd point out there is WordPress already has two templates in the hierarchy for the homepage, there's home.php for when the homepage is the blog index and front-page.php for when the homepage is a Page.

  • Author

It's live, it's just a simple portfolio site I'm doing for a friend.

It's not going to win any design awards, but then again it's nowhere near finished nor is it anything more than a reference for employers and a place to house a basic blog.

 

www.scottomahony.co.uk

 

Well, how I have it set up is I have 2 template files, one for the blog and one for the home page (currently the home page template file is called homepage.php but it has the title of portfolio).

It's a custom 'loop' if you will that I've written for the custom post type that pulls in content from that post type (post type is called headerhome).

The custom post type (headerhome) uses advanced custom fields to edit the content and make it easy to update.

I've set wordpress to use a 'static page' for the front page and selected the homepage.php template as that page, although it's not 'static'.

 

 

Here's my (messy) code for the homepage.php

 

 

<?php

/*
Template Name: Homepage
*/
?>
<?php get_header(); ?>




<div id="main-content">

<div class="head-home">
   <?php

  query_posts(array(
		  'post_type' => 'headerhome',
		  'showposts' => 1
	  ) ); 
  ?>
  <?php while (have_posts()) : 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_query();?>

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



<?php get_footer(); ?>

Righto. First step is to rename the template to front-page.php, and make sure you have "Static Page" selected for homepage in Settings > Reading.

 

Next up, you want:

 

<?php
$portfolio_query = new WP_Query(array(
   'post_type' => 'headerhome',
   'showposts' => 1
   ) );
?>

 

instead of query_posts(), so you can use a second loop for the portfolio stuff with:

 

<?php while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>

 

leaving the main loop free to set up the page as WP expects.

 

As a side note, you should never use query_posts(), there's always a better way (either hooking to pre_get_posts or setting up a separate query).

Edited by Renaissance-Design

  • Author

Checked static page set for the homepage(it was), template renamed to front-page.php.

Change the loop to what you wrote.

 

Posted a new image to test just incase something was cached, still defaults to single.php instead of single-headerhome.php

 

Full code:-

 

<?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_query();?>

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


<?php get_footer(); ?>

 

 

ev3y1e.png

Edited by andys

  • Author

This is currently displaying using single.php?

 

Yup, I copied single.php and make some changes so I'll know if it displays the new template.

For example single-headerhome.php has no sidebar.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.