首页 > 解决方案 > 如何在wordpress中获取页面内容?

问题描述

我正在建立一个名为 About us 的页面,并且永久链接为:http: //virtual1.com/index.php/about-us

我也查询获取页面内容,但我得到的是空白(它没有显示最后修改页面的日期,也没有显示作者,内容段落)这是代码:

<?php 
$args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'page',
    'slug' => 'about-us'
);
$the_query = new WP_Query( $args );

?>
<?php get_header(  ); ?>
<?php
while ( $the_query->have_posts() ): $the_query->the_post();?>
<!--Main body-->
<div id="content">
      <div class="container">
          <div class="row">
            <div class="content-inner">
                            <!-- Left sidebar -->
             <div id="component" class="col-sm-12">
             <main role="main">

              <div id="system-message-container">
              </div>

<article class="page-item page-item__blog page-item__" itemscope="">
    <div class="item_info">
    <dl class="item_info_dl">
        <dt class="article-info-term">
        </dt>
            //Get the date last modified of the page
            <dd>
                <time datetime="2018-02-26 10:14" class="item_published">
                    <?php get_the_modified_date(  ); ?>     
                </time>
            </dd>
            <dd>
            //Get the author of the page
            <address class="item_createdby">
            <?php get_the_author(  ); ?>            
            </address>
        </dd>
    </dl>
</div>

//Get the paragraph in the page here
<div class="item_fulltext">
    <p>
        <?php echo $content?>
    </p>    
</div>
<!--End loop-->
<?php endwhile; ?>
<!--End of main body-->
<?php get_footer(  ); ?>

标签: phpwordpress

解决方案


在page.php中编写代码,代替$content 函数the_content()将完成您的工作。

<?php 
      get_header(); 
      $page_id=get_the_ID();
      $the_query = new WP_Query( array( 'p' => $page_id ) );

      while ( $the_query->have_posts() ): $the_query->the_post();
?>
       <div id="content">
      <div class="container">
          <div class="row">
            <div class="content-inner">
                            <!-- Left sidebar -->
             <div id="component" class="col-sm-12">
             <main role="main">

              <div id="system-message-container">
              </div>

<article class="page-item page-item__blog page-item__" itemscope="">
    <div class="item_info">
    <dl class="item_info_dl">
        <dt class="article-info-term">
        </dt>
            //Get the date last modified of the page
            <dd>
                <time datetime="2018-02-26 10:14" class="item_published">
                    <?php get_the_modified_date(); ?>     
                </time>
            </dd>
            <dd>
            //Get the author of the page
            <address class="item_createdby">
            <?php get_the_author(); ?>            
            </address>
        </dd>
    </dl>
</div>

//Get the paragraph in the page here
<div class="item_fulltext">
    <p>
        <?php the_content(); ?>
    </p>    
</div>
<?php 
      endwhile;
?>

推荐阅读