Skip to content

Instantly share code, notes, and snippets.

@WordPress-Handbuch
Created March 7, 2019 13:24
Show Gist options
  • Save WordPress-Handbuch/d0736c5ff257f8b45cecb1d2653744a5 to your computer and use it in GitHub Desktop.
Save WordPress-Handbuch/d0736c5ff257f8b45cecb1d2653744a5 to your computer and use it in GitHub Desktop.
404 handler for WordPress search showing posts alternatives related to the page not found, and a search form
<?php
global $wp;
$urlquery = $wp->request;
$urlquery = preg_replace("/(\.*)(html|htm|php)$/","",$urlquery);
$parts = explode('/', $urlquery);
$keyword = end($parts);
echo 'Mal sehen, ob es zum Thema "' . $keyword . '" Blogbeiträge gibt...';
$query = new WP_Query( array( 's' => $keyword ) );
// Fuer das Such-Plugin Relevanssi die Kommentarzeichen der folgenden zwei Zeilen entfernen:
//$query->parse_query( $args );
//relevanssi_do_query( $query );
if(!empty($query->posts)):
?>
<h4>Ist bei diesen Blogbeiträgen etwas Interessantes dabei?</h4>
<ul class="posts-list">
<?php
foreach($query->posts as $single_post) {
$title = apply_filters('the_title', $single_post->post_title);
$url = get_permalink($single_post);
echo '<li><a href="' . $url . '">' . $title .'</a></li>';
}
?>
</ul>
<?php endif;?>
<h4>Falls nicht, dann geben Sie gerne ein anderes Stichwort ein:</h4>
<?php get_search_form(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment