Skip to content

Commit cf9cbd3

Browse files
committed
Update Latest-Posts-Hover.php
1 parent 7b2a436 commit cf9cbd3

1 file changed

Lines changed: 130 additions & 122 deletions

File tree

widgets/Latest-Posts-Hover.php

Lines changed: 130 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected function _register_controls()
206206
'type' => \Elementor\Controls_Manager::SWITCHER,
207207
'label_on' => esc_html__('On', 'Latest-Posts-Hover'),
208208
'label_off' => esc_html__('Off', 'Latest-Posts-Hover'),
209-
'return_value' => 'block',
209+
'return_value' => 'inline-block',
210210
'default' => 'none',
211211
'selectors' => [
212212
'{{WRAPPER}} .date' => 'display: {{VALUE}};',
@@ -314,7 +314,7 @@ protected function _register_controls()
314314
'type' => \Elementor\Controls_Manager::SWITCHER,
315315
'label_on' => esc_html__('On', 'Latest-Posts-Hover'),
316316
'label_off' => esc_html__('Off', 'Latest-Posts-Hover'),
317-
'return_value' => 'block',
317+
'return_value' => 'inline-block',
318318
'default' => 'none',
319319
'selectors' => [
320320
'{{WRAPPER}} .tag' => 'display: {{VALUE}};',
@@ -507,7 +507,6 @@ protected function _register_controls()
507507
'default' => 'left',
508508
'toggle' => true,
509509
'selectors' => [
510-
'{{WRAPPER}} .categories-links' => 'text-align: {{VALUE}};',
511510
'{{WRAPPER}} .category' => 'text-align: {{VALUE}};',
512511

513512

@@ -519,6 +518,24 @@ protected function _register_controls()
519518
],
520519
]
521520
);
521+
$this->add_control(
522+
'categories_in',
523+
[
524+
'label' => esc_html__('Select Categories to include', 'Latest-Posts-Hover'),
525+
'type' => \Elementor\Controls_Manager::SELECT2,
526+
'multiple' => true,
527+
'options' => $this->get_category(),
528+
]
529+
);
530+
$this->add_control(
531+
'exclude_categories',
532+
[
533+
'label' => esc_html__('Select Categories to exclude', 'Latest-Posts-Hover'),
534+
'type' => \Elementor\Controls_Manager::SELECT2,
535+
'multiple' => true,
536+
'options' => $this->get_category(),
537+
]
538+
);
522539
$this->end_controls_section();
523540
$this->start_controls_section(
524541
'section_content',
@@ -689,7 +706,7 @@ protected function _register_controls()
689706
[
690707
'label' => esc_html__('Inactive text color', 'Latest-Posts-Hover'),
691708
'type' => \Elementor\Controls_Manager::COLOR,
692-
'default' => 'black',
709+
'default' => 'white',
693710
'selectors' => [
694711
'{{WRAPPER}} .category-filter-button' => ' color: {{VALUE}} !important;',
695712
],
@@ -756,7 +773,7 @@ protected function _register_controls()
756773
[
757774
'label' => esc_html__('Active text clor', 'Latest-Posts-Hover'),
758775
'type' => \Elementor\Controls_Manager::COLOR,
759-
'default' => 'black',
776+
'default' => 'white',
760777
'selectors' => [
761778
'{{WRAPPER}} .category-filter-button.active' => ' color: {{VALUE}} !important;',
762779
],
@@ -778,7 +795,7 @@ protected function _register_controls()
778795
[
779796
'label' => esc_html__('Active background color', 'Latest-Posts-Hover'),
780797
'type' => \Elementor\Controls_Manager::COLOR,
781-
'default' => 'black',
798+
'default' => 'red',
782799
'selectors' => [
783800
'{{WRAPPER}} .category-filter-button.active' => ' background-color: {{VALUE}};',
784801
],
@@ -823,7 +840,7 @@ protected function _register_controls()
823840
[
824841
'label' => esc_html__('Hover text color', 'Latest-Posts-Hover'),
825842
'type' => \Elementor\Controls_Manager::COLOR,
826-
'default' => 'black',
843+
'default' => 'white',
827844
'selectors' => [
828845
'{{WRAPPER}} .category-filter-button:hover' => ' color: {{VALUE}} !important;',
829846
],
@@ -845,7 +862,7 @@ protected function _register_controls()
845862
[
846863
'label' => esc_html__('Hover background color', 'Latest-Posts-Hover'),
847864
'type' => \Elementor\Controls_Manager::COLOR,
848-
'default' => 'black',
865+
'default' => 'orange',
849866
'selectors' => [
850867
'{{WRAPPER}} .category-filter-button:hover' => ' background-color: {{VALUE}};',
851868
],
@@ -889,6 +906,15 @@ protected function _register_controls()
889906

890907
$this->end_controls_section();
891908
}
909+
private function get_category()
910+
{
911+
$categories = get_categories();
912+
$options = [];
913+
foreach ($categories as $category) {
914+
$options[$category->term_id] = $category->name;
915+
}
916+
return $options;
917+
}
892918
private function get_pages()
893919
{
894920
$pages = get_pages();
@@ -910,24 +936,23 @@ protected function render()
910936
$args = [
911937
'posts_per_page' => $settings['posts_per_page'],
912938
'category_name' => isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '',
939+
'tag' => isset($_GET['tags']) ? sanitize_text_field($_GET['tags']) : '',
913940
];
914941
if (isset($_GET['category']) && ($_GET['category']) == "all") {
915-
$args = [
916-
'posts_per_page' => $settings['posts_per_page'],
917-
];
918-
} else {
919-
$args = [
920-
'posts_per_page' => $settings['posts_per_page'],
921-
'category_name' => isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '',
922-
942+
unset($args['category_name']);
943+
}
944+
if (isset($_GET['date'])) {
945+
$date_query = [
946+
'after' => $_GET['date'],
947+
'inclusive' => true,
923948
];
949+
$args['date_query'] = $date_query;
924950
}
925951
if ($settings['all_post'] == 'all') {
926-
$args = [
927-
'posts_per_page' => -1,
928-
];
952+
$args['posts_per_page'] = -1;
929953
}
930-
954+
$args['category__not_in'] = $settings['exclude_categories'];
955+
$args['category__in'] = $settings['categories_in'];
931956
$posts = get_posts($args);
932957
$cardColor = $settings['card_color'];
933958
$wordPc = $settings['content_word_pc'];
@@ -939,11 +964,10 @@ protected function render()
939964
$flex = 25;
940965
}
941966
$widht = $flex - 1;
942-
$title_color = $settings['Title_color'];
943967
if ($posts) {
944968
echo '<div class="category-filter">
945-
<form method="get" action="">
946-
<button type="submit" name="category" value="all" class="category-filter-button';
969+
<form method="get" action="">
970+
<button type="submit" name="category" value="all" class="category-filter-button';
947971
if (isset($_GET['category']) && $_GET['category'] == 'all') {
948972
echo ' active';
949973
}
@@ -961,119 +985,102 @@ protected function render()
961985
echo '</form> </div>';
962986
echo '<div class="card2-container">';
963987
$selected_page_id = $settings['selected_page'];
964-
if (wp_is_mobile()) {
965-
// Codice da eseguire se la larghezza dello schermo è minore o uguale a 900 pixel
966988

967-
// Rendering dei post
968-
foreach ($posts as $post) {
969-
$post_title = get_the_title($post->ID);
989+
foreach ($posts as $post) {
990+
$post_title = get_the_title($post->ID);
991+
if (wp_is_mobile()) {
992+
$post_content = wp_trim_words($post->post_content, $wordMobile);
993+
} else {
970994
$post_content = wp_trim_words($post->post_content, $wordPc);
971-
$post_date = date_i18n(get_option('date_format'), strtotime($post->post_date));
972-
$post_link = get_permalink($post->ID);
973-
$tags = get_the_tags($post->ID);
974-
if ($tags) {
975-
foreach ($tags as $tag) {
976-
echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>';
977-
}
978-
// Check if the post has a featured image
979-
$featured_image = get_the_post_thumbnail_url($post->ID);
980-
if (!$featured_image) {
981-
// If not, use the custom default image
982-
$featured_image = $settings['default_image']['url'];
983-
}
995+
}
996+
$post_date = get_the_date('j F Y', $post->ID);
997+
$post_link = get_permalink($post->ID);
998+
$tags = get_the_tags($post->ID);
999+
$post_numb = get_the_date('Y-m-d', $post->ID);
1000+
$date_array = explode('-', $post_numb);
1001+
// Check if the post has a featured image
1002+
$featured_image = get_the_post_thumbnail_url($post->ID);
1003+
if (!$featured_image) {
1004+
// If not, use the custom default image
1005+
$featured_image = $settings['default_image']['url'];
1006+
}
1007+
if (is_admin()) {
9841008
echo '<div class="card2" style="background-image: url(' . $featured_image . '); ">';
985-
echo '
986-
<div class="info">
987-
<a class="title" href="' . $post_link . '">' . $post_title . ' <a/>';
988-
if ($tags) {
989-
foreach ($tags as $tag) {
990-
echo '<a href="' . get_tag_link($tag->term_id) . '" class="tag">' . $tag->name . '</a>';
991-
} } echo'
992-
<p class="date">' . $post_date . '</p> ';
1009+
} else {
1010+
if (wp_is_mobile()) {
1011+
echo '<div class="card2" style="background-image: url(' . $featured_image . ')" >';
1012+
} else {
1013+
echo '<div class="card2" style="background-image: url(' . $featured_image . ')" onclick="window.location.href=\'' . $post_link . '\'">';
1014+
}
1015+
}
1016+
echo '
1017+
<div class="info">
1018+
<a class="title" href="' . $post_link . '">' . $post_title . ' <a/>';
1019+
if (!empty($tags)) {
9931020
if ($selected_page_id != 0) {
9941021
$page_link = get_permalink($selected_page_id);
995-
$categories = get_the_category($post->ID);
996-
if (!empty($categories)) {
997-
echo '<div class="categories-links">';
998-
foreach ($categories as $category) {
999-
$category_link = add_query_arg('category', $category->slug, $page_link);
1000-
echo '<a href="' . $category_link . '" class="category"> ' . $category->name . '&nbsp; </a>';
1001-
}
1002-
echo '</div>';
1022+
foreach ($tags as $tag) {
1023+
$tag_link = add_query_arg('tags', $tag->slug, $page_link);
1024+
echo '<a href="' . $tag_link . '" class="tag"> ' . $tag->name . '</a> ';
10031025
}
10041026
} else {
1005-
$categories = get_the_category($post->ID);
1006-
if (!empty($categories)) {
1007-
echo '<div class="categories-links">';
1008-
foreach ($categories as $category) {
1009-
echo '<a href="' . get_category_link($category->term_id) . '" class="category"> ' . $category->name . '&nbsp; </a>';
1010-
}
1011-
echo '</div>';
1027+
foreach ($tags as $tag) {
1028+
1029+
echo '<a href="' . get_tag_link($tag->term_id) . '" class="tag">' . $tag->name . '</a> ';
10121030
}
10131031
}
1014-
echo '<a class="description" href="' . $post_link . '">' . $post_content . ' </a>
1015-
</div>
1016-
</div>';
10171032
}
1018-
wp_reset_postdata();
1019-
echo '</div>';
1020-
}
1021-
}
1022-
else {
1023-
foreach ($posts as $post) {
1024-
$post_title = get_the_title($post->ID);
1025-
$post_content = wp_trim_words($post->post_content, $wordPc);
1026-
$post_date = date_i18n(get_option('date_format'), strtotime($post->post_date));
1027-
$post_link = get_permalink($post->ID);
1028-
$tags = get_the_tags($post->ID);
1029-
// Check if the post has a featured image
1030-
$featured_image = get_the_post_thumbnail_url($post->ID);
1031-
if (!$featured_image) {
1032-
// If not, use the custom default image
1033-
$featured_image = $settings['default_image']['url'];
1033+
$date_parts = explode(' ', $post_date);
1034+
$i = 2;
1035+
$date_array[1] = $date_array[0] . '/' . $date_array[1];
1036+
$date_array[2] = $date_array[1] . '/' . $date_array[2];
1037+
if ($selected_page_id != 0) {
1038+
$page_link = get_permalink($selected_page_id);
1039+
echo '<div>';
1040+
foreach ($date_parts as $part) {
1041+
if ($i == 1) {
1042+
$date_array[1] = $date_array[1] . '/01';
1043+
}
1044+
$date_link = add_query_arg('date', $date_array[$i], $page_link);
1045+
echo ' <a href="' . $date_link . '" class="date">' . ucfirst($part) . '</a>';
1046+
$i -= 1;
10341047
}
1035-
if (is_admin()) {
1036-
echo '<div class="card2" style="background-image: url(' . $featured_image . '); ">';
1037-
} else {
1038-
echo ' <div class="card2" style="background-image: url(' . $featured_image . '); "onclick=\'window.location.href="' . $post_link . '"\'>';
1048+
echo '</div>';
1049+
} else {
1050+
echo '<div>';
1051+
$i = 2;
1052+
foreach ($date_parts as $part) {
1053+
$date_link = home_url() . '/' . $date_array[$i];
1054+
echo '<a href="' . $date_link . '" class="date">' . ucfirst($part) . '</a>';
1055+
$i -= 1;
10391056
}
1040-
echo '
1041-
<div class="info">
1042-
<a class="title" href="' . $post_link . '">' . $post_title . ' <a/>';
1043-
if ($tags) {
1044-
foreach ($tags as $tag) {
1045-
echo '<a href="' . get_tag_link($tag->term_id) . '" class="tag">' . $tag->name . '</a>';
1046-
} } echo'
1047-
<p class="date">' . $post_date . '</p> ';
1048-
if ($selected_page_id != 0) {
1049-
$page_link = get_permalink($selected_page_id);
1050-
$categories = get_the_category($post->ID);
1051-
if (!empty($categories)) {
1052-
echo '<div class="categories-links">';
1053-
foreach ($categories as $category) {
1054-
$category_link = add_query_arg('category', $category->slug, $page_link);
1055-
echo '<a href="' . $category_link . '" class="category"> ' . $category->name . '<br>; </a>';
1056-
}
1057-
echo '</div>';
1057+
echo '</div>';
1058+
}
1059+
if ($selected_page_id != 0) {
1060+
$page_link = get_permalink($selected_page_id);
1061+
$categories = get_the_category($post->ID);
1062+
if (!empty($categories)) {
1063+
foreach ($categories as $category) {
1064+
$category_link = add_query_arg('category', $category->slug, $page_link);
1065+
echo '<a href="' . $category_link . '" class="category"> ' . $category->name . ' </a>';
10581066
}
1059-
} else {
1060-
$categories = get_the_category($post->ID);
1061-
if (!empty($categories)) {
1062-
echo '<div class="categories-links">';
1063-
foreach ($categories as $category) {
1064-
echo '<a href="' . get_category_link($category->term_id) . '" class="category"> ' . $category->name . '&nbsp; </a>';
1065-
}
1066-
echo '</div>';
1067+
}
1068+
} else {
1069+
$categories = get_the_category($post->ID);
1070+
if (!empty($categories)) {
1071+
foreach ($categories as $category) {
1072+
echo '<a href="' . get_category_link($category->term_id) . '" class="category"> ' . $category->name . ' </a>';
10671073
}
10681074
}
1069-
echo '<a class="description" href="' . $post_link . '">' . $post_content . ' </a>
1070-
</div>
1071-
</div>';
10721075
}
1073-
wp_reset_postdata();
1074-
echo '</div>';
1076+
echo '<a class="description" href="' . $post_link . '">' . $post_content . ' </a>
1077+
</div>
1078+
</div>';
10751079
}
1076-
} else {
1080+
wp_reset_postdata();
1081+
echo '</div>';
1082+
}
1083+
if (!$posts) {
10771084
// Gestisci il caso in cui $posts non è un array valido
10781085
echo '<div class="error-message">';
10791086
echo esc_html__('No post', 'Latest-Posts-Hover');
@@ -1133,6 +1140,7 @@ protected function render()
11331140
background-position: center center;
11341141
background-size: cover;
11351142
cursor: pointer;
1143+
11361144
}
11371145
11381146
.info {
@@ -1181,11 +1189,11 @@ protected function render()
11811189
11821190
}
11831191
.tag {
1184-
margin-top: 0;
1192+
margin-top: 0px;
11851193
margin-bottom: 0px;
11861194
padding: 0 5px;
11871195
font-size: 18px;
1188-
color: black;
1196+
color:black;
11891197
display:none;
11901198
text-align: left;
11911199
}
@@ -1250,4 +1258,4 @@ protected function render()
12501258
12511259
</style>';
12521260
}
1253-
}
1261+
}

0 commit comments

Comments
 (0)