-
Notifications
You must be signed in to change notification settings - Fork 0
/
featured_articles.php
129 lines (108 loc) · 4.58 KB
/
featured_articles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
AUTHOR(S): Jose Cortes
DESCRIPTION:
- This snippet is for the featured articles section of the Housing Hub page.
- This snippet is meant to be used in conjunction with the archive-bulldog-living.php[subsequent-articles.php, upcoming-events.php, RA-Events] snippet.
TODO:
- Add a safeguard for when only 1 or 2 posts are returned.
- Add styles for everything.
NOTES:
- The featured articles section is a custom post type called 'bulldog-living'.
*/
?>
<!-- Featured Articles -->
<section class="Container-for-Articles-and-Title">
<div class="Container-for-Articles">
<?php
// Set up the arguments to query the most recent 1-3 posts of the custom post type 'bulldog-living'
$args = array(
'post_type' => 'bulldog_living',
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'date'
);
/* Run the query */
$query = new WP_Query($args);
/* Check if there are posts matching the query */
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
$thumbnail_url = has_post_thumbnail() ? get_the_post_thumbnail_url() : 'https://fresnostatehousing.org/wp-content/uploads/2023/06/Victoy-E-scaled.jpg';
?>
<article class="featured-article" style="background-image: url('<?php echo $thumbnail_url ?>');">
<div class="Article-Date">
<span><?php echo get_the_date() ?></span>
</div> <!-- Article-Date -->
<div class="Article-Content">
<div class="Article-Ti-Desc">
<h2 class="Article-Title"><?php echo wp_trim_words(get_the_title(), 10, '...') ?></h2>
<?php if (has_excerpt()) : ?>
<p class="Article-Description"><?php echo wp_trim_words(get_the_excerpt(), 50, '...') ?></p>
<?php endif; ?>
</div> <!-- Article-Ti-Desc -->
<a href="<?php the_permalink(); ?>" class="Article-ReadMore">
<span class="Link-Text">Read More</span>
</a> <!-- Article-ReadMore -->
</article> <!-- featured-article -->
<?php endwhile; ?>
<?php wp_reset_postdata(); // Reset the global post object
?>
<?php else : ?>
<p>No featured articles found.</p>
<?php endif; ?>
</div> <!-- Container-for-Articles -->
<div class="dots"></div> <!-- dots-->
</section> <!-- Container-for-Articles-and-Title -->
<script>
let slideIndex = 0;
let timer;
function showSlides() {
let slides = document.querySelectorAll(".featured-article");
let dots = document.querySelectorAll(".dot");
for (let i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
for (let i = 0; i < dots.length; i++) {
dots[i].classList.remove("active");
}
slides[slideIndex-1].style.display = "flex";
dots[slideIndex-1].classList.add("active");
timer = setTimeout(showSlides, 5000); // Change slide every 5 seconds
}
function createDots() {
let slides = document.querySelectorAll(".featured-article");
let dotsContainer = document.querySelector(".dots");
for (let i = 0; i < slides.length; i++) {
let dot = document.createElement("span");
dot.classList.add("dot");
// Adding the click event listener to the dot
dot.addEventListener("click", function() {
goToSlide(i);
});
dotsContainer.appendChild(dot);
}
}
function goToSlide(index) {
slideIndex = index;
clearTimeout(timer); // Clear the current timer
showSlides(); // Display the slide
}
function addHoverListeners() {
let slides = document.querySelectorAll(".featured-article");
for (let i = 0; i < slides.length; i++) {
slides[i].addEventListener("mouseover", function() {
clearTimeout(timer);
});
slides[i].addEventListener("mouseout", function() {
timer = setTimeout(showSlides, 5000); // Restart the slide transition
});
}
}
createDots();
showSlides();
addHoverListeners();
</script>