double0201 2012-03-09 11:00
浏览 14
已采纳

一页上有多个循环

I need 3 different queries in one page.

When I do it I get an error like this:

Cannot redeclare filter_where() (previously declared in W:\home\zerk\www\wp-content\themes ewss\most_commented.php:19) in W:\home\zerk\www\wp-content\themes ews\most_commented.php on line 41

Here is my code:

<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条回答 默认 最新

  • dongtanzhu5417 2012-03-09 11:09
    关注

    The problem is given in the error message very clearly.

    Cannot redeclare filter_where()

    You can't re-declare the function filter_where - try this. Notice the functions are given unique names.

    • filter_where
    • filter_where2
    • filter_where3

    This is true in all PHP, you can't have more than one function with the same name.

    <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>
    

    That said, the code has lots of other problems too - I would suggest reading a basic introduction to PHP.

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

    The whole idea of a function is one of encapsulation, that is, you write the code once - then call it when you need that functionality.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等