2012-03-09 120 views
0

我需要一个页面中的3个不同的查询。一个页面上有多个循环

当我这样做,我得到这样一个错误:

不能重新声明filter_where()(以前以W宣称:\家\油枪加油\ WWW \可湿性粉剂内容\主题\ newss \ most_commented.php: 19)以W:\家\油枪加油\ WWW \可湿性粉剂内容\主题\上线41

这里新闻\ most_commented.php是我的代码:

<div id="page-wrap"> 

    <h3>Most commented </h3> 

    <div id="example-five"> 

     <ul class="nav"> 
      <li class="nav-one"><a href="#featured" class="current">Lat day</a></li> 
      <li class="nav-two"><a href="#core">Lat week</a></li> 
      <li class="nav-three"><a href="#jquerytuts">Lat month</a></li> 
     </ul> 

     <div class="list-wrap"> 

      <ul id="featured"> 

<?php 
    function filter_where($where = '') { 
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-1 days')) . "'"; 
    return $where; 
    } 
    add_filter('posts_where', 'filter_where'); 
    query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC'); 
    while (have_posts()): the_post(); ?> 
    <li> 
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php   the_title();  ?></a> 
</li> 
<?php 
endwhile; 
wp_reset_query(); 
?> 

      </ul> 

      <ul id="core" class="hide"> 
<?php 
function filter_where($where = '') { 
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
return $where; 
} 
add_filter('posts_where', 'filter_where'); 
query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC'); 
while (have_posts()): the_post(); ?> 
<li> 
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title();  ?></a> 
</li> 
    <?php 
endwhile; 
wp_reset_query(); 
?> 
      </ul> 

      <ul id="jquerytuts" class="hide"> 
<?php 
function filter_where($where = '') { 
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; 
return $where; 
} 
add_filter('posts_where', 'filter_where'); 
query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC'); 
while (have_posts()): the_post(); ?> 
<li> 
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title();  ?></a> 
</li> 
<?php 
endwhile; 
wp_reset_query(); 
?> 
      </ul> 



</div> 

回答

2

问题是在给定的错误消息非常清楚。

不能重新声明filter_where()

不能重新声明功能filter_where - 试试这个。注意函数被赋予唯一的名字。

  • filter_where
  • filter_where2
  • filter_where3

这是在所有的PHP真的,你不能有相同名称的多个功能。

<div id="page-wrap"> 
    <h3>Most commented </h3> 
    <div id="example-five"> 
     <ul clas="nav"> 
      <li class="nav-one"><a href="#featured" class="current">Lat day</a></li> 
      <li class="nav-two"><a href="#core">Lat week</a></li> 
      <li class="nav-three"><a href="#jquerytuts">Lat month</a></li> 
     </ul> 
    <div class="list-wrap"> 
     <ul id="featured"> 
      <?php 
       function filter_where($where = '') { 
      $where .= " AND post_date > '" . date('Y-m-d', strtotime('-1 days')) . "'"; 
       return $where; 
      } 
       add_filter('posts_where', 'filter_where'); 
       query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC'); 
       while (have_posts()): the_post(); ?> 
      <li> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php   the_title();  ?></a> 
      </li> 
      <?php 
       endwhile; 
       wp_reset_query(); 
       ?> 

     </ul> 

     <ul id="core" class="hide"> 
      <?php 
       function filter_where2($where = '') { 
      $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
       return $where; 
      } 
       add_filter('posts_where', 'filter_where2'); 
       query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC'); 
       while (have_posts()): the_post(); ?> 
      <li> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title();  ?></a> 
      </li> 
      <?php 
       endwhile; 
       wp_reset_query(); 
       ?> 
     </ul> 

     <ul id="jquerytuts" class="hide"> 
      <?php 
       function filter_where3($where = '') { 
      $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; 
       return $where; 
      } 
       add_filter('posts_where', 'filter_where3'); 
       query_posts('post_type=post&posts_per_page=5&orderby=comment_count&order=DESC'); 
       while (have_posts()): the_post(); ?> 
      <li> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title();  ?></a> 
      </li> 
      <?php 
       endwhile; 
       wp_reset_query(); 
       ?> 
     </ul> 
    </div> 

也就是说,代码也有很多其他问题 - 我建议阅读PHP的基本介绍。

http://php.net/manual/en/tutorial.php

功能的整体思路是封装的一个,那就是,你编写一次代码 - 然后调用它,当你需要的功能。

http://www.w3schools.com/php/php_functions.asp

+0

非常感谢你much.I试试这个并没有什么错误,但没有帖子也。我无法理解问题出在哪里。我有帖子有评论,但没有输出的查询。 – Ronin 2012-03-09 11:48:33

+0

就像我说的,代码有很多其他问题 - 阅读关于wordpress中的多个循环 - http://codex.wordpress.org/The_Loop#Multiple_Loops和http://www.catswhocode.com/blog/multiple-wordpress-循环 – Fraser 2012-03-09 13:57:50

相关问题