douyanpeng0748
2014-02-20 06:59
浏览 39
已采纳

在wordpress中的functions.php中创建一个循环

I am trying to create a loop that will work as a custom navigation menu in the header using custom post type manager. I am creating a function in the functions.php and then calling the function in the header.php. I can't use the regular " while : have posts etc.." because it actually changes the page content. I just want to create a function that will bring up the image, custom fields, etc..

Here is my code that doesn't work:

 <?php
    // Our Team Navigation Menu

    function our_team_arg( $arg2 ) {

    $arg2 = array('posts_per_page' => 60, 'post_type' => 'our_team');

       query_posts($arg2);

    $myposts = get_posts( $arg2 );

    foreach ( $myposts as $post ) : setup_postdata( $post );  
    ?>
     <div class="founderblk">
       <a href="<?php the_permalink(); ?>">
           <img alt="<?php the_title(); ?>" src="<?php print_custom_field('team_member_image:to_image_src'); ?>">
       </a><br />
      <span class="foundertitle"><?php print_custom_field('team_member_title'); ?></span>
     </div>
  <?php
  endforeach; 

    wp_reset_postdata();
 }
   // END Our Team Navigation Menu
?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglin9717 2014-02-20 08:01
    已采纳

    you can use wp_query and use while normally it will not change the page content when you are using wp_reset_postdata(); and query_posts actually call wp_query internally here is an example

     $args = array('posts_per_page' => 60, 'post_type' => 'site-product');
      // The Query
    $custom_query = new WP_Query( $args );
    
    if ( $custom_query->have_posts() ) { 
    
      while ( $custom_query->have_posts() ) { $custom_query->the_post(); ?>
    
       <div class="founderblk">
           <a href="<?php the_permalink(); ?>">
               <img alt="<?php the_title(); ?>" src="<?php print_custom_field('team_member_image:to_image_src'); ?>">
           </a><br />
          <span class="foundertitle"><?php print_custom_field('team_member_title'); ?></span>
         </div>
    <?php 
     }
    
    } else {
      // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    for the second line in your code

    $arg2 = array('posts_per_page' => 60, 'post_type' => 'our_team');
    

    I assume that you write it for testing because it is actually overwrite the function arguments

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题