WordPress

kategorie – Wyszukiwanie według tagu, kategorii i autora bez wtyczki

  • 8 lutego, 2017
  • 5 min read
kategorie – Wyszukiwanie według tagu, kategorii i autora bez wtyczki


Tworzę stronę internetową z funkcją wyszukiwania. Zastanawiam się, co powinienem zrobić, aby wyszukiwarka działała tak, jak powinna search everything. Wyszukiwane hasło, które wstawił użytkownik, może trafiać w nazwę tagu posta, kategorię postu, treść postu lub tytuł postu. Tak długo, jak w poście znajduje się słowo trafione przez wyszukiwane hasło, wynik zostanie wyświetlony. Nie zamierzam używać wtyczki ani dodawać przycisku opcji do mojego formularza wyszukiwania. Próbowałem szukać w okolicy, ale nie udało się. Jakieś sugestie i rozwiązanie?

<!DOCTYPE html>
<html>
<?php get_header(); ?>
<body>

<?php $q1 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '5',
    's' => get_search_query()
    )
);

$q2 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '5',
    'tax_query' => array(
            //your query
        )
    )
);

$merged = array_merge($q1, $q2);

?>

<?php echo wp_specialchars($s); ?><br/>
<?php the_search_query(); ?><br/>
<?php the_search_query(); ?>
<br/>
<article>
<p>You have searched for "<?php echo wp_specialchars($s); ?>". We found <?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    $text="<span class="resultsFounds">";
if ( $allsearch->found_posts <= 0 ) {
    $text .= sprintf(__( 'no company' ), $count );
}

elseif ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( '%d related company' ), $count );
} 

else {
    $text .= sprintf(__( '%d related companies' ), $count );
}       
$text .= '</span>'; 
echo $text;
?> with the keyword you searched for. If the results are not what you expected, we suggest you to try for different keywords which related to the company.</p>
<?php if (have_posts()) : ?>
<h2>Keywords : <?php echo wp_specialchars($s); ?><?php 
    /* Search Count */
    $count = $wp_query->found_posts; 
    $text="<span class="resultsFound">";
if ( $count  <= 0 ) {
    $text .= sprintf(__( '( no company )' ), $count );
}

elseif ( $count <= 1 ) {
    $text .= sprintf(__( '( %d company )' ), $count );
} 

else {
    $text .= sprintf(__( '( %d companies )' ), $count );
}       
$text .= '</span>'; 
echo $text;
?></h2>

<?php include("adsRandom.php"); ?>
<?php include("boostBiz/bizAds.php"); ?>

<?php while (have_posts()) : the_post(); ?>
<div class="ncc <?php the_ID(); ?><?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?> new<?php endif; ?><?php if (is_sticky()) { ?> sponsored<?php } ?>" <?php if (is_sticky()) { ?>title="Our Advertiser"<?php } ?>>
<h3 class="excerpt"><a href=" the_permalink() ?>" title="<?php the_title(); ?>"><?php search_title_highlight(); ?></a></h3>
<?php search_excerpt_highlight(); ?>
<p class="excerptInfo"><?php printf( __( 'Listed in %2$s', 'NCC' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?> |<?php
$tags_list = get_the_tag_list( '', ', ' );
if ( $tags_list );
?>
<?php printf( __( ' Located In: %2$s', 'NCC' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?><?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?> | Published <?php echo get_the_date(); ?><?php endif; ?></p>
</div><!--ncc <?php the_ID(); ?>-->

<?php endwhile; ?>
<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>

<?php else : ?>

<h2><?php _e('Keywords : ','NCC'); ?><?php echo wp_specialchars($s); ?><?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    $text="<span class="resultsFound">";
if ( $allsearch->found_posts <= 0 ) {
    $text .= sprintf(__( '( Nothing Found )' ), $count );
}

elseif ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( '( We found %d company )' ), $count );
} 

else {
    $text .= sprintf(__( '( We found %d companies )' ), $count );
}       
$text .= '</span>'; 
echo $text;
?></h2>

<article class="nccSingle">
<p>Your search - "<b><?php echo wp_specialchars($s); ?></b>" - did not match any documents. Possibly, there is no company listed with this keyword. Or, the inserted keyword was wrong in spelling?</p>
<p><b>Suggestions:</b></p>
<ul>
  <li>Make sure all words are spelled correctly.</li>
  <li>Try different keywords.</li>
  <li>Try more general keywords.</li>
</ul>
</article>
<?php endif; ?>
</body>
</html>


Źródło

Warto przeczytać!  kategorie — Czy istnieje sposób na zduplikowanie ślimaków kategorii?