Skip to content

Commit

Permalink
issue #3 & issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgimenez committed May 18, 2015
1 parent cb7371b commit eaf848a
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 11 deletions.
36 changes: 34 additions & 2 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ jQuery(document).ready(function($) {
$('.btn-loadmore').on('click',function(e){
var data = {
'action': 'midia_load_posts',
'paged': $(this).attr('data-paged')
'paged': $(this).attr('data-paged'),
'category': $(this).attr('data-category')
};
var default_html = $(this).html();
$(this).html($(this).attr('data-loading'));
Expand All @@ -197,6 +198,37 @@ jQuery(document).ready(function($) {
}
});

})
});
$('.btn-ajax-categoria').on('click',function(e){
var data = {
'action': 'midia_load_category',
'category': $(this).attr('data-category')
};
console.log(data);
var elem = this;
$('.btn-ajax-categoria.active').removeClass('active');
$('.btn-loadmore').show();
$.ajax({
type: 'POST',
url: odin_main.ajaxurl,
data: data,
dataType: 'json',
complete: function(response){
var obj = $.parseJSON(response.responseText);
var selector = $('.btn-loadmore').attr('data-selector');
$(elem).addClass('active');
$('.btn-loadmore').attr('data-max-paged',obj.max_paged);
$('.btn-loadmore').attr('data-paged',2);
$('.btn-loadmore').attr('data-category',$(elem).attr('data-category'));
var paged = parseInt($('.btn-loadmore').attr('data-paged'));
var max_paged = parseInt($('.btn-loadmore').attr('data-max-paged'));
if(paged > max_paged){
$('.btn-loadmore').fadeOut('slow');
}
$(selector).html(obj.html);
},
});
});

});

