繁体   English   中英

一页上有多个循环

[英]Multiple loops on one page

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

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

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

问题在错误消息中非常清楚地给出。

无法重新声明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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM