Commit 4b02c6fb authored by Bruno Antunes Magrini's avatar Bruno Antunes Magrini

create see also posts list, list posts from the same category.

parent 1501b218
<?php
/*
Widget Name: Lista de posts da mesma categoria
Description: Adiciona Lista de posts da mesma categoria
Author: hacklab/
Author URI: https://hacklab.com.br/
*/
namespace widgets;
class SeeAlsoPostsList extends PostsList {
function __construct() {
parent::__construct('see-also-posts-list', 'Lista de posts da mesma categoria', '', '', '', plugin_dir_path(__FILE__));
unset($this->form_options['conteudo']);
$this->form_options['posts_per_page']= array(
'type' => 'number',
'label' => __('Quantiodade de posts', 'guaraci'),
'default' => '1'
);
}
function get_template_variables($instance, $args) {
$post_id = get_queried_object()->ID;
$term_list = wp_get_post_terms($post_id, 'category', array( 'fields' => 'ids' ) );
$posts_query_args = array(
'post_type' => 'post',
'post__not_in' => array($post_id),
'posts_per_page' => $instance['posts_per_page'],
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_list,
),
),
);
$see_also_query = new \WP_Query($posts_query_args);
return [
'query' => $see_also_query,
'link' => (object) $instance['link'],
'show_excerpt' => $instance['show_excerpt'],
'hide_categories' => $instance['hide_categories'],
'css_classes' => $instance['css_class'],
'show_images' => $instance['show_images']
];
}
}
Siteorigin_widget_register('see-also-posts-list', __FILE__, 'widgets\\SeeAlsoPostsList');
<?php
use guaraci\images;
$i = 0;
?>
<div class="posts-list <?= $css_classes ?>">
<div class="row">
<?php
while ($query->have_posts()):
$i++;
$query->the_post();
$img_tag = images::tag('card-large', 'card--image');
?>
<div class="column large-<?= 12 / $instance['columns'] ?> mb-15">
<div class="card">
<?php if( $img_tag != '' && ( $show_images == 'all_images' || ( $show_images == 'first_image' && $i == 1 ) ) ) : ?>
<a tabindex="-1" href="<?= get_the_permalink() ?>" class="card--image-wrapper">
<?= $img_tag ?>
</a>
<?php endif; ?>
<div class="card--content">
<?php if(!$hide_categories) : ?>
<div class="categories-list">
<?php
$post_categories = wp_get_post_categories( get_the_ID() );
foreach($post_categories as $category_id){
$category = get_category($category_id);
?>
<div class="categories-list--category category-<?= $category->slug ?>">
<a href="<?= esc_url(get_term_link($category, 'category')) ?>"><?= $category->name ?></a>
</div>
<?php
}
?>
</div>
<?php endif;?>
<h4 class="card--title">
<a href="<?= get_the_permalink() ?>"><?php the_title() ?></a>
</h4>
<?php if( ( $show_excerpt == 'all_excerpts' || ( $show_excerpt == 'first_excerpt' && $i == 1 ) ) ) : ?>
<div class="card-excerpt"><?php echo get_the_excerpt() ?></div>
<?php endif; ?>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
<?php if($link->url): ?>
<div class="read-more" >
<a class="read-more-link" href="<?= $link->url ?>" class="goto"><?= $link->label ?></a>
</div>
<?php endif; ?>
</div>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment