[英]Wordpress Custom Fields. Display custom fields from user profile on a custom page
在我的wordpress博客上,我具有允許登錄用戶能夠選擇他們希望在博客頁面上以流形式查看的帖子類別的功能。 因此,每個用戶只能看到他們訂閱的類別中的帖子,而不是所有類別。
我用了這段代碼。
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th>
<label for="post_category">Display Categories</label><br />
<span class="description">Which categories would you like to see posts from?</span>
</th>
<td>
<ul class="categorychecklist form-no-clear">
<?php wp_terms_checklist( 0, array( 'checked_ontop' => false, 'taxonomy' => 'category', 'selected_cats' => get_user_meta( $user->ID, 'post_category', true ) ) ) ?>
</ul>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if( !isset( $_POST['post_category'] ) )
return false;
if( !is_array( $_POST['post_category'] ) )
return false;
update_usermeta( $user_id, 'post_category', array_map( 'absint',
$_POST['post_category'] ) );
}
add_action( 'pre_get_posts', 'set_user_categories', 1000 );
function set_user_categories( $wp_obj ) {
if( !is_front_page() )
return;
if( 'nav_menu_item' == $wp_obj->query_vars['post_type'] )
return;
global $current_user;
$user_cats = get_user_meta( $current_user->ID, 'post_category', true );
if( !$user_cats )
return;
$wp_obj->set( 'category__in', $user_cats );
}
這就是結果。
這段代碼可以正常工作,但是,我希望這些選項顯示在另一個頁面而不是用戶配置文件上,因此我專門創建了另一個頁面模板來處理此問題。
<?php
/*
Template Name: Subscribe
*/
?>
<?php get_header() ?>
<div class="page-container">
<div><?php the_field('my_show_extra_profile_fields'); ?></div>
<?php get_footer(); ?>
如您所見,我嘗試顯示“ my_show_extra_profile_fields”字段,但未顯示。
如何使這些選項顯示在我的頁面模板上而不是用戶個人資料上?
試試這個代碼。 extra_field_name可能是“ post_category”
global $current_user;
echo get_user_meta( $current_user->ID, 'extra_field_name', true );
像
echo get_user_meta( $current_user->ID, 'post_category', true );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.