【问题标题】:Why do wordpress theme loops in a single page为什么 wordpress 主题在单个页面中循环
【发布时间】:2014-07-21 11:19:35
【问题描述】:

我是 wordpress 和 wordpress 循环的新手。我试图理解循环但没有任何成功。贝尔和我一起,我会尽力解释我不明白的......

我使用了一个名为“graphy”的模板。当我创建一个“页面”时,可以选择创建一个没有侧边栏的页面,模板称为“nosidebar.php”,这是它的代码:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

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

            <?php get_template_part( 'content', 'page' ); ?>

            <?php
                // If comments are open or we have at least one comment, load up the comment template
                if ( comments_open() || '0' != get_comments_number() ) :
                    comments_template();
                endif;
            ?>

        <?php endwhile; // end of the loop. ?>

    </main><!-- #main -->
</div><!-- #primary -->

1- 为什么这个模板包含一个循环?它只显示没有侧栏的单个页面内容!显然它不是循环浏览帖子并显示它们!

我尝试创建自己的模板页面,该页面仅用于首页,这就是我想出的

<?php
/**
 * Template Name: Main_Page
 * Description: A page template without sidebar.
 *
 * @package Graphy
 */
get_header();
?>

<!--<div id="primary" class="content-area">-->
<!--<main id="main" class="site-main" role="main">-->
<div id="main_content">
<?php 
    the_content(); // the content  
?>
</div>
<!--</main> #main -->
<!--</div> #primary -->

<?php get_footer(); ?>

但是,当我安装 this plugin 时,它用于将小部件插入页面和帖子 使用 main_page 没有显示小部件,但是当我切换到“无侧边栏页面”时它起作用了。 然后我将循环复制到我的主页中并且它起作用了。

2- 这个循环使插件工作的秘密是什么,而只调用&lt;?php the_content() ?&gt; 不能?!显然,这个循环除了互联网上 90% 的帖子所解释的内容之外,还做了一些其他的事情。

【问题讨论】:

标签: php wordpress


【解决方案1】:

在您的第一个问题上,页面模板会输出信息,这就是您看到循环的原因。显示的此信息是在 tinymce 编辑器内的页面编辑器屏幕中输入的信息。

为了更好地理解,请阅读我最近在 WPSE 上完成的这两篇文章

关于问题二,the_content() 不输出侧边栏和小部件,而是输入到帖子编辑器中的内容。侧边栏显示特定呼叫。

你需要去看看你的主题是如何注册侧边栏的。我还怀疑您的侧边栏的行为是由body_classes 操纵的。不幸的是,我帮不上忙,因为这对您的主题非常具体

【讨论】:

    【解决方案2】:

    这都是关于 the_post(); 的,那里不需要任何 while 循环。 试试这个

    <?php
    the_post();
    the_content();
    ?>
    

    它会起作用的。 the_post() 是检索帖子内容的函数。 仅当从类别中调用检索时才需要 while 循环。

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多