Skip to content

Commit c868c1b

Browse files
committed
Update Latest-4-Posts-Hover.php
1 parent b261991 commit c868c1b

1 file changed

Lines changed: 98 additions & 30 deletions

File tree

widgets/Latest-4-Posts-Hover.php

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,88 @@ protected function _register_controls() {
5656
}
5757
protected function render() {
5858
$settings = $this->get_settings_for_display();
59+
if($settings['posts_per_page']==null){
60+
$settings['posts_per_page']=4;
61+
}
5962
$args = [
6063
'posts_per_page' => $settings['posts_per_page'],
61-
];
64+
];
6265
$posts = get_posts($args);
63-
66+
$flex=100/$settings['posts_per_page'];
67+
$widht=$flex-1;
68+
6469
if ($posts) {
6570
echo '<div class="card2-container">';
66-
foreach ($posts as $post) {
67-
$post_title = get_the_title($post->ID);
68-
$post_content = wp_trim_words($post->post_content, 15);
69-
$post_date = date('d/m/Y', strtotime($post->post_date));
70-
$post_link = get_permalink($post->ID);
71-
// Check if the post has a featured image
72-
$featured_image = get_the_post_thumbnail_url($post->ID);
73-
if (!$featured_image) {
74-
// If not, use the custom default image
75-
$default_image = $settings['default_image']['url'];
76-
$featured_image = $default_image;
77-
}
71+
if (wp_is_mobile()) {
72+
// Codice da eseguire se la larghezza dello schermo è minore o uguale a 900 pixel
7873

79-
echo '<div class="card2">
80-
<div style="background-image: url(' . $featured_image . ');">
81-
<a href="' . $post_link . '" class="card2-link">
82-
<div class="info"> <h1>' . $post_title . '</h1> <p class="post-date">' . $post_date . '
83-
</p> <p class="description">' . $post_content . '</p> </div> </div> </a> </div> '; } echo '</div>';
84-
}
85-
74+
// Rendering dei post
75+
foreach ($posts as $post) {
76+
setup_postdata($post);
77+
78+
$post_title = get_the_title($post->ID);
79+
$post_content = wp_trim_words($post->post_content, 15);
80+
$post_date = date('d/m/Y', strtotime($post->post_date));
81+
$post_link = get_permalink($post->ID);
82+
// Check if the post has a featured image
83+
$featured_image = get_the_post_thumbnail_url($post->ID);
84+
if (!$featured_image) {
85+
// If not, use the custom default image
86+
$featured_image = $settings['default_image']['url'];
87+
}
88+
$categories = get_the_category($post->ID);
89+
if (!empty($categories)) {
90+
$category = $categories[0];
91+
$category_name = isset($category->name) ? $category->name : '';
92+
$category_link = isset($category->term_id) ? get_category_link($category->term_id) : '';
93+
} else {
94+
$category_name = '';
95+
$category_link = '';
96+
}
97+
echo '<div class="card2"style="background-image: url(' . $featured_image . ');">
98+
<div class="info">
99+
<a href="' . $post_link . '" class="card2-link" style="display: block;">
100+
<h1>' . $post_title . '</h1> <p class="post-date">' . $post_date . '
101+
</p> <p class="description">' . $post_content .
102+
'</p> </div> </div> ';
103+
}
104+
wp_reset_postdata();
105+
echo '</div>';
106+
}
107+
else {
108+
foreach ($posts as $post) {
109+
$post_title = get_the_title($post->ID);
110+
$post_content = wp_trim_words($post->post_content, 15);
111+
$post_date = date('d/m/Y', strtotime($post->post_date));
112+
$post_link = get_permalink($post->ID);
113+
// Check if the post has a featured image
114+
$featured_image = get_the_post_thumbnail_url($post->ID);
115+
if (!$featured_image) {
116+
// If not, use the custom default image
117+
$featured_image = $settings['default_image']['url'];
118+
}
119+
$categories = get_the_category($post->ID);
120+
if (!empty($categories)) {
121+
$category = $categories[0];
122+
$category_name = isset($category->name) ? $category->name : '';
123+
$category_link = isset($category->term_id) ? get_category_link($category->term_id) : '';
124+
} else {
125+
$category_name = '';
126+
$category_link = '';
127+
}
128+
echo '
129+
<div class="card2"style="background-image: url(' . $featured_image . ');">
130+
<a href="' . $post_link . '" class="card2-link">
131+
<div class="info"> <h1>' . $post_title . '</h1>
132+
<a href="' . $category_link . '"> Categoria: ' . $category_name . '</a>
133+
<a class="post-date">' . $post_date . '
134+
</a> <p class="description">' . $post_content .
135+
'</p></div> </div> </a>';
136+
}
137+
wp_reset_postdata();
138+
echo '</div>';
139+
}
140+
}
86141
else {
87142
// Gestisci il caso in cui $posts non è un array valido
88143
echo '<div class="error-message">';
@@ -96,12 +151,15 @@ protected function render() {
96151
.card2 {
97152
border-radius: 16px;
98153
margin: 0 auto;
99-
width: 450px;
100154
box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2),
101155
0px 5px 8px 0px rgba(0, 0, 0, 0.14),
102156
0px 1px 14px 0px rgba(0, 0, 0, 0.12);
103-
overflow: hidden;
104-
margin-bottom: 20px; /
157+
overflow: hidden;
158+
width: '.$widht.'%;
159+
margin-bottom: 20px;
160+
background-position: center center;
161+
background-size: cover;
162+
cursor: pointer;
105163
}
106164
107165
.info {
@@ -141,6 +199,7 @@ protected function render() {
141199
margin: 0;
142200
font-size: 32px;
143201
line-height: 1;
202+
bottom: margin 0px;
144203
color: rgba(0, 0, 0, 0.87);
145204
overflow-wrap: break-word;
146205
}
@@ -167,17 +226,26 @@ protected function render() {
167226
.card2-container {
168227
display: flex;
169228
flex-wrap: wrap;
170-
justify-content: center;
171-
}
172-
173-
.card2-container .card2 {
174-
flex-basis: calc(25% - 20px);
229+
justify-content: left;
230+
flex-basis: ('.$flex.'- 20px);
175231
}
232+
}
233+
}';
234+
if($flex!=100){
235+
echo'
236+
@media ( max-width:1200px) {
237+
.card2-container .card2 {
238+
flex-basis: 49%;
239+
}
240+
}';
241+
}
242+
243+
echo'
176244
@media (max-width: 900px) {
177245
.card2-container .card2 {
178246
flex-basis: 100%;
179247
}
180-
}
248+
}
181249
</style>';
182250

183251
}

0 commit comments

Comments
 (0)