Support » Fixing WordPress » Get Page and Post Content – Custom Template

  • Hi,
    I use the code below to grab content from page 312 in my custom template. I’m wondering if I can use this template for many pages, but I need to use a function without any specific page ID as pages ID will be 312, 313, 314, etc…Something that grab the content of the page whatever is the page.

    $page_id = 312;
    $page_data = get_page( $page_id );
    $content = $page_data->post_content;
    $title = $page_data->post_title;
    echo $page_data->post_content;


Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi


    Forum Moderator

    Is this inside or outside of The Loop?

    Jumust, I think you and I have the same exact problem. I have several custom pages but I want to be able to edit them directly from admin, not through the template itself. I’m also calling up the content by referencing the post ID and hard-coding it into the template, but there has got to be a more efficient way to do this. I’m thinking there might be a way to create a hook that relates the page title or slug to the content? Have you figured this out?

    I figured it out. WordPress already has the code for grabbing that content. It should be located in page.php of your theme right after the get_header(); if your using Starkers theme like I am.

    Here’s the snippet you need:

    `<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    <?php if ( is_front_page() ) { ?>
    <h2><?php the_title(); ?></h2>
    <?php } else { ?>
    <h1><?php the_title(); ?></h1>
    <?php } ?>

    <?php the_content(); ?>
    <?php wp_link_pages( array( 'before' => '' . __( 'Pages:', 'twentyten' ), 'after' => '' ) ); ?>
    <?php edit_post_link( __( 'Edit', 'twentyten' ), '', '' ); ?>

    <?php comments_template( '', true ); ?>

    <?php endwhile; ?> `

    Works like a charm. Hope this helps.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Page and Post Content – Custom Template’ is closed to new replies.