Reputation: 611
On my wordpress blog I have the functionality that allows users who are logged in to be able to select categories of posts they would like to see as a stream on the blog page. So each user only sees posts from categories they have subscribed to and not all categories.
I used this code.
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 );
}
this is the result.
This code works fine, however, I want these options to appear on another page and not the user profile, so I created another page template specifically for handling this.
<?php
/*
Template Name: Subscribe
*/
?>
<?php get_header() ?>
<div class="page-container">
<div><?php the_field('my_show_extra_profile_fields'); ?></div>
<?php get_footer(); ?>
as you can see, I tried to display the field "my_show_extra_profile_fields" but it doesn't show up.
How can i make these options show on my page template subscribe.php and not the user profile??
Upvotes: 1
Views: 761
Reputation: 2517
Try this code. extra_field_name may be "post_category"
global $current_user;
echo get_user_meta( $current_user->ID, 'extra_field_name', true );
as like
echo get_user_meta( $current_user->ID, 'post_category', true );
Upvotes: 1