【问题标题】:Wordpress the_content showing all pagesWordpress the_content 显示所有页面
【发布时间】:2015-08-26 19:40:47
【问题描述】:

所以,我正在处理一个自定义 Wordpress 主题并遇到了一个非常奇怪的问题,但我找不到解决方案。现在,我有两个页面设置了虚拟内容,一个设置为静态首页。但是,这两个页面的内容都被拉到了主页上。

这里是实时网站的链接。 wintonsmotionpictures.com

我一直在将事情简化为基础,试图找出问题所在。这是我的 page.php 文件的内容。

<?php
// Filename: page.php
get_header(); ?>

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

<div class="container">
<?php the_content();
 endwhile; endif; 
?>
</div>  

<?php get_footer(); ?>

这是被吐出到 Firebug 中的 HTML 内容。

<main>
  <section class="wrapper">
    <div class="content-wrap">
      <div class="container">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu est condimentum, dictum nibh vitae, maximus arcu.</p>

        <div class="container">
          <p>Fusce mollis justo vitae porta porta. Proin congue fringilla quam et vehicula. Mauris commodo arcu sit amet neque elementum vestibulum.</p>
        </div>  

      </div>
    </div>
  </section>
</main>

这也是我的 header.php 文件。同样,整个主题都被相当精简了。

    <?php
/*
 Filename: header.php
 Author: Jesse Winton
 */
?>
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php
    /*
     * Print the <title> tag based on what is being viewed.
     */
    wp_title( '|', true, 'right' );

    ?></title>

<!-- stylesheets -->
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,800,700|Merriweather:400,700" rel="stylesheet" type="text/css">
<!-- / stylesheets -->

<!-- scripts -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="<?php bloginfo( 'template_directory' ); ?>/js/modernizr.js" type="text/javascript"></script>
<!-- / scripts -->

<!-- meta tags -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="author" content="Jesse Winton Design"/>
<meta name="robots" content="Index, Follow"/>
<meta name="viewport" content="user-scalable=yes, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />  
<!-- / meta tags -->

<?php wp_head(); ?>

</head>

  <body>

   <?php query_posts(array('post_type'=>'page', 'orderby' => 'menu_order', 'order' => 'ASC')); ?>
                          <?php if(have_posts()): while(have_posts()): the_post(); ?>
                          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                          <?php endwhile; endif; ?> 

    <main>

    <section class="wrapper">
        <div class="content-wrap">

我一辈子都找不到这里的问题。任何想法将不胜感激。

谢谢,

杰西

【问题讨论】:

  • 我不太确定我是否关注了你,但你在page.php 中有一个循环,它会遍历你的所有帖子,并为每个帖子调用the_content。为什么你会期望它只是占据首页?
  • @andrewsi 看到我的编辑,包括一个链接。

标签: php wordpress


【解决方案1】:

你不应该在 page.php 模板上循环。只需拨打the_post() 一次。

<?php
// Filename: page.php
get_header();

the_post(); ?> 

<div class="container">
<?php the_content(); ?>
</div>  

<?php get_footer(); ?>

header.php 中的这个查询阻碍了实际的 post 查询。删除它,您会在相应的位置看到相应的内容:

   <?php query_posts(array('post_type'=>'page', 'orderby' => 'menu_order', 'order' => 'ASC')); ?>
                          <?php if(have_posts()): while(have_posts()): the_post(); ?>
                          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                          <?php endwhile; endif; ?> 

您应该改用WP_Query()wp_reset_postdata();。在此期间,尝试添加 wp_reset_postdata(); 作为 header.php 的最后一行。

【讨论】:

  • 谢谢,但现在它只显示首页的内容,即使在关于虚拟页面上也是如此。同样,它是网站的超级精简版,但链接是 here.
  • 是的,但是.container div 中的实际内容与主页相同。如果您查看我的原始帖子,您会发现每个页面上的内容都不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多