- Home
- Categorie
- Coding e Sistemistica
- PHP
- Ricerca concatenata categorie wordpress
-
Ricerca concatenata categorie wordpress
ciao a tutti,
sto cercando di realizzare una personalizzazione su un plugin wordpress.
Avendo infatti creato con wordpress un sito di annunci "classico", suddiviso per categorie città (es. roma) e categorie oggetto (es. abbigliamento donna), vorrei fare in modo che il visitatore, una volta entrato nel sito, abbia a disposizione in prima pagina un piccolo form, dove, selezionando la sua città e la categoria dell'oggetto (es. roma e abbigliamento donna) tramite due menu a tendina, e cliccando sul pulsante "cerca", visualizzi una pagina con presenti tutti i posts comuni alla categoria roma e abbigliamento donna (ossia tutti gli annunci inseriti sia nella categoria roma che abbigliamento donna). Non me ne intendo assolutamente di php, però credo si debba fare una sorta di funzione "and".
Ho provato ad informarmi riguardo ai plugin disponibili, ma l'unico che si avvicina a fare una cosa del genere è "super search", che comunque non consente questo tipo di concatenazione delle categorie nei risultati di ricerca.
Vorrei pertanto modificare questo plugin manualmente in modo da adattarlo alle mie esigenze.
Ho già provveduto a modificare il codice html, che incollo qui di seguito:
[HTML]<form action="annuncivisibili.it" method="get">
<select name="cat" id="ss_dropdown">
<option value="" selected="selected">Città</option>
<option value="14">Agrigento</option>\n<option value="9">Alessandria</option>\n<option value="15">Ancona</option>\n<option value="16">Aosta</option>\n<option value="18">Ascoli Piceno</option>\n<option value="20">Avellino</option>\n<option value="21">Bari</option>\n<option value="23">Barletta</option>\n<option value="25">Belluno</option>\n<option value="26">Benevento</option>\n<option value="27">Bergamo</option>\n<option value="28">Biella</option>\n<option value="29">Bologna</option>\n<option value="30">Bolzano</option>\n<option value="31">Brescia</option>\n<option value="32">Brindisi</option>\n<option value="33">Cagliari</option>\n<option value="34">Caltanissetta</option>\n<option value="35">Campobasso</option>\n<option value="38">Caserta</option>\n<option value="39">Catania</option>\n<option value="53">Cesena</option>\n<option value="41">Chieti</option>\n<option value="43">Cosenza</option>\n<option value="48">Fermo</option>\n<option value="49">Ferrara</option>\n<option value="50">Firenze</option>\n<option value="51">Foggia</option>\n<option value="52">Forlì</option>\n<option value="54">Frosinone</option>\n<option value="55">Genova</option>\n<option value="57">Grosseto</option>\n<option value="58">Imperia</option>\n<option value="62">Latina</option>\n<option value="63">Lecce</option>\n<option value="64">Lecco</option>\n<option value="65">Livorno</option>\n<option value="67">Lucca</option>\n<option value="69">Mantova</option>\n<option value="70">Massa Carrara</option>\n<option value="72">Matera</option>\n<option value="73">Messina</option>\n<option value="74">Milano</option>\n<option value="75">Modena</option>\n<option value="76">Monza</option>\n<option value="77">Napoli</option>\n<option value="78">Novara</option>\n<option value="80">Olbia Tempio</option>\n<option value="82">Padova</option>\n<option value="83">Palermo</option>\n<option value="84">Parma</option>\n<option value="85">Pavia</option>\n<option value="86">Perugia</option>\n<option value="87">Pesaro-Urbino</option>\n<option value="88">Pescara</option>\n<option value="90">Pisa</option>\n<option value="96">Ravenna</option>\n<option value="98">Reggio Emilia</option>\n<option value="101">Roma</option>\n<option value="102">Rovigo</option>\n<option value="103">Salerno</option>\n<option value="106">Savona</option>\n<option value="107">Siena</option>\n<option value="108">Siracusa</option>\n<option value="110">Taranto</option>\n<option value="113">Torino</option>\n<option value="115">Trapani</option>\n<option value="117">Treviso</option>\n<option value="119">Udine</option>\n<option value="120">Varese</option>\n<option value="121">Venezia</option>\n<option value="122">Verbano Cusio Ossola</option>\n<option value="123">Vercelli</option>\n<option value="124">Verona</option>\n<option value="125">Vibo Valentia</option>\n<option value="126">Vicenza</option>\n </select><select name="cat" id="ss_dropdown">
<option value="" selected="selected">Categoria Oggetto</option>
<option value="129">Abbigliamento Bambino</option>\n<option value="7">Abbigliamento Donna</option>\n<option value="138">ABBIGLIAMENTO E ACCESSORI</option>\n<option value="128">Abbigliamento Uomo</option>\n<option value="168">Accessori Cellulari</option>\n<option value="239">Accessori Moto</option>\n<option value="191">Affitto</option>\n<option value="234">Agriturismi</option>\n<option value="212">Altri Animali e Accessori</option>\n<option value="206">Altri Servizi e Professionisti</option>\n<option value="192">Altro</option>\n<option value="176">Altro Elettronica e Computer</option>\n<option value="182">Altro Film, Libri e Musica</option>\n<option value="223">Altro Hobby e Tempo Libero</option>\n<option value="188">Altro Infanzia</option>\n<option value="229">Altro Lezioni e Corsi</option>\n<option value="235">Altro Vacanze e Viaggi</option>\n<option value="207">ANIMALI E ACCESSORI</option>\n<option value="147">ARREDAMENTO</option>\n<option value="150">Arredo Camera da Letto</option>\n<option value="151">Arredo Cucine</option>\n<option value="149">Arredo Soggiorno</option>\n<option value="139">ARTE E ANTIQUARIATO</option>\n<option value="200">Assistenza Informatica e Siti Internet</option>\n<option value="214">Attrezzature</option>\n<option value="154">AUTO</option>\n<option value="156">Auto Usate</option>\n<option value="199">Babysitter</option>\n<option value="203">Badanti</option>\n<option value="216">Biciclette</option>\n<option value="208">Cani</option>\n<option value="232">Case Vacanze</option>\n<option value="317">CATEGORIA OGGETTO</option>\n<option value="195">Cerco Lavoro</option>\n<option value="220">Collezionismo</option>\n<option value="172">Consolle</option>\n<option value="164">Desktop e Notebook</option>\n<option value="175">Elettrodomestici</option>\n<option value="163">ELETTRONICA E COMPUTER</option>\n<option value="221">Eventi</option>\n<option value="180">Film DVD, Blu-Ray</option>\n<option value="177">FILM, LIBRI E MUSICA</option>\n<option value="227">Formazione Professionale</option>\n<option value="209">Gatti</option>\n<option value="184">Giocattoli</option>\n<option value="219">HOBBY E TEMPO LIBERO</option>\n<option value="233">Hotel</option>\n<option value="189">IMMOBILI</option>\n<option value="183">INFANZIA</option>\n<option value="193">LAVORO CERCO OFFRO</option>\n<option value="187">Letti e Culle</option>\n<option value="224">LEZIONI E CORSI</option>\n<option value="226">Lezioni Lingue Straniere</option>\n<option value="178">Libri</option>\n<option value="173">Macchine Fotografiche</option>\n<option value="143">Mobili Antichi</option>\n<option value="236">MOTO</option>\n<option value="237">Moto Nuove</option>\n<option value="238">Moto Usate</option>\n<option value="222">Musicisti e Artisti</option>\n<option value="202">Noleggio Auto</option>\n<option value="231">Offerte Bed and Breakfast</option>\n<option value="194">Offro Lavoro</option>\n<option value="148">Oggettistica Arredo</option>\n<option value="198">Organizzazione Eventi</option>\n<option value="85">Pavia</option>\n<option value="86">Perugia</option>\n<option value="140">Quadri e Disegni</option>\n<option value="225">Ripetizioni Private</option>\n<option value="197">Ristrutturazioni ed Edilizia</option>\n<option value="134">Scarpe Donna</option>\n<option value="1">Senza categoria</option>\n<option value="196">SERVIZI E PROFESSIONISTI</option>\n<option value="165">Software</option>\n<option value="213">SPORT</option>\n<option value="144">Statue e Sculture</option>\n<option value="179">Strumenti Musicali</option>\n<option value="174">Telecamere</option>\n<option value="167">Telefoni Cellulari</option>\n<option value="204">Traduzioni</option>\n<option value="230">VACANZE E VIAGGI</option>\n<option value="153">Varie Arredamento</option>\n<option value="146">Varie Arte e Antiquariato</option>\n<option value="162">Varie Auto</option>\n<option value="190">Vendita</option>\n<option value="170">Videogiochi Computer</option>\n </select><input type="submit" value="Search" /> </form>[/HTML]
Allo stato attuale, il plugin prende in considerazione soltanto il secondo menu a tendina (in questo caso la categoria dell'oggetto), ignorando completamente la città, e mostrando pertanto i posts contenuti nella categoria dell'oggetto selezionata.
Qualcuno riuscirebbe gentilmente a darmi una mano?
Incollo qui di seguito il contenuto dei files php del plugin:
functions.php
[PHP]<?php
#output child categories
function ss_get_child($parent){
$active = get_option('ss_exclude_child');
$args = array(
'type' => 'post',
'child_of' => $parent,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false );$categories = get_categories($args);
$return = "";
if($active == 1){
foreach($categories as $cat){
$return .= ",-".$cat->term_id;
}
}
return $return;
}#generate options for the forum
function ss_get_options($echo){
$catmode = get_option('ss_easy_mode');
$ezmode = get_option('ss_show_children');
$mancats = get_option('ss_manual_cats');
$categories = get_categories();
$output = "";
if(empty($mancats)){
$catmode == 1;
}#check if the user checked "easy mode"
if ($catmode == 1){#if they did, check if they chose to include child cats if($ezmode == 0) { without child cats foreach($categories as $category){ if ($category->parent == 0){ $child = ss_get_child($category->term_id); $output .= '<option value="'.$category->term_id.$child.'">'.$category->name.'</option>\n'; } } } else { with child cats foreach($categories as $category){ $child = ss_get_child($category->term_id); $output .= '<option value="'.$category->term_id.$child.'">'.$category->name.'</option>\n'; } }
} else {
the manually selected cats
foreach($categories as $category){
if(in_array($category->name, $mancats)){
$child = ss_get_child($category->term_id);
$output .= '<option value="'.$category->term_id.$child.'">'.$category->name.'</option>\n';
}
}
}
if($echo == 1){
echo $output;
} else {
return $output;
}
}function ss_full_output(){
$templatemode = get_option('ss_template_mode');if ($templatemode == 0){
?>
<form action="<?php bloginfo('wpurl'); ?>" method="get">
<select name="cat" id="ss_dropdown">
<option value="" selected="selected">Select a Category</option>
<?php
ss_get_options("1");
?>
</select>
<input id="ss_searchbox" type="text" value="Search for..." onfocus="if(this.value=='Search for...'){this.value=''}" onblur="if(this.value==''){this.value='Search for...'}" name="s" />
<input type="submit" value="Search" />
</form>
<?php
} else {
$template = get_option('ss_template');
$options = ss_get_options("0");
$url = get_option('siteurl');
$formstart = "<form action='".$url."' method='get'>";
$fullselect = '<select name="cat" id="ss_dropdown"><option value="" selected="selected">Select a Category</option>'.$options.'</select>';
$find = array('<%formstart%>', '<%formend%>', '<%fullselect%>', '<%selectstart%>', '<%options%>', '<%selectend%>', '<%textbox%>', '<%submitbutton%>');
$replace = array( $formstart, '</form>', $fullselect, '<select name="cat" id="ss_dropdown">', $options, '</select>', '<input id="ss_searchbox" type="text" value="Search for..." onfocus="if(this.value=='Search for...'){this.value=''}" onblur="if(this.value==''){this.value='Search for...'}" name="s" />', '<input type="submit" value="Search" />');
$template = str_replace($find, $replace, $template);
echo $template;
}
}
[/PHP]
settings_page:[PHP]<?php
/* Admin Page Get */
function ssoptions(){
$catmode = get_option('ss_easy_mode');
$ezmode = get_option('ss_show_children');
$mancats = get_option('ss_manual_cats');
$categories = get_categories();
$tempmode = get_option('ss_template_mode');
$temp = get_option('ss_template');
$exchild = get_option('ss_exclude_child');if(isset($_POST['hidden'])){ if( $_POST['hidden'] == 'Y' ) { $ezmode = $_POST['child']; $catmode = $_POST['easy']; $mancats = $_POST['cat']; $tempmode = $_POST['tmode']; $temp = $_POST['temp']; if (get_magic_quotes_gpc()){ $temp = stripslashes($temp); } $exchild = $_POST['exchild']; sort($mancats); update_option( 'ss_show_children', $ezmode); update_option( 'ss_easy_mode', $catmode); update_option( 'ss_manual_cats', $mancats); update_option( 'ss_template_mode', $tempmode); update_option( 'ss_template', $temp); update_option('ss_exclude_child', $exchild); // Put an options updated message on the screen ?><div class="updated"><p><strong>Your Settings Have Been Saved!</strong></p></div><?php }} ?>
<div class="wrap">
<h2>Super Search Settings</h2>
<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="hidden" value="Y"><!-- Easy vs Manual -->
<div style="background-color: #fdfdfb; border: 1px solid #e8e6dd;-moz-border-radius: 5px; -webkit-border-radius: 5px; margin: 0 0 5px 0; padding: 3px 5px;">
<strong>Easy category setup, or manually select them?</strong>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" /><p style="margin: 0px;">
<?php if($catmode == 1){ ?>
<input type="radio" name="easy" value="1" checked />
<label>Easy Categories</label>
<input type="radio" name="easy" value="0" />
<label>Manually Select</label>
</p><?php }else{ ?>
<input type="radio" name="easy" value="1" />
<label>Easy Categories</label>
<input type="radio" name="easy" value="0" checked/>
<label>Manually Select</label>
</p>
<?php } ?>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">Easy categories mode will allow you to quickly setup the categories using two presets: with subcategories, or without subcategories. If this doesn't work for you, choose manual mode and choose which categories you would like to appear in the drop-down list.</p>
</div>
<!-- End Easy vs Manual --><!-- Exclude child -->
<div style="background-color:; border: 1px solid;-moz-border-radius: 5px; -webkit-border-radius: 5px; margin: 0 0 5px 0; padding: 3px 5px;">
<strong>Exclude Child Categories From Results?</strong>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" /><p style="margin: 0px;">
<?php if($exchild == 1){ ?>
<input type="radio" name="exchild" value="1" checked />
<label>Exclude Child Categories</label>
<input type="radio" name="exchild" value="0" />
<label>Include Child Categories</label>
</p><?php }else{ ?>
<input type="radio" name="exchild" value="1" />
<label>Exclude Child Categories</label>
<input type="radio" name="exchild" value="0" checked/>
<label>Include Child Categories</label>
</p>
<?php } ?>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">Enabling this will prevent posts from child categories from showing up from results. Please note that if a post is in both the parent category being searched for AND one of its child categories, it will NOT show up.</p>
</div>
<!-- Exclude child --><!-- Easy Mode Selection -->
<div style="background-color:; border: 1px solid;-moz-border-radius: 5px; -webkit-border-radius: 5px; margin: 0 0 5px 0; padding: 3px 5px;">
<strong>Easy Mode</strong>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">
<?php if($ezmode == 1){ ?>
<input type="radio" name="child" value="1" checked />
<label>Include Child Categories</label>
<input type="radio" name="child" value="0" />
<label>Exclude Child Categories</label>
</p><?php }else{ ?>
<input type="radio" name="child" value="1" />
<label>Include Child Categories</label>
<input type="radio" name="child" value="0" checked/>
<label>Exclude Child Categories</label>
</p>
<?php } ?>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">If you have decided to use the "Easy" mode for category selection, these options let you decide if you'd like to have child categories included or not. If you chose manual selection mode, these options will not affect the outcome of the drop-down.</p>
</div>
<!-- End Easy Mode --><!-- Manual Selection -->
<div style="background-color:; border: 1px solid;-moz-border-radius: 5px; -webkit-border-radius: 5px; margin: 0 0 5px 0; padding: 3px 5px;">
<strong>Manual Category Selection</strong>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">
<?php if(empty($mancats)){
foreach($categories as $cat){
echo '<input type="checkbox" name="cat[]" value="'.$cat->name.'" checked /><label>'.$cat->name.'</label><br />';
}
}else{
foreach($categories as $cat){
if(in_array($cat->name, $mancats)){
echo '<input type="checkbox" name="cat[]" value="'.$cat->name.'" checked /><label>'.$cat->name.'</label><br />';
}else{
echo '<input type="checkbox" name="cat[]" value="'.$cat->name.'" /><label>'.$cat->name.'</label><br />';
}
}
} ?>
</p>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">If you have selected "Manual Category Selection" mode above, you can use these options to decide which categories you would like to show up, and which ones you wouldn't. It's all up to you!</p>
</div>
<!-- End Manual Selection --><hr>
<!-- Templating Options -->
<div style="background-color:; border: 1px solid;-moz-border-radius: 5px; -webkit-border-radius: 5px; margin: 0 0 5px 0; padding: 3px 5px;">
<strong>Do you want to use the default template, or make your own?</strong>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" />
<p style="margin: 0px;">
<?php if($tempmode == 0){ ?>
<input type="radio" name="tmode" value="0" checked />
<label>Use the Default Template</label>
<input type="radio" name="tmode" value="1" />
<label>Make my Own</label>
</p><?php }else{ ?>
<input type="radio" name="tmode" value="0" />
<label>Use the Default Template</label>
<input type="radio" name="tmode" value="1" checked/>
<label>Make my Own</label>
</p>
<?php } ?>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" /><p style="margin: 0px;">
<textarea name="temp" rows="5" cols="100"><?php echo $temp; ?></textarea>
</p>
<hr color="#e8e6dd" size="1px" style="margin: 3px 0; padding: 0px;" /><p style="margin: 0px;">To edit the template, there are various quick codes to use:<br />
<strong><%formstart%></strong> - Creates the beginning <form> tag<br />
<strong><%formend%></strong> - Closes the <form> tag<br />
<strong><%fullselect%></strong> - Creates the select and options tags using the default classes<br />
<strong><%selectstart%></strong> - Creates opening <select> tags with the default classes (if you want to create your own select manually, set its name to "cat")<br />
<strong><%selectend%></strong> - Closes the <select> tag<br />
<strong><%options%></strong> - Adds the <option> tags into the select tag<br />
<strong><%textbox%></strong> - Creates the text box input tag with the default class (use the name of "s" if you want to create your own textbox manually)<br />
<strong><%submitbutton%></strong> - Creates the submit button tag<br />
</p>
</div><!-- End Templating Options -->
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Update Options', 'mt_trans_domain' ) ?>" />
</p></form>
</div>
<?php } ?>[/PHP]supersearch.php
[PHP]<?php
/*
Plugin Name: Super Search
Plugin URI: canhaswebsite.com/super-search
Description: This plugin lets your users search for posts in specific categories and child categories
Author: mc2w
Version: 1.2
Author URI: wiflba.com
*//* Adding some variables and menu pages to wordpress */
add_option('ss_show_children', '0', '', 'yes');
add_option('ss_easy_mode', '1', '', 'yes');
add_option('ss_manual_cats', '', '', 'yes');
add_option('ss_template_mode', '0', '', 'yes');
add_option('ss_template', '<%formstart%><%fullselect%><%textbox%><%submitbutton%><%formend%>', '', 'yes');
add_action('admin_menu','modify_menu');
add_option('ss_exclude_child', '0', '', 'yes');include('settings_page.php');
include('functions.php');/* Makes the settings page visible in Wordpress */
function modify_menu(){
add_options_page('Super Search Settings', 'Super Search', 8, 'supersearch', 'ssoptions');
}class SSWIDGET extends WP_Widget {
function SSWidget() {
parent::WP_Widget(false, $name = 'Super Search Block');
}function widget($args, $instance) { extract( $args ); $title = apply_filters('widget_title', $instance['title']); echo $before_widget; if ($title){ echo $before_title . $title . $after_title; } supersearch(); echo $after_widget; } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); return $instance; } function form($instance) { $title = esc_attr($instance['title']); ?> <p> <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> </label> </p> <?php }
}
add_action('widgets_init', create_function('', 'return register_widget("SSWIDGET");'));
function supersearch(){
ss_full_output();
}?>
[/PHP]Grazie anticipate.
-
Ciao erremetal,
Lo script prende in considerazione solo la seconda tendina perché le due select box hanno lo stesso name. Quando il form viene inviato, il secondo valore sovrascrive il primo, e nell'array $_POST trovi un solo valore 'cat', che corrisponde alla selezione della seconda select.Ciao,
LucaP.s. Ho inserito nel tuo post i tag html e php, così il codice ora è più leggibile. Cerca di utilizzarli sempre quando riporti del codice, sono buoni amici per ottenere più facilmente risposte alle tue domande
-
Ciao Luca
grazie della risposta...anche se sono un lamer totale per quanto riguarda il php, credo di avere un pò capito la situazione.
Quindi perchè mi prenda in considerazione entrambi i menu a tendina e mi faccia vedere soltanto i post comuni ad entrambi le categorie selezionate, devo ad es. chiamare il primo select box in modo diverso (es. cat2), giusto?
E che codice devo aggiungere/modificare ai vari $_POST (suppongo) contenuti nella settings_page?
E' una cosa molto complessa da fare o modificando tre righe di codice si fa?:?
P.S. Si, scusa per il codice postato in modo disordinato, ma non so bene come funziona il forum, sono niubbo, porta pazienza