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

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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用