@@ -83,6 +83,15 @@ protected function _register_controls()
8383 'step ' => 0.1 ,
8484 ]
8585 );
86+ $ this ->add_control (
87+ 'selected_page ' ,
88+ [
89+ 'label ' => esc_html__ ('Select Page if you have a page with all post, made with this widget ' , 'OpenWidget ' ),
90+ 'type ' => \Elementor \Controls_Manager::SELECT ,
91+ 'options ' => $ this ->get_pages (),
92+ 'default ' => '0 ' ,
93+ ]
94+ );
8695 $ this ->end_controls_section ();
8796 $ this ->start_controls_section (
8897 'section_title ' ,
@@ -299,7 +308,7 @@ protected function _register_controls()
299308 'type ' => \Elementor \Controls_Manager::SWITCHER ,
300309 'label_on ' => esc_html__ ('On ' , 'Latest-Posts-Hover ' ),
301310 'label_off ' => esc_html__ ('Off ' , 'Latest-Posts-Hover ' ),
302- 'return_value ' => 'block ' ,
311+ 'return_value ' => 'inline- block ' ,
303312 'default ' => 'none ' ,
304313 'selectors ' => [
305314 '{{WRAPPER}} .category ' => 'display: {{VALUE}}; ' ,
@@ -523,7 +532,7 @@ protected function _register_controls()
523532 'type ' => \Elementor \Controls_Manager::SWITCHER ,
524533 'label_on ' => esc_html__ ('On ' , 'Latest-Posts-Hover ' ),
525534 'label_off ' => esc_html__ ('Off ' , 'Latest-Posts-Hover ' ),
526- 'return_value ' => 'block ' ,
535+ 'return_value ' => 'flex ' ,
527536 'default ' => 'none ' ,
528537 'selectors ' => [
529538 '{{WRAPPER}} .category-filter ' => 'display: {{VALUE}}; ' ,
@@ -766,6 +775,16 @@ protected function _register_controls()
766775
767776 $ this ->end_controls_section ();
768777 }
778+ private function get_pages ()
779+ {
780+ $ pages = get_pages ();
781+ $ options = [];
782+ $ options [0 ] = esc_html__ ('Default ' , 'OpenWidget ' );
783+ foreach ($ pages as $ page ) {
784+ $ options [$ page ->ID ] = $ page ->post_title ;
785+ }
786+ return $ options ;
787+ }
769788 protected function render ()
770789
771790 {
@@ -781,42 +800,41 @@ protected function render()
781800 $ args = [
782801 'posts_per_page ' => $ settings ['posts_per_page ' ],
783802 ];
803+ } else {
804+ $ args = [
805+ 'posts_per_page ' => $ settings ['posts_per_page ' ],
806+ 'category_name ' => isset ($ _GET ['category ' ]) ? sanitize_text_field ($ _GET ['category ' ]) : '' ,
807+
808+ ];
784809 }
785810 if ($ settings ['all_post ' ] == 'all ' ) {
786-
787- if (isset ($ _GET ['category ' ]) && ($ _GET ['category ' ]) == "all " ) {
788- $ args = [
789- 'posts_per_page ' => -1 ,
790- ];
791- }
792811 $ args = [
793812 'posts_per_page ' => -1 ,
794- 'category_name ' => isset ($ _GET ['category ' ]) ? sanitize_text_field ($ _GET ['category ' ]) : '' ,
795-
796813 ];
797814 }
815+
798816 $ posts = get_posts ($ args );
799817 $ opacity = $ settings ['card_opacity ' ];
800818 $ cardColor = $ settings ['card_color ' ];
801819 $ wordPc = $ settings ['content_word_pc ' ];
802820 $ wordMobile = $ settings ['content_word_mobile ' ];
803- $ flex = 100 / count ($ posts );
804-
805- if ( count ($ posts ) > 4 ) {
821+ if (count ($ posts ) != 0 ) {
822+ $ flex = 100 / count ($ posts );
823+ }
824+ if (count ($ posts ) > 4 || count ($ posts ) == 0 ) {
806825 $ flex = 25 ;
807826 }
808827 $ widht = $ flex - 1 ;
809828 $ title_color = $ settings ['Title_color ' ];
810829 if ($ posts ) {
811-
812830 echo '<div class="category-filter">
813831 <form method="get" action="">
814832 <button type="submit" name="category" value="all" class="category-filter-button ' ;
815833 if (isset ($ _GET ['category ' ]) && $ _GET ['category ' ] == 'all ' ) {
816834 echo ' active ' ;
817835 }
818836 echo '">All</button> ' ;
819- $ categories = get_categories ();
837+ $ categories = get_categories ([ ' hide_empty ' => 0 ] );
820838 foreach ($ categories as $ category ) {
821839 $ category_name = $ category ->name ;
822840 $ category_slug = $ category ->slug ;
@@ -827,10 +845,8 @@ protected function render()
827845 echo '"> ' . $ category_name . '</button> ' ;
828846 }
829847 echo '</form> </div> ' ;
830- if (isset ($ _GET ['category ' ]) && $ _GET ['category ' ] != 'all ' ) {
831- $ category = get_category_by_slug ($ _GET ['category ' ]);
832- }
833- echo '<div class="card2-container"> ' ;
848+ echo '<div class="card2-container"> ' ;
849+ $ selected_page_id = $ settings ['selected_page ' ];
834850 if (wp_is_mobile ()) {
835851 // Codice da eseguire se la larghezza dello schermo è minore o uguale a 900 pixel
836852
@@ -846,23 +862,31 @@ protected function render()
846862 // If not, use the custom default image
847863 $ featured_image = $ settings ['default_image ' ]['url ' ];
848864 }
849- $ categories = get_the_category ($ post ->ID );
850- if (!empty ($ categories )) {
851- $ category_name = array ();
852- foreach ($ categories as $ category ) {
853- $ category_name [] = $ category ->name ;
854- }
855- $ category_string = implode (', ' , $ category_name );
856- } else {
857- $ category_name = '' ;
858- }
865+ echo '<div class="card2" style="background-image: url( ' . $ featured_image . '); "> ' ;
859866 echo '
860- <div class="card2" style="background-image: url( ' . $ featured_image . ')">
861867 <div class="info">
862868 <a class="title" href=" ' . $ post_link . '"> ' . $ post_title . ' <a/>
863869 <p class="date"> ' . $ post_date . '</p> ' ;
864- if ($ category_name != '' ) {
865- echo '<a class="category" role="button" tabindex="0" href="google.com"> ' . $ category_string . '</a> ' ;
870+ if ($ selected_page_id != 0 ) {
871+ $ page_link = get_permalink ($ selected_page_id );
872+ $ categories = get_the_category ($ post ->ID );
873+ if (!empty ($ categories )) {
874+ echo '<div class="categories-links"> ' ;
875+ foreach ($ categories as $ category ) {
876+ $ category_link = add_query_arg ('category ' , $ category ->slug , $ page_link );
877+ echo '<a href=" ' . $ category_link . '" class="category"> ' . $ category ->name . '</a> ' ;
878+ }
879+ echo '</div> ' ;
880+ }
881+ } else {
882+ $ categories = get_the_category ($ post ->ID );
883+ if (!empty ($ categories )) {
884+ echo '<div class="categories-links"> ' ;
885+ foreach ($ categories as $ category ) {
886+ echo '<a href=" ' . get_category_link ($ category ->term_id ) . '" class="category"> ' . $ category ->name . '</a> ' ;
887+ }
888+ echo '</div> ' ;
889+ }
866890 }
867891 echo '<a class="description" href=" ' . $ post_link . '"> ' . $ post_content . ' </a>
868892 </div>
@@ -882,34 +906,40 @@ protected function render()
882906 // If not, use the custom default image
883907 $ featured_image = $ settings ['default_image ' ]['url ' ];
884908 }
885- $ categories = get_the_category ($ post ->ID );
886- if (!empty ($ categories )) {
887- $ category_name = array ();
888- foreach ($ categories as $ category ) {
889- $ category_name [] = $ category ->name ;
890- }
891- $ category_string = implode (', ' , $ category_name );
909+ if (is_admin ()) {
910+ echo '<div class="card2" style="background-image: url( ' . $ featured_image . '); "> ' ;
892911 } else {
893- $ category_name = '' ;
894- }
895- if (!is_admin ()){
896- echo ' <div class="card2" style="background-image: url( ' . $ featured_image . '); "onclick= \'window.location.href=" ' . $ post_link . '" \'> ' ;
897- }
898- if (is_admin ()){
899- echo '<div class="card2" style="background-image: url( ' . $ featured_image . ')"> ' ;
912+ echo ' <div class="card2" style="background-image: url( ' . $ featured_image . '); "onclick= \'window.location.href=" ' . $ post_link . '" \'> ' ;
900913 }
901914 echo '
902915 <div class="info">
903916 <a class="title" href=" ' . $ post_link . '"> ' . $ post_title . ' <a/>
904917 <p class="date"> ' . $ post_date . '</p> ' ;
905- if ($ category_name != '' ) {
906- echo '<a class="category" href="google.com"> ' . $ category_string . '</a> ' ;
918+ if ($ selected_page_id != 0 ) {
919+ $ page_link = get_permalink ($ selected_page_id );
920+ $ categories = get_the_category ($ post ->ID );
921+ if (!empty ($ categories )) {
922+ echo '<div class="categories-links"> ' ;
923+ foreach ($ categories as $ category ) {
924+ $ category_link = add_query_arg ('category ' , $ category ->slug , $ page_link );
925+ echo '<a href=" ' . $ category_link . '" class="category"> ' . $ category ->name . '</a> ' ;
926+ }
927+ echo '</div> ' ;
928+ }
929+ } else {
930+ $ categories = get_the_category ($ post ->ID );
931+ if (!empty ($ categories )) {
932+ echo '<div class="categories-links"> ' ;
933+ foreach ($ categories as $ category ) {
934+ echo '<a href=" ' . get_category_link ($ category ->term_id ) . '" class="category"> ' . $ category ->name . '</a> ' ;
935+ }
936+ echo '</div> ' ;
937+ }
907938 }
908939 echo '<a class="description" href=" ' . $ post_link . '"> ' . $ post_content . ' </a>
909940 </div>
910941 </div> ' ;
911942 }
912-
913943 wp_reset_postdata ();
914944 echo '</div> ' ;
915945 }
@@ -924,7 +954,7 @@ protected function render()
924954 echo '<style>
925955 .category-filter {
926956 display: none;
927- justify-content:left ;
957+ justify-content:right ;
928958 flex-wrap: wrap;
929959 }
930960
@@ -1035,7 +1065,7 @@ protected function render()
10351065 font-size: 18px;
10361066 color:black;
10371067 display:none;
1038- text-align: left ;
1068+ text-align: center ;
10391069 }
10401070
10411071 .description {
0 commit comments