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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