代码之家  ›  专栏  ›  技术社区  ›  sayayin

模板中使用的变量/代码,以便页面内容显示在现场

  •  0
  • sayayin  · 技术社区  · 6 年前

    学习一些wordpress,创建我的第一个自定义wordpress站点。我为我的主页创建了模板,然后我想为一大堆子页面创建一个模板。所以我创建了一个文件page-sub.php。在页面中我可以看到这个模板,所以我把它分配给了我的子页面。但我缺少显示使用模板的页面内容的代码/变量。你们能告诉我需要什么吗?下面是到目前为止我在模板中的代码。

    <?php
     /*
       Template Name: Sub Page Template
     */
    
      get_header();
    ?>
    
    <div class="main">
      <div class="container">
    
         <?php
            if ( is_singular() ) :
                the_title( '<h1 class="entry-title">', '</h1>' );
            endif;
    
            the_content();
         ?>
    
       </div>
    </div>
    <?php 
        get_footer();
    ?>
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   itivae    6 年前

    我想你需要使用Wordpress Post循环。

    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            //
            // Post Content here
            //
        } // end while
    } ?>
    

    也许这就是你要找的。 https://codex.wordpress.org/The_Loop

    就您的代码而言,我可能会让您在PHP之外使用HTML。 例如,我认为你想做的就是这个。

    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            <h1 class="entry-title"><?php the_title(); ?></h1>
            <?php the_content(); ?>
        } 
    } ?>
    

    我不完全理解你为什么使用单数if语句。你有什么特别想做的吗? 像这样?

    <?php 
        if ( have_posts() ) {
            while ( have_posts() ) {
                the_post(); 
                if ( is_singular() ) : 
                    the_title( '<h1 class="entry-title">', '</h1>' ); 
                endif; 
                the_content(); 
            } 
        }
    ?>