问题描述:

I am working on my Page not found 404 template. For some reason my conditional doesn't seem to work right:

What I am trying to achieve is to break down the path and search for each expression.

I remove - and _ from the expression.

$s = preg_replace("/(.*)-(html|htm|php|asp|aspx)$/","$1",$wp_query->query_vars['name']);

$posts = query_posts('post_type=any&name='.$s);

$s = str_replace("-"," ",$s);

$s = str_replace("_"," ",$s);

if (count($posts) == 0) {

$posts = query_posts('post_type=any&s='.$s);

}

if (count($posts) > 0) {

echo "<ol><li>";

echo "<p>Were you looking for <strong>one of the following</strong> posts or pages?</p>";

echo "<ul>";

foreach ($posts as $post) {

echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';

}

echo "</ul>";

}

If I type "products_s"

It says:

in Let me help you find what you came here for: Were you looking for

one of the following posts or pages?

Products

So It finds products, which is great.

But if i type in "products_services"

Even though I am expecting to get products and services pages listed as above, it doesn't finds anything.

What am I missing?

Thanks,

网友答案:

I have never used wordpress but I do notice you change the queryposts()

In the first check you do:

$posts = query_posts('post_type=any&name='.$s); //notice &name=

In the second check you do:

$posts = query_posts('post_type=any&s='.$s); // notice &s=

It all depends what is happening inside of queryposts(). Personally I would just pass the string through with '_' '-' and explode('-',$string) on the other side to split them into an array.

网友答案:

The reason it's not working for you is because Wordpress's search only returns posts that meet all the words in the query. So, "products_s" turns into "products s" and both "products" and "s" are in the word Products. So Products gets returned. "products_ducts" would return the same page.

If you had a page named Products and Services, you'd get a result with your second search.

Perhaps you could split the string at the spaces and perform a search for each word and append the results to each other.

相关阅读:
Top