Php 避免使用foreach重复同一函数3次

Php 避免使用foreach重复同一函数3次,php,Php,我试图在WordPress仪表板上显示3种帖子。所有这些都可以正常工作,但我如何避免重复3次“相同”函数和WordPress挂钩,而使用foreach: function recent_videos_dashboard() { ?> <ul> <?php global $post; $posts = get_posts(array( 'post_type' => 'videos',

我试图在WordPress仪表板上显示3种帖子。所有这些都可以正常工作,但我如何避免重复3次“相同”函数和WordPress挂钩,而使用
foreach

function recent_videos_dashboard() {
?>
   <ul>
     <?php
        global $post;

        $posts = get_posts(array(
            'post_type' => 'videos',
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach; ?>
  </ul>
<?php
}

function add_recent_videos_dashboard() {
    wp_add_dashboard_widget( 'recent_videos_dashboard', __( 'Vidéo(s) récente(s)' ), 'recent_videos_dashboard' );
}

if ( !current_user_can( 'manage_options' ) ) {
    add_action('wp_dashboard_setup', 'add_recent_videos_dashboard' );
}

function recent_posts_dashboard() {
?>
   <ul>
     <?php
        global $post;

        $posts = get_posts(array(
            'post_type' => 'post',
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach; ?>
  </ul>
<?php
}

function add_recent_posts_dashboard() {
    wp_add_dashboard_widget( 'recent_posts_dashboard', __( 'Article(s) récente(s)' ), 'recent_posts_dashboard' );
}

if ( !current_user_can( 'manage_options' ) ) {
    add_action('wp_dashboard_setup', 'add_recent_posts_dashboard' );
}

function recent_podcasts_dashboard() {
?>
   <ul>
     <?php
        global $post;

        $posts = get_posts(array(
            'post_type' => 'podcasts',
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach; ?>
  </ul>
<?php
}

function add_recent_podcasts_dashboard() {
    wp_add_dashboard_widget( 'recent_podcasts_dashboard', __( 'Podcast(s) récente(s)' ), 'recent_podcasts_dashboard' );
}

if ( !current_user_can( 'manage_options' ) ) {
    add_action('wp_dashboard_setup', 'add_recent_podcasts_dashboard' );
}

我能得到一些帮助吗?

接受给定的代码,但改为使用匿名函数和稍微不同的帖子类型数组(包括每个帖子类型的名称字符串,以便我们可以为每个帖子类型使用不同的名称字符串),下面是代码的尝试。我没有测试方法,因此请谨慎使用-并让我知道我的错误:

// do stuff here only if it is an 'ordinary' sort of user
if ( !current_user_can( 'manage_options' ) ) :

  $my_posts = [
    [ 'type' => 'post', 'name' => 'Article(s) récente(s)'],
    [ 'type' => 'videos', 'name' => 'Vidéo(s) récente(s)'],
    [ 'type' => 'podcasts', 'name' => 'Podcast(s) récente(s)']
  ];

  foreach ( $my_posts as $my_post ) :

    add_action( 'wp_dashboard_setup', function() {
      wp_add_dashboard_widget('recent_' . $my_post['type'] . '_dashboard', __( $my_post['name'] ), function () {    
?>
   <ul>
<?php
        global $post;

        $posts = get_posts(array(
            'post_type' => $my_post['type'],
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
<?php   endforeach; ?>
   </ul>
<?php       
        }//end of callback function given to wp_add_dashboard_widget call
      );// end of call to wp-add-dashboard_widget
    } //end of callback function given to add_action call
    
    );//end of add_action
    
  endforeach;//end of going through each type of my_posts
  
endif;//finished doing stuff for ordinary user
//仅当它是“普通”类型的用户时才在此处执行操作
如果(!当前用户可以(‘管理选项’):
$my_posts=[
['type'=>'post','name'=>'文章摘要],
[“输入”=>“视频”,“名称”=>“视频中心”,
['type'=>'Podcast','name'=>'Podcast(s)récente(s)'
];
foreach($myu post作为$myu post):
添加操作('wp_仪表板设置',函数(){
wp_add_dashboard_widget('recent_u.$my_post['type'].'u dashboard','uu('my_post['name']),函数(){
?>

我不明白这个问题。避免重复什么?函数名?调用将\u操作添加到wp\u dashboard\u设置挂钩?抱歉,我更新了我的问题。为了避免重复同一个函数3次并使用foreach而不是foreach,这不是错误的解决方案,但要注意的是,在新方法中,您将添加相同的字符串(s)雷森特(s)“,到每个帖子类型的仪表板。@AHaworth你是对的。我相信WordPress钩子中的第二个参数必须是函数。我不知道如何为第二个参数设置函数。我认为使用匿名函数可能是一个好方法,除此之外,它们不会扰乱命名空间/可能导致naming冲突,假设你不想让这些函数做任何其他事情。我会在一个小时左右给出一个可能的答案。非常感谢。我为每个函数添加了
使用($array\u value)
来获取范围内的变量。它工作得非常好!再次感谢你。
// do stuff here only if it is an 'ordinary' sort of user
if ( !current_user_can( 'manage_options' ) ) :

  $my_posts = [
    [ 'type' => 'post', 'name' => 'Article(s) récente(s)'],
    [ 'type' => 'videos', 'name' => 'Vidéo(s) récente(s)'],
    [ 'type' => 'podcasts', 'name' => 'Podcast(s) récente(s)']
  ];

  foreach ( $my_posts as $my_post ) :

    add_action( 'wp_dashboard_setup', function() {
      wp_add_dashboard_widget('recent_' . $my_post['type'] . '_dashboard', __( $my_post['name'] ), function () {    
?>
   <ul>
<?php
        global $post;

        $posts = get_posts(array(
            'post_type' => $my_post['type'],
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
<?php   endforeach; ?>
   </ul>
<?php       
        }//end of callback function given to wp_add_dashboard_widget call
      );// end of call to wp-add-dashboard_widget
    } //end of callback function given to add_action call
    
    );//end of add_action
    
  endforeach;//end of going through each type of my_posts
  
endif;//finished doing stuff for ordinary user