Skip to content

Instantly share code, notes, and snippets.

@ggsalas
Created October 5, 2015 13:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ggsalas/de5fccdb0eaf82b9d7bc to your computer and use it in GitHub Desktop.
Save ggsalas/de5fccdb0eaf82b9d7bc to your computer and use it in GitHub Desktop.
Wordpress plugin to display posts of the displayed user on a new buddypress user tab
<?php
/*
Plugin Name: Tab "My Posts"
Plugin URI:
Description: Wordpress plugin to display posts of the displayed user on a new buddypress user tab
Version: 1.0
Author: GGsalas
Author URI: http://ggsalas.com
*/
global $post;
global $bp;
global $current_user;
// Add new tab only if the user has post (is author of any post)
add_action( 'bp_setup_nav', 'solo_autores' );
function solo_autores(){
// Do the query only if is displayed any user page
if (bp_is_current_component('mis-articulos') || bp_is_current_component('profile') ||bp_is_current_component('media') || bp_is_current_component('activity') || bp_is_current_component('notifications') || bp_is_current_component('messages') || bp_is_current_component('groups') || bp_is_current_component('settings')){
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'author' => bp_displayed_user_id(),
'paged' => get_query_var('pag',1)
);
//The Query
$wp_query = new WP_Query( $args );
if( $wp_query->have_posts()){
bp_content_setup_myposts();
}else{}
}
}
// Tab "Mis articulos"
function bp_content_setup_myposts() {
global $bp;
bp_core_new_nav_item( array(
'name' => __('Mis Artículos', 'buddypress'),
'slug' => 'mis-articulos',
'parent_url' => trailingslashit( bp_displayed_user_domain() . '/' ),
'screen_function' => 'my_posts',
'position' => 30,
'default_subnav_slug' => '/',
));
}
function my_posts() {
add_action( 'bp_template_content', 'my_posts_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function my_posts_content() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'author' => bp_displayed_user_id(),
'paged' => get_query_var('pag',1)
);
//The Query
$wp_query = new WP_Query( $args );
// The Loop
if ( $wp_query->have_posts() ) {
//echo "<div class=\"container mis-articulos\"> <div class=\"row\">";
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php
//get_template_part('loop', 'conoce');
}
//echo "</div></div>";
/*
* Pagination links
* https://codex.wordpress.org/Function_Reference/paginate_links
* http://www.codegur.net/24970097/wordpress-custom-query-pagination-and-query-vars
*/
echo paginate_links( array(
'base' => @add_query_arg('pag','%#%'),
'format' => '',
'current' => max( 1, get_query_var('pag') ),
'total' => $wp_query->max_num_pages
) );
}
}
/* New query_vars to get paginstion working
* https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
* https://premium.wpmudev.org/forums/topic/woocommerce-breaks-the-pagination-on-a-buddypress-page#post-961665
*/
function themeslug_query_vars( $qvars ) {
$qvars[] = 'pag';
return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment