Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brunolimadevelopment/dd972d91fa81605d79019a822d363890 to your computer and use it in GitHub Desktop.
Save brunolimadevelopment/dd972d91fa81605d79019a822d363890 to your computer and use it in GitHub Desktop.
[WP] THUMB + CATEGORIA + TITULO + DESCRIÇÃO
// ARQUIVO FUNCTIONS.PHP
// O hook {taxonomy}_edit_form_fields é responsável
// por exibir o campo que criamos através da função render_category_metas.
function render_category_metas( $term ) {
$color = get_term_meta( $term->term_id, 'color', true );
?>
<div class="form-field">
<tr class="form-field">
<th scope="row" valign="top">
<label>Selecionar Cor"</label>
</th>
<td>
<input type="color" name="color" value="<?php echo $color; ?>"/>
<?php wp_nonce_field('color_nonce_action','color_nonce_field'); ?>
</td>
</tr>
</div>
<?php
}
add_action( 'category_edit_form_fields', 'render_category_metas' );
// Funções que salvam e excluem os metas:
function save_category_metas( $term_id ) {
if ( isset( $_POST['color'] )
&& wp_verify_nonce( $_POST['color_nonce_field'], 'color_nonce_action' ) ) {
update_term_meta( $term_id, 'color', strip_tags( $_POST['color'] ) );
}
}
add_action( 'create_category', 'save_category_metas' );
add_action( 'edited_category', 'save_category_metas' );
function delete_category_metas( $term_id ) {
delete_term_meta( $term_id, 'color' );
}
add_action( 'delete_category', 'delete_category_metas' );
// ARQUIVO SECTION
<?php
$args = array(
'post_type' => 'post',
'cat' => '2,5,9',
//'category__not_in' => '8', // não inclua essa categoria nas postagens
'posts_per_page' => 5,
'no_found_rows' => 'true',
'orderby' => 'date',
'order' => 'DESC'
);
$postagem = new WP_Query($args);
?>
<section class="section">
<div class="wrap">
<?php if ($postagem->have_posts()) : ?>
<div class="list-postagens">
<?php while ($postagem->have_posts()) : $postagem->the_post();
$cat = get_the_category(get_the_ID()); // pega a categoria do post.
$color = get_term_meta( $cat[0]->term_id, 'color', true); // campo customizado.
?>
<div id="post-<?php the_ID(); ?>" class="card-postagem">
<figure class="card-postagem__figure">
<?php the_post_thumbnail( 'thumb-small', array( 'class' => 'card-postagem__thumb', 'alt' => get_the_title(), 'title' => get_the_title() ) ); ?>
</figure>
<div class="card-postagem__legend">
<a href="<?php echo get_category_link($cat[0]->term_id); ?>" class="card-postagem__categoria" style="color: #fff; background: <?php echo $color ? $color : '#343434'; ?>;"><?php echo $cat[0]->name; ?></a>
<?php the_title('<h2 class="card-postagem__title">', '</h2>'); ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); else : ?>
<p><?php _e('Nenhum post cadastrado.'); ?></p>
<?php endif; ?>
</div>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment