WordPress搜索框只搜索文章,不搜索页面等其他类型内容

WordPress中对于文章类型非常重要一个标识是post_type,我们在搜索时如果只搜索文章,不需要搜索页面等其他类型的内容,就要用到post_type来区分。

下面是一个简单的搜索所有内容的搜索框:

<form action="/" method="get">
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" placeholder="Search" />
    <button type="submit">Search</button>
</form>

改进之后:

<form action="/" method="get">
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" placeholder="Search" />
    <button type="submit">Search</button>
    <input type="hidden" name="post_type" value="post" />
</form>

这样就只搜索文章了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注