簡體   English   中英

避免使用 foreach 重復 3 次相同的函數

[英]Avoid repeating 3 times the same function with a foreach

我正在嘗試在 WordPress 儀表板上顯示 3 種帖子。 一切正常,但我怎樣才能避免重復“相同”函數和 WordPress 鈎子 3 次,並使用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' );
}

所以我改用了foreach但我得到了一個錯誤:

警告:call_user_func_array() 期望參數 1 為有效回調,未找到函數“add_recent_videos_dashboard”或無效的函數名稱

$array_post_types = array('post','videos','podcasts');

foreach ($array_post_types as $array_post_type) {
    $recent_post = 'recent_' . $array_post_type . '_dashboard';

    ${$recent_post} = function() use ($array_post_type) {
        global $post;

        ${'array_' .$array_post_type} = get_posts(array(
            'post_type' => $array_post_type,
            'numberposts' => 5
        ));

        foreach( ${'array_' .$array_post_type} 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;
    };

    $add_recent_posts = 'add_recent_' . $array_post_type . '_dashboard';

    ${$add_recent_posts} = function () use ($array_post_type) {
        wp_add_dashboard_widget( $recent_post, __( 'Vidéo(s) récente(s)' ), $recent_post );
    };

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

我可以幫忙嗎?

使用給定的代碼但更改為使用匿名函數和稍微不同的帖子類型數組(包括每個帖子類型的名稱字符串,以便我們可以為每種帖子類型使用不同的名稱),這是對代碼的一種嘗試。 我沒有辦法測試這個,所以請謹慎使用 - 讓我知道我有什么問題:

// 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_ 也作為前綴放置 - 它可以更改為您的函數/插件的某些唯一前綴,以確保與其他代碼不發生沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM