Come filtrare i post per categorie?
7 ago 2013, 12:31:35
Visualizzazioni: 14.2K
Voti: 0
Ho un custom post type e ora devo filtrare i progetti in base alle loro categorie senza essere reindirizzato a un'altra pagina. Inoltre, ho bisogno di avere una categoria all
che mostri tutti i progetti. Link al sito di test. Apprezzerei qualsiasi aiuto.
portfolio-type.php:
<?php
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 270, 170, true );
add_image_size( 'screen-shot', 720, 540 );
}
add_action('init', 'portfolio_register');
function portfolio_register() {
$labels = array(
'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio Item', 'post type singular name'),
'add_new' => _x('Add New', 'portfolio item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio Items'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'portfolio' , $args );
}
function create_portfolio_taxonomies() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categories' ),
);
register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args );
}
add_action( 'init', 'create_portfolio_taxonomies', 0 );
?>
index.php:
<!-- Start Portfolio Page -->
<section id="portfolio" class="page">
<div class="container">
<div class="row">
<div class="span12">
<div class="title">Portfoolio</div>
<hr>
<div class="sub-title visible-desktop">Tööd:</div>
<!-- Start Filters -->
<?php
$taxonomy = 'portfolio_categories';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) :
?>
<ul class="option-set" data-option-key="filter">
<li class="filter-icon hidden-phone">A</li>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>" ><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
<?php endif;?>
<!-- End Filters -->
<!-- Start Projects -->
<div id="posts" class="row isotope">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$title= str_ireplace('"', '', trim(get_the_title()));
$desc= str_ireplace('"', '', trim(get_the_content()));
?>
<div class="item post item span4 isotope-item">
<a class="project-wrp fancybox" title="<?=$title?>" rel="lightbox[work]" href="<?php print portfolio_thumbnail_url($post->ID) ?>"><div class="profile-photo"><div class="profile-icon">f</div><?php the_post_thumbnail(array('230','170'),array('alt' => '')); ?> </div>
<div class="project-name"><?php echo $title; ?></div>
<div class="project-client"><?php echo $desc; ?></div>
</a>
</div>
<?php endwhile; endif; ?>
</div>
</div>
</div>
</div>
</section>
<!-- End Portfolio Page -->

Laniakea
503
Tutte le risposte alla domanda
1
1
HTML:
<ul id="filters">
<li><a href="#" data-filter="*">mostra tutto</a></li>
<li><a href="#" data-filter=".metal">metallo</a></li>
<li><a href="#" data-filter=".transition">transizione</a></li>
<li><a href="#" data-filter=".alkali, .alkaline-earth">alcalino e alcalino-terroso</a></li>
<li><a href="#" data-filter=":not(.transition)">non transizione</a></li>
<li><a href="#" data-filter=".metal:not(.transition)">metallo ma non transizione</a></li>
</ul>
<div id="container">
<div class="element transition metal">...</div>
<div class="element post-transition metal">...</div>
<div class="element alkali metal">...</div>
<div class="element transition metal">...</div>
<div class="element lanthanoid metal inner-transition">...</div>
<div class="element halogen nonmetal">...</div>
<div class="element alkaline-earth metal">...</div>
...
</div>
Logica JS:
// cache container
var $container = $('#container');
// initialize isotope
$container.isotope({
// options...
});
// filter items when filter link is clicked
$('#filters a').click(function(){
var selector = $(this).attr('data-filter');
$container.isotope({ filter: selector });
return false;
});
Sembra che ti sia mancato l'attributo "data-filter" sui link dei filtri e la classe da filtrare all'interno dell'attributo class dei tuoi elementi post. Potresti stampare lo slug della categoria nell'attributo "data-filter" e lo stesso valore nell'attributo class dei tuoi elementi post.
AGGIORNAMENTO
In base alle nuove risposte,
index.php
<!-- Preso da index.php -->
<!-- Inizio Filtri -->
<?php
$taxonomy = 'portfolio_categories';
$terms = get_terms($taxonomy); // Ottieni tutti i termini di una tassonomia
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul class="option-set" data-option-key="filter">
<li class="filter-icon hidden-phone">A</li>
<li><a href="#" data-option-value="*">Tutto</a></li>
<?php foreach ( $terms as $term ) { ?>
<li><a data-option-value=".<?php echo $term->slug; ?>" href="#" ><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
<?php endif;?>
<!-- Fine Filtri -->
<!-- Inizio loop elementi Portfolio -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$title= str_ireplace('"', '', trim(get_the_title()));
$desc= str_ireplace('"', '', trim(get_the_content()));
/*
* Nota: la funzione get_the_terms() recupera tutti i termini di una tassonomia che appartengono al post.
* Ritorna: (array|false|wp_error)
* 1. array di oggetti term
* 2. false se non ci sono termini nella tassonomia
* 3. oggetto wp_error se la tassonomia specificata non è valida
*/
$termsObj = get_the_terms( $post->ID, 'portfolio_categories' );
if ( $termsObj && !is_wp_error( $termsObj ) ) :
//Abbiamo bisogno solo dello slug di questo/questi termine/i quindi cicliamo il risultato per prendere solo il valore slug e memorizzarlo in un nuovo array
$terms = array();
foreach( $termsObj as $obj ){
$terms[] = $obj->slug;
}
//Uniamo o implodiamo il nuovo array in una stringa e poi lo inseriamo nell'attributo class dell'elemento portfolio
$termsString = join( ' ', $terms);
endif;
?>
<div class="item post item span4 isotope-item <?php echo $termsString; ?>">
<a class="project-wrp fancybox" title="<?=$title?>" rel="lightbox[work]" href="<?php print portfolio_thumbnail_url($post->ID) ?>">
<div class="profile-photo"><div class="profile-icon">f</div><?php the_post_thumbnail(array('230','170'),array('alt' => '')); ?> </div>
<div class="project-name"><?php echo $title; ?></div>
<div class="project-client"><?php echo $desc; ?></div>
</a>
</div>
<?php endwhile; endif; ?>
<!-- Fine loop elementi Portfolio -->

iEmanuele
1.62K
7 ago 2013 13:21:51
Commenti
continuiamo questa discussione in chat

7 ago 2013 23:46:59
Domande correlate
2
risposte
1
risposte