1

I am using a wordpress theme which has a page template named roomtypes. I am not a php pro but as far as I understand this template gets the posts. When I add a Room type, it is created as a post with a post_type=roomtype.
So this template gets these posts, adds their title and a thumbnail image.

My problem is, when I create a page and enter some text it does not shows when I choose this template.
So with some digging I found out that the_content(); function does the trick. I tried to add it and it worked:)
The matter is, I cannot use the content where I need it to be. This is the code:

<?php
/*
Template Name: Roomtypes
 */
global $wp_query;
get_header();?>
       <div class="middle-inner-wrapper" style="background:#e7dfd6 url(<?

php echo TEMPLATE_URL.get_option('tgt_default_inner_background');?>) no-

repeat center top;">

            <div class="localization">
                <p class="site-loc"><a href="<?php echo HOME_URL;?>" 

style="color:white"> <?php echo get_option('tgt_hotel_name'); ?

></a></p><p>&raquo;&nbsp;<?php _e ('Rooms', 'hotel');?></p>
            </div>            
         <div style="clear:both;"></div>         
            <div class="middle-inner">
                <div class="center-inner">               
                    <!-- This is content -->

                    <?php                           


                        global $post;
                        query_posts

("post_type=roomtype&orderby=name");
                        if ( have_posts() ) {       
                            ?>
                        <div class="title">
                        <p class="h1"><?php _e ('Rooms', 

'hotel');?></p> 
                        </div>


                        <?php $i=0; while ( 

have_posts() ) { the_post(); $i++; 
                         //$thumbnail_id = 

get_post_thumbnail_id($post->ID);
                        // $link_image = 

wp_get_attachment_image_src( $thumbnail_id, 'roomtype-image' );
                        //$link_thumbnail =  

wp_get_attachment_image_src($thumbnail_id); 
                        if(has_post_thumbnail()) {
                            $thumbnail_id = 

get_post_thumbnail_id($post->ID);
                            $link_image = 

wp_get_attachment_image_src( $thumbnail_id, 'roomtype-image' );
                        } else {    
                          $args = array(


'post_type' => 'attachment',


'numberposts' => -1,            


'post_parent' => $post->ID, 


'post_mime_type' => 'image'
                                    ); 
                            $attachments = 

get_posts($args);
                            $link_image = 

wp_get_attachment_image_src( $attachments[0]->ID, 'roomtype-image' );   
                        }           
                        ?>              


                        <?php if($i%2!=0){  ?>

                        <div class="room-left">
                            <div class="room-

box" align="center">            

                                <a 

href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php if 

($link_image[0] != ''){ ?>
                                <img 

src="<?php echo $link_image[0]; ?>" />
                                <?php } ?>
                                </a>
                            </div>
                            <a class="room-

link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php 

the_title(); ?></a>

                        </div>
                        <?php } else{ ?>
                        <div class="room-right">
                            <div class="room-

box" align="center">
                                <a 

href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php if 

($link_image[0] != ''){ ?>
                                <img 

src="<?php echo $link_image[0]; ?>" style="margin-top:3px;" />
                                <?php } ?>
                                </a>
                            </div>
                            <a class="room-

link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php 

the_title(); ?></a>
                        </div>

                        <?php } };
                        }else{ ?>
                        <div class="title">     


                        <p class="h11"> <?php _e('No Rooms 

Available!', 'hotel') ?> </p>
                        <div class="title-comments">
                        </div>

                        <div class="news-content">
                            <p>
                            <?php
                                _e('Sorry, but this section 

has no rooms!', 'hotel');
                            ?> 
                            </p>    
                        </div>

                    </div>
                        <?php } ?>
                        <?php wp_reset_query(); ?>
                    <div style="clear:both;"></div>         
                </div>
            </div>

            <?php get_sidebar();?>
           <div class="bottom">
                <!--<img src="<?php echo TEMPLATE_URL;?

>/images/inner-page-bottom.jpg" alt="inner_page_bottom_image"/>-->
           </div>

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

    <?php get_footer();?>

I need the content to be where I'm indicating in the code, but some how when I add

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

  <?php the_content(); ?>

 <?php endwhile; endif; ?>
</div>

Things gets messy.
If I only add the_content(); then it gets much more worse,all those posts content is shows:(
How can I add only the content of the page I use the page template?

2 Answers 2

1

the_content() (and many others) must be inside The Loop.

For retrieving a post/page/custom-post-type data outside the Loop, take a look here:
Get post content from outside the loop

The working code, based on your example, would be:

<div class="middle-inner">
<div class="center-inner">               
    <!-- This is content -->

    <?php                               
        global $post;
        query_posts("post_type=roomtype&orderby=name");
        if ( have_posts() ) {       
            ?>

        <div class="title">
        <p class="h1"><?php _e ('Rooms', 'hotel');?></p> 
        </div>

        <?php $i=0; while ( have_posts() ) { the_post(); $i++; 
         //$thumbnail_id = get_post_thumbnail_id($post->ID);
        // $link_image = wp_get_attachment_image_src( $thumbnail_id, 'roomtype-image' );
        //$link_thumbnail =  wp_get_attachment_image_src($thumbnail_id);    

        <?php echo 'I NEED MY PAGE CONTENT HERE - just add the following line' ?>
        <?php the_content(); ?>

        if(has_post_thumbnail()) {
            $thumbnail_id = get_post_thumbnail_id($post->ID);
            $link_image = wp_get_attachment_image_src( $thumbnail_id, 'roomtype-image' );
        } else {    
          $args = array(
                    'post_type' => 'attachment',
                    'numberposts' => -1,            
                    'post_parent' => $post->ID, 
                    'post_mime_type' => 'image'
                    ); 
            $attachments = get_posts($args);
            $link_image = wp_get_attachment_image_src( $attachments[0]->ID, 'roomtype-image' ); 
        }           
        ?>
5
  • I pasted your code into mine but only a white page come up. Let me share the whole page because I might missed something:) I might be significat to me but you might see the problem :) I will edit my post.
    – cevizmx
    Commented Dec 2, 2012 at 10:13
  • I'd say you should restart from the original file. The code you pasted is quite hard to read without proper indentations.. :/
    – brasofilo
    Commented Dec 2, 2012 at 15:55
  • brasofilo, actually the last code is the origilan one. I have used codepag.org agani, maybe it looks better. codepad.org/9NPqW4sn what do you think?
    – cevizmx
    Commented Dec 2, 2012 at 23:27
  • Searching for the error.... But now your problem is not the_content()?, because I can't see it in that paste.
    – brasofilo
    Commented Dec 2, 2012 at 23:47
  • Yes, I pasted the original code. I am trying to add the_content(); here codepad.org/umUlHADt which does not work
    – cevizmx
    Commented Dec 3, 2012 at 8:47
0

Use get_the_content() to store the page's content to a variable ($page_content) and then you can use it where ever you need it. If you use it inside your loop for the posts, use something like if ( $i == 0 ) echo $page_content so it'll only be shown once:

<div class="middle-inner">
    <div class="center-inner">           
        <!-- This is content -->            
        <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        <?php $page_content = get_the_content(); ?>
        <?php endwhile; ?>

        <?php
        global $post;
        query_posts("post_type=roomtype&orderby=name");
        if ( have_posts() ) {       
        ?>
        <div class="title">
        <p class="h1"><?php _e ('Rooms', 'hotel');?></p> 
        </div>

        <?php echo $page_content; ?>

        <?php $i=0; while ( have_posts() ) { the_post(); $i++; 
         //$thumbnail_id = get_post_thumbnail_id($post->ID);
        // $link_image = wp_get_attachment_image_src( $thumbnail_id, 'roomtype-image' );
        //$link_thumbnail =  wp_get_attachment_image_src($thumbnail_id);    

        if(has_post_thumbnail()) {
            $thumbnail_id = get_post_thumbnail_id($post->ID);
            $link_image = wp_get_attachment_image_src( $thumbnail_id, 'roomtype-image' );
        } else {    
          $args = array(
                    'post_type' => 'attachment',
                    'numberposts' => -1,            
                    'post_parent' => $post->ID, 
                    'post_mime_type' => 'image'
                    ); 
            $attachments = get_posts($args);
            $link_image = wp_get_attachment_image_src( $attachments[0]->ID, 'roomtype-image' ); 
        }           
        ?>
2
  • Tried your code as well, the page came normally but nothing from the page_content(); function. I even tried to embed it in a div, div came in the code but nothing was inside the div:(
    – cevizmx
    Commented Dec 2, 2012 at 10:18
  • @cevizmx - I've updated my post. I had assumed that you were already using the page's loop somewhere. Commented Dec 4, 2012 at 12:35

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.