Is it possible to call bp_get_options_nav() for a specific group ? I need to get the same in-group navigation in every post with a defined post type.
Posts are associated with Groups by slugs and both the group has a meta of post id and the post has a meta of group id (Groups were created from posts), and I'm trying to make the navigation between them seamless.
Read This url:-
http://www.generalthreat.com/2011/10/creating-a-buddypress-group-home-page/
https://wordpress.stackexchange.com/questions/58485/adding-navigation-item-page-for-quick-chat-plugin-to-each-of-my-buddypress-group
Or Try
Creating a BuddyPress Group Home Page
Step One: Create an Activity tab for the activity stream, since we’re displacing it.
To do this, we’re going to use parts of the Group Extension API to create a new nav item for the group.
In your theme’s functions.php, add the following:
function add_activity_tab() {
global $bp;
if(bp_is_group()) {
bp_core_new_subnav_item(
array(
'name' => 'Activity',
'slug' => 'activity',
'parent_slug' => $bp->groups->current_group->slug,
'parent_url' => bp_get_group_permalink( $bp->groups->current_group ),
'position' => 11,
'item_css_id' => 'nav-activity',
'screen_function' => create_function('',"bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );"),
'user_has_access' => 1
)
);
if ( bp_is_current_action( 'activity' ) ) {
add_action( 'bp_template_content_header', create_function( '', 'echo "' . esc_attr( 'Activity' ) . '";' ) );
add_action( 'bp_template_title', create_function( '', 'echo "' . esc_attr( 'Activity' ) . '";' ) );
}
}
}
add_action( 'bp_actions', 'add_activity_tab', 8 );
That adds a tab called ‘Activity’ to every group’s navigation bar, pointing to a page called ‘activity’ within the group. This new activity page is calling the same groups/single/home.php template file as the current home page. That template actually handles routing for ALL group pages, as we’ll see in a minute.
Step Two: Create a template file for your group Home page
If you’re going to have a Group home page, you need to control how it’s displayed. To do that, you’ll need a template file in your theme’s groups/single directory. It can be called whatever you’d like, but for this guide, we’re sticking with front.php.
Create this file and put whatever you want in it. This template will be in the “group loop”, so you can use functions like bp_group_description() to display group info.
Here’s an example:
<div class="home-page single-group" role="main">
<?php if(bp_is_item_admin()) { ?>
<div class="notice info">
<p>Welcome to your group home page!<br />
Click Admin above to set the content for this page.</p>
</div>
<?php } ?>
<?php bp_group_description(); ?>
</div>
Up to now, all we’ve done is create a new Activity tab on the group page that doesn’t even show the activity stream. But this last piece will make everything work:
Step Three: Edit your theme’s groups/single/home.php template file
Look for this section in your home.php file:
elseif ( bp_group_is_visible() && bp_is_active( 'activity' ) ) :
locate_template( array( 'groups/single/activity.php' ), true );
elseif ( bp_group_is_visible() ) :
locate_template( array( 'groups/single/members.php' ), true );
This directs a visitor to either the activity stream or the member list, depending on whether activity has been enabled. It’s a catch-all routing section, and it’s where we’ll be adding our group home page.
Change the lines above to:
elseif ( bp_group_is_visible() && bp_is_group_activity() ) :
locate_template( array( 'groups/single/activity.php' ), true );
elseif ( bp_group_is_visible() ) :
locate_template( array( 'groups/single/front.php' ), true );
This enables the Activity tab AND sends requests for the group home page to your new template! Of course, if you used a name other than front.php, you’ll need to change that line to match the name you chose.
Update! Step Four – Let BuddyPress JS know how to classify your new activity posts
Now you can make activity updates, but BuddyPress won’t remember that they came from your group. In order to fix that, we need to relax a check in another file, activity/post-form.php.
<?php elseif ( bp_is_group_home() ) : ?>
<input type="hidden" id="whats-new-post-object" name="whats-new-post-object" value="groups" />
<input type="hidden" id="whats-new-post-in" name="whats-new-post-in" value="<?php bp_group_id(); ?>" />
<?php endif; ?>
Without those hidden fields, BuddyPress thinks we’re just posting a personal status update. So let’s expand that to cover our new Activity page:
<?php elseif ( bp_is_group() ) : ?>
<input type="hidden" id="whats-new-post-object" name="whats-new-post-object" value="groups" />
<input type="hidden" id="whats-new-post-in" name="whats-new-post-in" value="<?php bp_group_id(); ?>" />
<?php endif; ?>
Now BuddyPress will tie the update to a group no matter where in the group we post it.
That’s it! Check out your new group home page and enjoy the results your BuddyPress hacking chops. And if you do something really cool with a group Home page, send a link my way!
Related
I have created multiple different headers as templates with Elementor.
I would like to display all of the different headers based on user role (Logged in/Logged out) and page.
I'm looking for a code snippet that I could easily customize to assign all of the different headers for different scenarios.
Could someone please create an example code snippet that would:
Display header A for Logged Out users on the entire
website, EXCEPT pages X and Y.
Display header B for Logged In users on the entire
website, EXCEPT pages X and Y.
Display header C for Logged Out users only on pages X and
Y.
Display header D for Logged In users only on pages X and
Y.
This way, people can easily copy the code and customize it to fit their needs.
EDIT
There's 2 places where I can create templates.
1st one is added by Elementor and is found in Admin > Templates > Saved Templates. Here I can create either section or page templates (Screenshot).
2nd one is added by my theme, OceanWP, and is found in Admin > Theme Panel > My Library. Here I can create only 1 type of template. The templates created here can later be assigned as custom headers or footers to individual pages or the entire website.
Are the templates created in these 2 places considered to be template parts? Is there a difference where I choose to create the header templates?
Here's a list of the header templates I have created:
Template title
Post ID
A
Main Header (Logged Out)
5448
B
Main Header (Logged In)
6714
C
Checkout Header (Logged Out)
6724
D
Checkout Header (Logged In)
3960
Here's the page I want to have a different header than the entire website:
Page title
Post ID
Slug
X
Checkout
18
checkout
Something like this should work:
<?php
if (! is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header A
}
if (is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header B
}
if (! is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header C
}
if (is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header D
}
?>
The following offer the same result as the previous answer but is minified and has less repetitiveness.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
if ( is_user_logged_in() )
get_header( 'B' ); //... header-B.php
else
get_header( 'A' ); //... header-A.php
else
if ( is_user_logged_in() )
get_header( 'D' ); //... header-D.php
else
get_header( 'C' ); //... header-C.php
?>
Following your comments
I'm guessing that what you refer as...
section templates
...are in fact templates part. Instead of using get_header( string $name ); you would then use get_template_part( string $slug, string $name = null );.
$slug and $name can be anything that you chose.
Source # https://developer.wordpress.org/reference/functions/get_template_part/
For example, section-A.php would be get_template_part( 'section', 'A' );.
<?php
//...
if ( is_user_logged_in() )
get_template_part( 'section', 'B' ); //... section-B.php
else
get_template_part( 'section', 'A' ); //... section-A.php
?>
In regards to specifying pages and templates. is_page() can take IDs, slugs or titles.
is_page( int|string|int[]|string[] $page = '' )
Parameter
Description
$page
(int|string|int[]|string[]) (Optional) Page ID, title, slug, or array of such to check against. Default value: ''
Source # https://developer.wordpress.org/reference/functions/is_page/
But you could also use other is_ function like is_search() or is_archives(), is_404()... etc.
Here is a complete list # https://codex.wordpress.org/Conditional_Tags
If you want to add multiple conditional statement you can just add elseif statements in-between.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
//...
elseif ( is_search() || is_archive() )
//...
else
//...
?>
If you want to get a better understanding of PHP operators, which are how conditional statements are built, take a look # https://www.w3schools.com/php/php_operators.asp
I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.
Actually i've a static website and i'm trying to convert it into wordpress, i'm new to wordpress. I have successfully created following page.
header.php
footer.php
sidebar.php
functions.php
index.php
Then i have created a main-template.php page for my other pages like about, contact, gallery, etc, They will all be using same template. The website is almost completed but only two pages are left.
1. Jobs
2. News & Events
So in these pages, There will be new jobs coming, and same for news & events. So i think these two pages will be like a blog post.
Jobs page will display all the jobs with title and 100 words description and read more button, Then it will open in new page for that particular job.
I have will different job categories like electrician, plumber,mason,english language,call center etc
I want to show all categories jobs on Jobs page.
So I have created a Page in wp-admin>pages>add new called Jobs and newsEvents and selected template main-template that i have used for all pages. But this page will be blog type to show all jobs.
As you know I will be having two blog type pages one for JObs and one for News Events So I have only created two categories Jobs & NewsEvents
Now If i add a new job, I will go to wp-admin>posts>add New then add title, description, select category Job if it is job, similarly for newsevents.
So the question is How do i show jobs on jobs page and newsevents on newsevents page?
How to create single.php?
How many single.php do i have to create?
I have created `single.php`
<?php
/**
* The template for displaying Jobs
*/
/*get_header();*/
get_header();
?>
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', 'Jobs' );
endwhile;
?>
<?php
get_footer();
?>
and I have created Empty jobs page in wp-admin>pages>Job its link is localhost/MyProject/jobs I want to show jobs on this page, all jobs that i will post..
I have created content-jobs.php page too, Now what should i do next? how will i show jobs?
my content-jobs.php
<?php the_content() ?>
I have already read wordpress documentation but unable to understand properly, this is my first time doing it, Help me?
1) you need create separate template for these pages.
2) Create the pages with receptive names
3) Use these templates on these pages in admin
4) Create custom post type News& Events
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'News',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' )
),
'public' => true,
'has_archive' => true,
)
);
}
add this code in function.php
after that post some data in this post type
and to show this on front end in your template file use.
$args = array( 'post_type' => 'News', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
use this it will help you
I want the CMS to have the different pages (e.g. "Careers", "Jobs", "Team") with each having its own template, but then to combine them into one big scrollable page (e.g. "Our Company") that would have a template. How would I do this?
I know there used to be a function get_page but that's been deprecated (and replaced with get_post which is not the same thing), but that doesn't retrieve the page's template.
I want both the page and the template so I can output both into the main page.
I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page
Is this possible?
First for main page template choose default template and write you global elements there. And in this template use get_template part to include pages
<!--custom query for pages-->
<?php
$args= array('post_type'=>'page');
$query= new WP_Query($args);
$query->while(have_posts()):query->the_post();
$temp_name= get_page_template_slug( $post->ID );
$temp_name_exp =explode('.',$temp_name);
get_template_part($temp_name_exp[0]);
endwhile;
endif;
?>
and in career, blog etc pages
<?php
/*
Template name: Career or blog or something else
*/
?>
<?php the_tiele();
the_content();
?>
for
"I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page"
assign each pages wrapper to page slug example <section class="<?php echo $post->post_name; ?>"> and write a function to redirect your view page link to http://yoursiteurl/#page-slug
EDIT
In order to get one page's content into another use the following function:
function show_post($path){
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
and then create a template for the "Our company" page (like template-our_company.php) in which you will make a call to the function (e.g. <?php show_post('careers'); /* Shows the content of the "Careers" page using the slug. */ ?>).
So the template file should include something like this:
<?php
show_post('careers');
show_post('jobs');
show_post('team');
?>
For your 2nd question, you need to adjust the template-our_company.php file like this:
<?php
<div id="careers"></div>
show_post('careers');
<div id="jobs"></div>
show_post('jobs');
<div id="team"></div>
show_post('team');
?>
and then in the Menu dashboard, just adjust the navigation link to something like "/our-company/#careers" etc.
EDIT 2
In order to retrieve the content of pages with specified templates in another template, you can do the following:
Create the templates (files careers.php and jobs.php) and the posts that will be using those templates
/*
Template Name: Careers
*/
...
/*
Template Name: Jobs
*/
Then in the "parent" template, you can query the posts that have the above specified templates selected
untested code
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_wp_page_template',
'value' => 'careers.php',
'compare' => '='
),
array(
'key' => '_wp_page_template',
'value' => 'jobs.php',
'compare' => '='
)
)
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_content();
// or add anything else
endforeach;
wp_reset_postdata();
#user3418748's answer was a good start for me, but in my case I needed to load specific pages, and I found that just using get_template_part() by itself wasn't loading any content because I was doing it outside a loop. In order to get this to work you need to first set the global $post variable to the page/post you want to display. Here's the function I used (replace mytemplate with the name of your tempalte):
function mytemplate_show_page($path) {
global $post;
$post = get_page_by_path($path);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
I have a set of categories in my WP site. I also have a "team" thing in my site. Now when a user makes a new post he selects a category and then chooses the team to which post belongs.
I have added this team selectbox using meta boxes Now i want to have a link for each team.
How can i do that??..
I though of 2 ways:
(1) - Making a template and linking it to a page called team. So its url becomes :
site.com/team/
And now for each team making url like
site.com/team/team1
I thought that 2nd url will also be served from my template. But, instead it gave not found.
(2) - Making a custom page for each team.
Is there any other way???
Could create post-types, team 1, team 2, team 3. Than create a single-team1.php and use that template file to list stuff that has to do with team 1. Example:
<?php
$args = array(
'post_type' => 'team1',
'' => '',
'' => '',
'' => '',
);
$team_one_stuff = new WP_Query( $args );
if ( $team_one_stuff -> have_posts() ) : while ( $team_one_stuff->have_posts() ) : $team_one_stuff -> the_post();
?>
<!-- Do WP stuff here -->
<?php endwhile; else: ?>
Wow, nothing assigned to team 1... o__O
<?php endif; ?>
You could pimp-up the metaboxes you use for team by using ACF. Realy easy to create new metaboxes for certain pages or post-types whith that plugin.
Hope it helps.
/Paul