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

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?