一页上有多个循环

时间:2012-03-09 11:00:04

标签: php wordpress loops

我在一个页面中需要3个不同的查询。

当我这样做时,我收到如下错误:

无法在W:\ home \ zerk \ www \ wp-中重新声明filter_where()(之前在W:\ home \ zerk \ www \ wp-content \ themes \ newss \ most_commented.php:19中声明)第41行的content \ themes \ news \ 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>

1 个答案:

答案 0 :(得分: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