43 changes: 42 additions & 1 deletion inc/custom-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,45 @@ function disco_cpt() {

register_post_type( 'disco', $args );
}
/////////////////////////////////////////
/////////////////////////////////////////
if ( ! function_exists( 'custom_tax_categoria' ) ) {

// Register Custom Taxonomy
function custom_tax_categoria() {

$labels = array(
'name' => _x( 'Categorias', 'Taxonomy General Name', 'odin' ),
'singular_name' => _x( 'Categoria', 'Taxonomy Singular Name', 'odin' ),
'menu_name' => __( 'Categorias', 'odin' ),
'all_items' => __( 'Todas Categorias', 'odin' ),
'parent_item' => __( 'Categoria Parente', 'odin' ),
'parent_item_colon' => __( 'Categoria Parente:', 'odin' ),
'new_item_name' => __( 'Adicionar nova Categoria', 'odin' ),
'add_new_item' => __( 'Adicionar nova Categoria', 'odin' ),
'edit_item' => __( 'Editar categoria', 'odin' ),
'update_item' => __( 'Atualizar categoria', 'odin' ),
'view_item' => __( 'Ver Categoria', 'odin' ),
'separate_items_with_commas' => __( 'Categorias separadas por virgula', 'odin' ),
'add_or_remove_items' => __( 'Adicionar ou remover categorias', 'odin' ),
'choose_from_most_used' => __( 'Escolha pelos mais usados', 'odin' ),
'popular_items' => __( 'Categorias populares', 'odin' ),
'search_items' => __( 'Buscar Categoria', 'odin' ),
'not_found' => __( 'Não encontrado', 'odin' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'categoria', array( 'midia' ), $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_tax_categoria', 0 );

}
67 changes: 59 additions & 8 deletions inc/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function short_query_func( $atts ) {

$a = shortcode_atts( array(
'post_type' => 'flauta',
'paged' => get_query_var( 'paged', 1 )
'paged' => get_query_var( 'paged', 1 ),
'category' => false
), $atts );
switch ($a['post_type']) {
case 'flauta':
Expand All @@ -25,15 +26,32 @@ function short_query_func( $atts ) {
case 'projeto':
$per_page= '99999';
$thumb='thumb-projeto';
$class_container = "col-sm-8";


$class_container = "col-sm-8";
break;
case 'midia':
$per_page= '3';
$thumb='thumb-midia';
$class_item = 'col-sm-4';
$class_container = "row";
if(!defined('DOING_AJAX')){
$antes_interno = '<div class="midia-category">';
$antes_interno .= '<a class="btn btn-default btn-lg btn-ajax-categoria active" data-category="all">';
$antes_interno .= __('Todos','odin');
$antes_interno .= '</a>';
$categories = get_categories(
array(
'type' => 'midia',
'taxonomy' => 'categoria',
)
);
foreach ($categories as $cat) {
$antes_interno .= '<a class="btn btn-default btn-lg btn-ajax-categoria" data-category="'.$cat->term_id.'">';
$antes_interno .= apply_filters('the_title',$cat->name);
$antes_interno .= '</a>';
}

$antes_interno .= '</div>';
}

break;
case 'equipe':
Expand Down Expand Up @@ -69,7 +87,20 @@ function short_query_func( $atts ) {
'paged' => $paged,

);

if($a['category'] != false && $a['category'] !== 'all'){
$args = array(
'post_type' => $a['post_type'],
'posts_per_page' => $per_page,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'field' => 'term_id',
'terms' => $a['category'],
),
),
);
}
$query = new WP_Query( $args );
$html .= "<div class='".$class_container."' id='interno-".$a['post_type']."'>".$antes_interno."<div id='interno-nav-".$a['post_type']."'>";
if($query->have_posts()) :
Expand All @@ -88,13 +119,17 @@ function short_query_func( $atts ) {
}
else{
if($a['post_type'] == 'midia' && !defined('DOING_AJAX')){
$depois_interno = '<a class="btn btn-loadmore" data-paged="2" data-loading="'.__('Carregando...', 'odin').'" data-selector="#interno-nav-midia" data-max-paged="'.$query->max_num_pages.'">';
$depois_interno = '<a class="btn btn-loadmore" data-paged="2" data-loading="'.__('Carregando...', 'odin').'" data-selector="#interno-nav-midia" data-max-paged="'.$query->max_num_pages.'" data-category="all">';
$depois_interno .= __('Carregar +','odin');
$depois_interno .= '</a>';
}
$html .="<div class='".$class_item." cada-".$a['post_type']."'>".get_the_post_thumbnail($query->post->ID, $thumb)."</div>";
}
endwhile;
if($a['post_type'] == 'midia' && defined('DOING_AJAX') && DOING_AJAX){
global $ajax_max_paged;
$ajax_max_paged = $query->max_num_pages;
}
wp_reset_postdata();
else:
$html = 'Adicionar um '.$a['post_type'];
Expand Down Expand Up @@ -127,8 +162,24 @@ function lista_videos( ) {

//ajax midia
function ajax_midia_load_posts(){
echo do_shortcode('[query post_type="midia" paged="'.$_POST['paged'].'"]');
if(!isset($_POST['category']) || $_POST['category'] == 'all'){
echo do_shortcode('[query post_type="midia" paged="'.$_POST['paged'].'"]');
}
else{
echo do_shortcode('[query post_type="midia" paged="'.$_POST['paged'].'" category="'.$_POST['category'].'"]');
}
wp_die();
}
add_action( 'wp_ajax_midia_load_posts', 'ajax_midia_load_posts' );
add_action( 'wp_ajax_nopriv_midia_load_posts', 'ajax_midia_load_posts' );
add_action( 'wp_ajax_nopriv_midia_load_posts', 'ajax_midia_load_posts' );
//ajax load categories posts
function ajax_midia_load_category(){
global $ajax_max_paged;
$ajax_response = array();
$ajax_response['html'] = do_shortcode('[query post_type="midia" category="'.$_POST['category'].'"]');
$ajax_response['max_paged'] = $ajax_max_paged;
echo json_encode($ajax_response);
wp_die();
}
add_action( 'wp_ajax_midia_load_category', 'ajax_midia_load_category' );
add_action( 'wp_ajax_nopriv_midia_load_category', 'ajax_midia_load_category' );

0 comments on commit eaf848a

Please sign in to comment.