dtz88967
dtz88967
2014-06-22 15:19

循环内的函数

已采纳
function mm_posts() {
  $result = '';      

  if ( have_posts() ) :
    query_posts('cat=5&posts_per_page=4');
    while ( have_posts() ) : the_post();

    $post_thumbnail_id = get_post_thumbnail_id();
    $post_thumbnail_url = wp_get_attachment_image_src( $post_thumbnail_id, array(300, 200) );

    function mega_thmb() {
      if ( has_post_thumbnail() ) : 
        $img = '<img src="' .  $post_thumbnail_url[0] . '" alt="" class="img-responsive">';
      else :
        $img = '<img src="' .  get_template_directory_uri() . '/img/no-image.gif" alt="" class="img-responsive">';
      endif;

      return $img;
    }

      $result .= '
        <div class="col-md-3">' .
          mega_thmb() .
          '<div>Sport</div>
          <div>Lorem ipsum dolor sit amet</div>
        </div>';
    endwhile;
    wp_reset_query();
  endif;
  return $result;
}

Php displays error : Fatal error: Cannot redeclare mega_thmb() (previously declared in ...

What should I do? How write function inside the loop?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongwen7813 dongwen7813 7年前

    . Structure your code as follows. Additionally, each time the loop is run you're declaring the function again, even though it's already in memory. It needs to be outside the loop. Best practice would be to not embed the function at all

    function mega_thmb() {
          if ( has_post_thumbnail() ) : 
            $img = '<img src="' .  $post_thumbnail_url[0] . '" alt="" class="img-responsive">';
          else :
            $img = '<img src="' .  get_template_directory_uri() . '/img/no-image.gif" alt="" class="img-responsive">';
          endif;
    
          return $img;
        }   
    
    function mm_posts() {
      $result = '';      
    
      if ( have_posts() ) :
        query_posts('cat=5&posts_per_page=4');
        while ( have_posts() ) : the_post();
    
        $post_thumbnail_id = get_post_thumbnail_id();
        $post_thumbnail_url = wp_get_attachment_image_src( $post_thumbnail_id, array(300, 200) );
    
    
    
          $result .= '
            <div class="col-md-3">' .
              mega_thmb() .
              '<div>Sport</div>
              <div>Lorem ipsum dolor sit amet</div>
            </div>';
        endwhile;
        wp_reset_query();
      endif;
      return $result;
    }
    

    I suggest reading the PHP documents, and consulting a few tutorials before moving any further. There are probably some other changes that need to be made to your code to make it completely correct, but please don't define functions inside of other functions, especially inside loops. Read the manual.

    点赞 评论 复制链接分享
  • dongting3135 dongting3135 7年前

    Every time the function mm_posts runs, you are defining a function called mega_thmb. But in PHP, there can only be one function with a particular name (apart from methods in different classes), so this causes the error you are seeing.

    You are trying to do this because you think it will give you access to the variable $post_thumbnail_url, but that's not how PHP works either - a function can only see the variables passed to it as parameters, or included from global scope with the global keyword. Anything else is a local variable whose value will start off as null.

    There are two solutions I can see:

    • Define a normal function mega_thumb (what do you have against that u?) which is passed $post_thumbnail_url as a parameter.
    • Put the code that's currently in the broken mega_thmb straight into mm_posts, because you only use it in one place anyway, and it's only a couple of lines long.
    点赞 评论 复制链接分享

为你推荐