Reply To: profile page sidebar assignment
Hi guys,
you don’t need a plugin to get a contextual sidebar. You can do that with a few conditionnals and a function. Let’s take 2016 as example.
Copy sidebar.php file of your theme to child-theme.
Add an action hook to the template. Ie. xtra_sidebar
Almost done !
In Twenty Sixteen, it looks like this:
<aside id="secondary" class="sidebar widget-area" role="complementary">
<?php do_action( 'xtra_sidebar' ); ?>
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside>
Into bp-custom.php or child theme functions.php you add following function(s)
If you want different custom sidebar content for different groups:
function xtra_group_sidebar() {
if ( bp_is_active( 'groups' ) ) {
// conditionnal for group name (slug format)
if ( bp_is_groups_component() && 'kill-bill' == bp_current_item() ):
// your content
echo '<section id="categories-2222" class="widget widget_categories">';
echo '<h2 class="widget-title">what time is it Bill ? </h2>';
$my_date = date('H:i'); echo $my_date;
echo '</section>';
endif;
if ( bp_is_groups_component() && 'terminator' == bp_current_item() ):
echo '<h3 style="color:red">WHO KNOWS ?</h3>';
endif;
if ( bp_is_groups_component() && 'harry-poter' == bp_current_item() ):
echo 'Hou ouh...';
endif;
}
}
add_action( 'xtra_sidebar', 'xtra_group_sidebar' );
If you want a custom sidebar for profiles only, you use:
function xtra_profile_sidebar() {
if ( bp_is_active('xprofile' ) && bp_is_user() ) {
?><section id="categories-1111" class="widget widget_categories">
<h2 class="widget-title">My profile sidebar</h2>
My sidebar content</section>
<?php
}
}
add_action( 'xtra_sidebar', 'xtra_profile_sidebar' );
function xtra_directory_sidebar() {
if ( bp_is_active('xprofile' ) && bp_is_user() ) {
?>
<section id="categories-1111" class="widget widget_categories">
<h2 class="widget-title">My directory sidebar</h2>
My directory content</section>
<?php
}
}
add_action( 'xtra_sidebar', 'xtra_directory_sidebar' );
You can of course use the same technique for all other BP components.
For profiles, you can even add a user_id check so you can personalize the sidebar for a given user.
Or create a sidebar for visitors and another one for logged_in members… Sky is the limit ! 😉
Note that the div and css names are theme dependant. The example use those of 2016 but you have to adjust that to fit your theme.
Note also that most sidebars use listed content (ul/li). Try to respect that or you might break your theme. Check how it’s done and use the same. If you find ID numbers, use a very high number ! I use 1111, because i will never have 1111 widgets in my sidebar ! 😉
Another way to do that would be to add conditionnals into the sidebar template. For example:
if ( !this_component() ) {
show the original sidebar
} else {
show my xtra sidebar
}
Hope it helps and happy templating ! 🙂