1

If I'm creating a page template, let's say for example one for a testimonials custom post type I've created, I think I have it figured out that if I do the following in page-testimonials.php:

    <?php get_template_part('loop', 'page'); ?>
    <?php get_template_part('loop', 'testimonials'); ?>

Then that will output the content of the page itself and then go into the testimonials loop to grab a list of the most recent testimonials, etc.

I'm having trouble doing the same for a page that is called from WP Settings as the "Posts Page". Let's say at the top of the "Blog" page (a WordPress page), I want some intro text to the blog that says, "Welcome to the blog. Please check out new posts below." Instead of hardcoding that into the template, how would that be achieved? Am I even thinking of how actual page content is displayed depending on if a custom page template is chosen? Thanks!

1 Answer 1

1

I'm having trouble doing the same for a page that is called from WP Settings as the "Posts Page". Let's say at the top of the "Blog" page (a WordPress page), I want some intro text to the blog that says, "Welcome to the blog. Please check out new posts below." Instead of hardcoding that into the template, how would that be achieved?

First, understand that, per the WordPress Template Hierarchy, the template files used in this context are, in order of preference: home.php, and index.php. Nothing you do to a custom page template will have any impact on what WordPress uses to render the Blog Posts Index.

Your best solution here would be to hard-code this text into home.php, outside the Loop. You could put this text inside a template-part file, and call it via get_template_part( $foobar ) if you really wanted to, but doing so honestly serves no purpose here.

By the way: what is your aversion to hard-coding template-specific text into a template file?

p.s. here is a more current version of the Template Hierarchy. (I'll be adding it to the Codex shortly.)

EDIT

I am working on a theme that will be distributed/available to dozens, if not hundreds of different clients. I'd like clients to be able to control if there is any intro text within the blog area.

I really don't think you can output the $post->post_content for the static Page used as page_for_posts. Maybe you could use a custom query, e.g. using get_page():

<?php
$page_for_posts = get_page( get_option( 'page_for_posts' ) );
$page_for_posts_content = $page_for_posts->post_content;
echo apply_filters( 'the_content', $page_for_posts_content );
?>

Alternately, I would suggest one of two approaches:

  1. Create a Theme Option to define this text/content
  2. Make that part of the template file a dynamic sidebar, and let users add a Widget - e.g. a Text Widget - in which they can define this text/content
5
  • Hi @Chip, Really appreciate the quick response. The main reason for not hard-coding in static text is because I am working on a theme that will be distributed/available to dozens, if not hundreds of different clients. I'd like clients to be able to control if there is any intro text within the blog area. I am correct in how I was doing "other" custom page templates? I understand the "Posts Page" setting in WordPress seems to override any page template called, but do you agree that it seems weird you can't put any intro text before this area is called from a page? Thanks!
    – Zach
    Commented Nov 1, 2011 at 18:30
  • @Zach see updated answer; I've added three options. Commented Nov 1, 2011 at 18:52
  • H*** crap @Chip I think you did it ha. The first option (with grabbing the page that is set as the page_for_posts) worked great! Honestly can't see a downside to this... seems like it'd give some extra flexibility to this area. Thank you very much for the help.
    – Zach
    Commented Nov 1, 2011 at 19:51
  • Glad it worked! That was a stab in the dark. :) Commented Nov 1, 2011 at 19:52
  • Ouch. get_page() is now deprecated.
    – zipzit
    Commented Nov 15, 2014 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.