Skip to content

Instantly share code, notes, and snippets.

@davidchc
Last active August 6, 2020 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidchc/ba2507959686016168ff739d20956e3f to your computer and use it in GitHub Desktop.
Save davidchc/ba2507959686016168ff739d20956e3f to your computer and use it in GitHub Desktop.
Exibir categorias personalizadas eposts
<?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<
<?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