2012-03-09 3 views
0

Мне нужно 3 разных запроса на одной странице.Несколько петель на одной странице

Когда я это делаю, я получаю сообщение об ошибке, как это:

Не можете переобъявить filter_where() (ранее объявленный в W: \ дом \ Zerk \ WWW \ сор-контента \ Themes \ newss \ most_commented.php: 19) в W: \ дом \ Zerk \ WWW \ \ темы сор-контента \ новости \ most_commented.php на линии 41

Вот мой код:

<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

+0

Как я уже сказал, код имеет много других проблем - прочитайте о нескольких циклах в wordpress - http://codex.wordpress.org/The_Loop#Multiple_Loops и http://www.catswhocode.com/blog/multiple-wordpress- петли – Fraser

Смежные вопросы