具有不同页面和帖子内容的页面模板

时间:2012-12-01 作者:cevizmx

我正在使用一个wordpress主题,它有一个名为roomtypes. 我不是一个php专业人士,但据我所知,这个模板得到了帖子。添加房间类型时,它将创建为带有post_type=roomtype.
因此,此模板获取这些帖子,添加它们的标题和缩略图。

我的问题是,当我创建一个页面并输入一些文本时,它在我选择此模板时不会显示<通过一些挖掘,我发现the_content(); 函数起作用。我试图添加它,但效果很好:)
问题是,我无法在需要的地方使用内容。代码如下:

<?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();?>
我需要内容在代码中指示的位置,但是当我添加

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

  <?php the_content(); ?>

 <?php endwhile; endif; ?>
</div>
事情变得一团糟
如果我只添加the_content(); 更糟糕的是,所有这些帖子的内容都显示出来了:(
我如何才能只添加我使用页面模板的页面内容?

2 个回复
SO网友:brasofilo

the_content() (和其他许多人)必须inside The Loop.

要在循环外部检索帖子/页面/自定义帖子类型数据,请查看此处:
Get post content from outside the loop

工作规范,based on your example, 将是:

<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\' ); 
        }           
        ?>

SO网友:stealthyninja

使用get_the_content() 将页面内容存储到变量($page_content) 然后你可以在任何需要的地方使用它。如果在帖子的循环中使用它,请使用if ( $i == 0 ) echo $page_content 所以它只会显示一次:

<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\' ); 
        }           
        ?>

结束

相关推荐