WordPress.org

Ready to get started?Download WordPress

Forums

Get Page and Post Content - Custom Template (4 posts)

  1. jumust
    Member
    Posted 3 years ago #

    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.

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

    Thanks

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Is this inside or outside of The Loop?

  3. Chilaxnanananananana
    Member
    Posted 3 years ago #

    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?

  4. Chilaxnanananananana
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic