Last active
August 6, 2020 11:48
-
-
Save davidchc/ba2507959686016168ff739d20956e3f to your computer and use it in GitHub Desktop.
Exibir categorias personalizadas eposts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Pega as taxonomias criadas criadas | |
$terms = get_terms( array( | |
'taxonomy' => 'nome_da_sua_taxonomia', | |
'hide_empty' => false, | |
) ); | |
//Pega todos os posts do tipo criado | |
$wp = new WP_Query(array( | |
"post_type" => "seu_tipo_personalizado" | |
)); | |
?> | |
<!-- Lista as categorias com imagens, e add o slug num atributo data, isso to colocando de exemplo--> | |
<ul> | |
<?php foreach($terms as $term) : ?> | |
<li> | |
<a href="#" data-list="<?php echo $term->slug ?>"> | |
<img src="<?php echo getImageUrlTaxonomy($term->term_id)?>" /> | |
<?php echo $term->name?> | |
</a> | |
</li> | |
<?php endforeach?> | |
</ul> | |
<!--Lista as publicações, e atributo os slugs a classe --> | |
<section class="cards"> | |
<?php while($wp->have_posts()) : $wp->the_post(); ?> | |
<article class="card <?php echo getSlugsTaxonomy(get_the_ID())?>"> | |
<?php the_title();?> | |
</article> | |
<?php endwhile?> | |
</section< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Cria uma função pra simplifica exibição da imagem | |
function getImageUrlTaxonomy($term_id) { | |
$taxonomy_image_id = get_term_meta($term_id, 'taxonomy_image_id', true); | |
return wp_get_attachment_image_url($taxonomy_image_id, 'thumbnail'); | |
} | |
//Cria uma função pra retornar os slugs da taxonomia de um post | |
function getSlugsTaxonomy($post_id) { | |
$terms = wp_get_post_terms( $post_id, 'nome_da_sua_taxonomia', array( 'fields' => 'all' ) ); | |
$results = array(); | |
foreach($terms as $term) { | |
$results[] = $term->slug; | |
} | |
return $results ? implode(' ', $results) : ''; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment