Skip to content

Commit

Permalink
feat: better search results
Browse files Browse the repository at this point in the history
* modifiche pagina risultati

* modifiche al breadcrumb

* modifica functions per il meta title

* aggiunto autocomplete off ai filtri di ricerca

* correzione checkbox dopo back button

* aggiunto autcomplete off ai tiltri della pagina argomenti
  • Loading branch information
rcoopcsc authored Nov 20, 2024
1 parent f9c7b03 commit 73f851d
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 102 deletions.
16 changes: 16 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,19 @@ function dsi_login_redirect( $redirect_to, $request, $user ) {
}

add_filter( 'login_redirect', 'dsi_login_redirect', 10, 3 );

//riscrivi il meta title nella pagina dei risultati della ricerca
function custom_search_title($title) {
if ( is_search() ) {
// Get the search query
$search_query = get_search_query();

if ( empty($search_query) ) {
$title['title'] = esc_html__('Risultati della ricerca', 'design_scuole_italia');
} else {
$title['title'] = sprintf(esc_html__('Risultati della ricerca per "%s"', 'design_scuole_italia'), $search_query);
}
}
return $title;
}
add_filter('document_title_parts', 'custom_search_title');
292 changes: 200 additions & 92 deletions search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,107 +9,215 @@

get_header();
?>
<main id="main-container" class="main-container petrol">
<?php get_template_part("template-parts/common/breadcrumb"); ?>

<section class="section bg-white py-2 py-lg-3 py-xl-5">
<div class="container">
<div class="row variable-gutters">
<div class="col-lg-5 col-md-8 offset-lg-3">
<div class="section-title">
<p><?php
if(get_search_query() != "")
_e("Risultati della ricerca per:", "design_scuole_italia");
?></p>
<h1 class="h2 mb-0"><?php if(get_search_query() != "")
echo get_search_query();
else
_e("Ricerca generica", "design-scuole-italia");

?></h1>
<p>
<?php
$str = "";
if(isset($_GET["type"]) && $_GET["type"] != "") {
if ( $_GET["type"] == "any" ) {
$str = __( "su <span>tutto il sito</span>", "design_scuole_italia" );
} else if ( $_GET["type"] == "school" ) {
$str = __( "nel <span>materiale relativo alla scuola</span>", "design_scuole_italia" );
} else if ( $_GET["type"] == "news" ) {
$str = __( "in <span>notizie ed eventi</span>", "design_scuole_italia" );
} else if ( $_GET["type"] == "service" ) {
$str = __( "nei <span>servizi</span>", "design_scuole_italia" );
} else if ( $_GET["type"] == "education" ) {
$str = __( "nella <span>didattica</span>", "design_scuole_italia" );
} else if ( $_GET["type"] == "class" ) {
$str = __( "nelle <span>materiale relativo alle classi</span>", "design_scuole_italia" );
}
}else{
$str = __( "in base ai filtri selezionati", "design_scuole_italia" );
<main id="main-container" class="main-container petrol">
<?php get_template_part("template-parts/common/breadcrumb"); ?>

<section class="section bg-white py-2 py-lg-3 py-xl-5">
<div class="container">
<div class="row variable-gutters">
<div class="col-lg-5 col-md-8 offset-lg-3">
<div class="section-title">
<h1>
<?php
global $wp_query;
// Modifico la risposta in base al numero di risultati
if ($wp_query->found_posts < 1) {
$result = "Nessun risultato";
} else if ($wp_query->found_posts < 2) {
$result = $wp_query->found_posts . " risultato";
} else {
$result = $wp_query->found_posts . " risultati";
}
echo $result . " ";
?>
<?php if (get_search_query() != "")
// Aggiungo il parametro di ricerca preceduto da "per" solo quando serve
echo "per \"" . get_search_query() . "\""; ?>
</h1>

<p class="d-block">

<?php if (isset($_GET["post_types"]) || isset($_GET["post_terms"]) || isset($_GET["type"]) ) {
// Aggiungo la frase "in base ai filtri selezionati:" se ce ne sono
echo "in base ai filtri selezionati:<br>";
} ?>

<?php
// Aggiungo i badge dei filtri corrispondenti al "type"
if (!isset($_GET["post_types"])) {

$post_type_links = array();
$post_types = array();

if (isset($_GET["type"]) && $_GET["type"] == "school") {
$post_types = array("documento", "luogo", "struttura", "page");
}

if (isset($_GET["type"]) && $_GET["type"] == "service") {
$post_types = array("servizio", "indirizzo");
}

if (isset($_GET["type"]) && $_GET["type"] == "education") {
$post_types = array("scheda_didattica", "scheda_progetto");
}

if (isset($_GET["type"]) && $_GET["type"] == "news") {
$post_types = array("evento", "post", "circolare");
}

if (isset($_GET["type"]) && $_GET["type"] == "any") {
$post_types = array("documento", "luogo", "struttura", "page", "servizio", "indirizzo", "scheda_didattica", "scheda_progetto", "evento", "post", "circolare");
}

foreach ($post_types as $post_type_name) {
$post_type = get_post_type_object($post_type_name);

if (!empty($post_type)) {
// Check if the label is "Articoli"
if ($post_type->label === "Articoli") {
// Special link for "Articoli"
$link = 'tipologia-articolo/articoli/';
} else {
// Default link
if (isset($post_type->rewrite['slug']) && is_array($post_type->rewrite)) {
$link = esc_url('/' . $post_type->rewrite['slug']);
} else {
// Handle the case where rewrite or slug is not available
$link = esc_url('/');
}
}

// Escape the label for output
$post_type_links[] = '<a class="badge badge-sm badge-pill badge-outline-primary" href="' . $link . '">' . esc_html($post_type->label) . '</a>';
}
}

echo implode(' ', $post_type_links);
}


if (isset($_GET["post_types"])) {
// Sanitize input
$post_types = array_map('sanitize_text_field', $_GET["post_types"]);

$post_type_links = array();

foreach ($post_types as $post_type_name) {
$post_type = get_post_type_object($post_type_name);

if (!empty($post_type)) {
// Check if the label is "Articoli"
if ($post_type->label === "Articoli") {
// Special link for "Articoli"
$link = 'tipologia-articolo/articoli/';
} else {
// Default link
if (isset($post_type->rewrite['slug']) && is_array($post_type->rewrite)) {
$link = esc_url('/' . $post_type->rewrite['slug']);
} else {
// Handle the case where rewrite or slug is not available
$link = esc_url('/');
}
}

// Escape the label for output
$post_type_links[] = '<a class="badge badge-sm badge-pill badge-outline-primary" href="' . $link . '">' . esc_html($post_type->label) . '</a>';
}
}
echo " ".$str;
?>
</p>
</div><!-- /title-section -->
</div><!-- /col-lg-5 col-md-8 offset-lg-2 -->

<div class="col-lg-3 col-md-4 offset-lg-1">
<?php get_template_part("template-parts/single/actions"); ?>
</div><!-- /col-lg-3 col-md-4 offset-lg-1 -->
</div><!-- /row -->
</div><!-- /container -->
</section><!-- /section -->

<?php
get_template_part( 'template-parts/search/toggle-search-filters-mobile' );
?>

<section class="section bg-gray-light">
<div class="container">
<div class="row variable-gutters sticky-sidebar-container">
<div class="col-lg-3 bg-white bg-white-left">
<?php get_template_part("template-parts/search/filters"); ?>
</div>
<div class="col-lg-7 offset-lg-1 pt84">
<h2 class="sr-only">Risultati di ricerca</h2>
<?php if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/list/article', get_post_type() );

endwhile;


if((get_query_var( 'paged' ) == $wp_query->max_num_pages || $wp_query->max_num_pages == 1) && !isset($_GET["post_terms"])){
$s_query = get_search_query();
get_template_part( 'template-parts/search/argomenti' );

echo implode(' ', $post_type_links);
}
?>

<?php
if (isset($_GET["post_terms"])) {
// aggiungo badges degli argomenti selezionati nel filtro
$post_terms = $_GET["post_terms"];

$term_links = array();

?>
<nav class="pagination-wrapper" aria-label="Navigazione della pagina">
<?php echo dsi_bootstrap_pagination(); ?>
</nav>
<?php
else :
foreach ($post_terms as $term_id) {
$term = get_term($term_id);

get_template_part( 'template-parts/content', 'none' );
if (!is_wp_error($term) && !empty($term)) {
$term_links[] = '<a class="badge badge-sm badge-pill badge-outline-primary" href="argomento/' . $term->slug . '">' . $term->name . '</a>';
}

if(!isset($_GET["post_terms"])) {
$s_query = get_search_query();
get_template_part( 'template-parts/search/argomenti' );
}
echo implode(' ', $term_links);
}
?>


</p>
</div><!-- /title-section -->
</div><!-- /col-lg-5 col-md-8 offset-lg-2 -->

<div class="col-lg-3 col-md-4 offset-lg-1">
<?php get_template_part("template-parts/single/actions"); ?>
</div><!-- /col-lg-3 col-md-4 offset-lg-1 -->
</div><!-- /row -->
</div><!-- /container -->
</section><!-- /section -->



<section class="section bg-white border-top border-bottom d-block d-lg-none">
<div class="container d-flex justify-content-between align-items-center py-3">
<h3 class="h6 text-uppercase mb-0 label-filter"><strong>Filtri</strong></h3>
<a class="toggle-search-results-mobile toggle-menu menu-search push-body mb-0" href="#">
<svg class="svg-filters">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#svg-filters"></use>
</svg>
</a>
</div>
</section>
<section class="section bg-gray-light">
<div class="container">
<div class="row variable-gutters sticky-sidebar-container">
<div class="col-lg-3 bg-white bg-white-left">
<?php get_template_part("template-parts/search/filters"); ?>
</div>
<div class="col-lg-7 offset-lg-1 pt84">
<h2 class="sr-only">Risultati di ricerca</h2>
<?php if (have_posts()): ?>
<?php
/* Start the Loop */
while (have_posts()):
the_post();
get_template_part('template-parts/list/article', get_post_type());

endwhile;


if ((get_query_var('paged') == $wp_query->max_num_pages || $wp_query->max_num_pages == 1) && !isset($_GET["post_terms"])) {
$s_query = get_search_query();
get_template_part('template-parts/search/argomenti');
}

?>
<nav class="pagination-wrapper" aria-label="Navigazione della pagina">
<?php echo dsi_bootstrap_pagination(); ?>
</nav>
<?php
else:

get_template_part('template-parts/content', 'none');

if (!isset($_GET["post_terms"])) {
$s_query = get_search_query();
get_template_part('template-parts/search/argomenti');
}

endif;
endif;


?>
</div><!-- /col-lg-8 -->
</div><!-- /row -->
</div><!-- /container -->
</section>
</main>
?>
</div><!-- /col-lg-8 -->
</div><!-- /row -->
</div><!-- /container -->
</section>
</main>

<?php
get_footer();
4 changes: 2 additions & 2 deletions template-parts/common/breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
'network' => false,
'show_title' => true,
'show_browse' => false,
'labels' => array(
'search' => esc_html__( 'Risultati Ricerca: %s','design_scuole_italia' ),
'labels' => array(
'search' => esc_html__( 'Risultati della ricerca','design_scuole_italia' ),
),
'post_taxonomy' => array(
'servizio' => 'tipologia-servizio', // 'post' post type and 'post_tag' taxonomy
Expand Down
2 changes: 1 addition & 1 deletion template-parts/search/filters-tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
?>
<li>
<div class="form-check my-0">
<input type="checkbox" class="custom-control-input" name="post_types[]" value="<?php echo $type; ?>" id="check-<?php echo $type; ?>" <?php if(in_array($type, $post_types)) echo " checked "; ?> onChange="this.form.submit()">
<input type="checkbox" class="custom-control-input" name="post_types[]" value="<?php echo $type; ?>" id="check-<?php echo $type; ?>" <?php if(in_array($type, $post_types)) echo " checked "; ?> onChange="this.form.submit()" autocomplete="off">
<label class="mb-0" for="check-<?php echo $type; ?>"><?php echo $name; ?></label>
</div>
</li>
Expand Down
Loading

0 comments on commit 73f851d

Please sign in to comment.