Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Related posts #31

Merged
merged 11 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion main.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,11 @@ function bs_swiper_hero() {
function bs_swiper_hero_fade() {
return bs_swiper_get_template('sc-swiper-hero-fade.php');
}
add_action('wp_head', 'bs_swiper_hero_fade');
add_action('wp_head', 'bs_swiper_hero_fade');


// Related Posts
function bs_swiper_related_posts() {
return bs_swiper_get_template('related-posts.php');
}
add_action('wp_head', 'bs_swiper_related_posts');
89 changes: 89 additions & 0 deletions templates/related-posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/*
* Adds related posts to single_*.php. Needs at least bootScore 5.3.1
*
* This template can be overriden by copying this file to your-theme/bs-swiper-main/related-posts.php
*
* @author bootScore
* @package bS Swiper
* @version 5.3.0
*/

function bootscore_related_posts() {

$post_id = get_the_ID();
$cat_ids = array();
$categories = get_the_category($post_id);

if (!empty($categories) && !is_wp_error($categories)) :
foreach ($categories as $category) :
array_push($cat_ids, $category->term_id);
endforeach;
endif;

$current_post_type = get_post_type($post_id);

$query_args = array(
'category__in' => $cat_ids,
'post_type' => $current_post_type,
'post__not_in' => array($post_id),
'posts_per_page' => '12',
);

$related_cats_post = new WP_Query($query_args);
?>

<div class="related-posts mb-3">
<hr>
<h2 class="h4 text-center my-4"><?php _e('You might also like', 'bootscore'); ?></h2>
<div class="px-lg-5 position-relative">
<div class="cards swiper-container swiper position-static">
<div class="swiper-wrapper">

<?php
if ($related_cats_post->have_posts()) :
while ($related_cats_post->have_posts()) : $related_cats_post->the_post();
?>

<div class="swiper-slide card h-auto mb-5">

<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('medium', array('class' => 'card-img-top')); ?>
<?php endif; ?>

<div class="card-body d-flex flex-column">

<?php the_title('<h3 class="card-title h6 text-truncate">', '</h3>'); ?>

<p class="card-text small text-truncate">
<?= strip_tags(get_the_excerpt()); ?>
</p>

<p class="card-text small mt-auto">
<a class="read-more stretched-link" href="<?php the_permalink(); ?>">
<?php _e('Read more »', 'bootscore'); ?>
</a>
</p>

</div>

</div><!-- .card -->

<?php endwhile; ?>

</div><!-- .swiper-wrapper -->

<!-- Add Pagination -->
<div class="swiper-pagination"></div>
<!-- Add Arrows -->
<div class="swiper-button-next end-0 d-none d-lg-block"></div>
<div class="swiper-button-prev start-0 d-none d-lg-block"></div>

</div><!-- .swiper-container -->
</div><!-- .px-lg-5.position-relative -->
</div><!-- .related-posts -->
<?php
// Restore original Post Data
wp_reset_postdata();
endif;
}