問題描述

我一直遇到的情況是,一個組織可能有幾個工作人員,我想要某種類型的列表和一個具有履歷信息的個人資料頁面。

通常我會為員工創建一個自定義的職位類型,如果需要,也可以自定義分類。

但是現在我想知道在 Wordpress 中使用內置 “user” post 類型可能不是最佳選擇。我意識到我可以自定義用户配置文件字段,顯示用户列表,單個配置文件等自定義分類是可能的,我相信。

這裏有最好的做法嗎?

我現在有一個案子,所有的工作人員也在自己的名字下寫博客文章,所以有一個用户帳户反正,我想到,也許我最好只是清理他們的用户個人資料,並使用 author.php 而不是使用自定義帖子類型的’staff’ 。

現在,我一直在使用 CPT,並使用帖子 2 帖子插件將他們的”staff” 帖子與他們的”user” 帳户相關聯,從而在他們的單人員頁面上創建他們的博客文章的列表。

任何關於如何最好地在 wordpress 中實現這一點的想法是讚賞。

最佳解決方案

如果您想在網站上公開展示的人是用户,即有一個帳户並撰寫帖子,在我看來,使用 WordPress 用户功能會更好一些:您將放在 CPT 中的所有信息也可以放入用户元數據,並且創建用户是強制性的 (他們必須登錄),而創建 CPT 可以避免,對我而言是多餘的。

但是,我知道使用 CPT 可能更簡單,原因如下:

  1. WP 管理員的默認配置文件頁面很少。

  2. 在 WP 中,根本沒有公開的個人資料頁面:author.php 不是個人資料頁面。

  3. 除了個人資料頁面,您可能想要循環使用員工,當然您也可以使用 WP_User_Query 來完成此操作,但是將工作人員與隱藏的用户隔離可能會有點困難:沒有用户分類和使用用户角色如果要將公共角色分配給不能公開顯示的任何用户,可能會產生問題。

幸運的是,這些問題不是真正的問題,可以很容易地解決。我建議的工作流程是:

  1. 創建一個新的用户角色。您可以從標準角色克隆功能,但創建角色,並將員工與其他用户隔離,將是 super-easy 。

  2. 添加用户配置文件的自定義字段,並放入所需的所有信息。

  3. 創建一個可以處理用户循環和用户配置文件的頁面模板。怎麼樣?看點 4 。

  4. 創建重寫端點。以這種方式,像 example.com/staff 這樣的 URL 將調用一個頁面 (您分配在 3. 上創建的模板),並且像 example.com/staff/user/nickname 這樣的 URL 將調用相同的頁面,但是通過可以在頁面中使用的值為 nickname 的查詢 var user 顯示用户配置文件。

1.,2. 和 4. 可以很容易地在插件中完成。我會給你這個插件的骨頭,應該改進:

<?php
/**
 * Plugin Name: Staff Plugin
 * Description: Test
 * Author: G.M.
*/

/**
* Add a new role cloning capabilities from editor and flush rewrite rules
*/
function install_staff_plugin() {
    $editor = get_role( 'editor' );
    add_role( 'staff', 'Staff', $editor->capabilities );
    staff_plugin_endpoint();
    flush_rewrite_rules();
}

/**
* Remove the role and flush rewrite rules
*/
function unistall_staff_plugin() {
    remove_role( 'staff' );
    flush_rewrite_rules();
}

/**
* Add the endpoint
*/
function staff_plugin_endpoint() {
    add_rewrite_endpoint( 'user', EP_PAGES );
}

/**
* Add custom field to profile page
*/
function staff_plugin_profile_fields( $user ) {
    $fields = array(
        'facebook' => __('Facebook'),
        'twitter'  => __('Twitter'),
        'photo_id' => __('Photo ID (use attachment id)')
    );
    echo '<h3>' . __('Staff Information') . '</h3>';
    echo '<table class="form-table">';
    foreach ( $fields as $field => $label ) {
        $now = get_user_meta( $user->ID, $field, true ) ? : "";
        printf( '<tr><th><label for="%s">%s</label></th>',
            esc_attr($field), esc_html($label) );
        printf( '<td><input type="text" name="%s" id="%s" value="%s" class="regular-text" /><br /></td></tr>',
            esc_attr($field), esc_attr($field), esc_attr($now) );
    }
    echo '</table>';
}

/**
* Save the custom fields
*/
function staff_plugin_profile_fields_save( $user_id ) {
    if ( ! current_user_can( 'edit_user', $user_id ) ) return;
    $fields = array( 'facebook', 'twitter', 'photo_id' );
    foreach ( $fields as $field ) {
        if ( isset( $_POST[$field] ) )
            update_user_meta( $user_id, $field, $_POST[$field] );
    }
}

add_action( 'init', 'staff_plugin_endpoint' );
add_action( 'show_user_profile', 'staff_plugin_profile_fields' );
add_action( 'edit_user_profile', 'staff_plugin_profile_fields' );
add_action( 'personal_options_update', 'staff_plugin_profile_fields_save' );
add_action( 'edit_user_profile_update', 'staff_plugin_profile_fields_save' );
register_activation_hook( __FILE__, 'install_staff_plugin' );
register_deactivation_hook( __FILE__, 'unistall_staff_plugin' );

插件完全符合我的説法。關於為用户配置文件添加自定義字段,作為示例,我只添加了 3 個字段。其中一個用於用户圖像,並接受附件的 ID 。當然在現實世界中,最好是調用媒體上傳者,讓用户選擇上傳圖像,但這不在本答案的範圍內。

保存並激活插件後,我們必須創建頁面模板,創建一個頁面並分配該模板。再次,我將在這裏發佈一個模板的概念證明:

<?php
/**
 * Template Name: Staff Page
*
*/

get_header(); ?>

<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">

<?php
/* The page content */
while ( have_posts() ) : the_post();
    $page_link = get_permalink();
    the_content();
endwhile;

$required_user = get_query_var( 'user' );

$wanted_meta = array(
    'first_name', // This is a standard meta
    'facebook',   // This is an example of custom meta
    'twitter'     // This is another example of custom meta
);

if ( empty( $required_user ) ) {

    /* The Users Loop */

    // Customize the args as you need
    $args = array (
        'role'    => 'Staff',
        'orderby' => 'post_count',
        'order'   => 'DESC',
        'fields'  => 'all'
    );
    $user_query = new WP_User_Query( $args );
    if ( ! empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
            $profile_url = trailingslashit($page_link) . 'user/' . $user->user_nicename;
            // This gets ALL the meta fields as a 2 dimensional array (array of arrays)
            $meta_fields = get_user_meta( $user->ID );
            ?>
            <div id="user-<?php echo $user->ID ?>">
            <?php
            // An example of custom meta where to save the id of an attachment
            if ( isset($meta_fields['photo_id'][0]) && ! empty($meta_fields['photo_id'][0]) ) {
                echo '<a href="http://'%20.%20esc_url($profile_url)%20.%20'/">';
                echo wp_get_attachment_image( $meta_fields['photo_id'][0], 'medium' );
                echo '</a>';
            }
            ?>
            <h2><?php echo '<p><a href="http://'%20.esc_url(%20$profile_url%20)%20.%20'/">' .
                $user->display_name . '</a></p>';?></h2>
            <p><?php echo $meta_fields['description'][0]; ?></p>
            <ul>
            <?php
            foreach ( $wanted_meta as $key ) {
                if ( isset($meta_fields[$key][0]) && ! empty($meta_fields[$key][0]) ) {
                    ?>
                    <li><?php echo $meta_fields[$key][0]; ?></li>
                <?php }
            } ?>
            </ul>
            </div>
            <?php
        }
    }

} else {

    /* One User Requested */

    $user = get_user_by( 'slug', $required_user );
    if ( $user ) {
        ?>
        <div id="user-<?php echo $user->ID ?>">
        <?php
        $meta_fields = get_user_meta( $user->ID );
        if ( isset( $meta_fields['photo_id'][0] ) && ! empty( $meta_fields['photo_id'][0] ) ) {
            echo wp_get_attachment_image( $meta_fields['photo_id'][0], 'full' );
        }
        ?>
        <h1><?php echo '<p>' . $user->display_name . '</p>';?></h1>
        <p><?php echo $meta_fields['description'][0]; ?></p>
        <p>
            <a href="http://<?php%20echo%20get_author_posts_url($user->ID);%20?>"><?php
                printf(__('See all posts by %s'), $user->display_name); ?></a> |
            <a href="http://<?php%20echo%20$page_link;%20?>"><?php _e('Back to Staff'); ?></a>
        </p>
        <ul>
        <?php
        foreach ( $wanted_meta as $key ) {
            if ( isset( $meta_fields[$key][0] ) && ! empty( $meta_fields[$key][0] ) ) {
                ?>
                <li><?php echo $meta_fields[$key][0]; ?></li>
                <?php
            }
        } ?>
        </ul>
        </div>
        <?php
    }
}
?>

</div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

現在創建一個頁面並分配此模板。然後將用户角色’staff’ 分配給您的員工並填寫配置文件。

作為最後的觸摸,在您的 author.php 中,您可以添加,可能在標題中,如下所示:

<div class="author-info">
    <?php
    $curauth = ( get_query_var( 'author_name' ) ) ?
        get_user_by( 'slug', get_query_var( 'author_name' ) ) :
        get_userdata( get_query_var( 'author' ) );
    $photo = get_user_meta( $curauth->ID, 'photo_id', true );
    if ( $photo ) echo wp_get_attachment_image( $photo, 'medium' );
    ?>
    <h2><?php echo $curauth->display_name; ?></h2>
    <h3><em><?php echo $curauth->user_description; ?></em></h3>
</div>

就這樣。測試它,改善它,並樂趣與它。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。