主页/WordPress笔记/博客文章/综合文章/如何在 WordPress 搜索结果中包含自定义文章类型

如何在 WordPress 搜索结果中包含自定义文章类型

Bug编译狮

Bug编译狮

在WordPress中,如果你想让搜索结果中的文章不仅包括默认的帖子(post),还包括自定义的文章类型(如页面、分类页等),你需要通过修改搜索查询来实现这一点。

方法一:手动添加搜索参数

  1. 打开 functions.php 文件: 通常位于 /wp-content/themes/your-theme-name/ 目录下。

  2. 编写代码: 在文件中找到或添加以下代码:

function search_custom_post_types($query) {
    if ($query->is_search && $query->is_main_query()) {
        $args = array(
            'post_type' => array('page', 'custom_post_type_name'), // 替换为你的自定义文章类型的名称
            'posts_per_page' => -1, // 显示所有相关文章
        );
        $query->set('s', $query->get('s')); // 继续使用原搜索词
        $query->set('post__not_in', array(0)); // 确保不显示已删除文章
        $query->set('orderby', 'title'); // 排序方式
        $query->set('order', 'ASC');
        $query->set('meta_key', '_menu_order'); // 如果有特定排序依据
        $query->set('meta_value', $query->get('paged'));
        $query->set('paged', $query->get('paged', 0, true));
    }
}
add_action('pre_get_posts', 'search_custom_post_types');
  1. 替换自定义文章类型名称: 将 'custom_post_type_name' 替换为你实际的自定义文章类型名称。

  2. 保存并激活更新: 修改完成后,记得保存并激活您的主题以应用更改。

示例效果

假设您有一个名为“news”的自定义文章类型和一个名为“archive”的自定义文章类型页面。当用户在搜索框中输入关键词时,搜索结果会同时显示这两类文章,而不会影响其他默认文章类型的结果。

注意事项

  • 这个方法只适用于基本的搜索功能,对于更复杂的搜索需求,可能需要使用插件或第三方服务。
  • 对于动态搜索(如使用 WP_Query 或者自定义搜索插件)的高级搜索功能,这种方法可能无法完全满足需求。

希望这个方法能帮助你在WordPress中实现搜索结果包含自定义文章类型的需求!如果你有任何疑问,请随时提问。

黑板前的Bug语法讲师

黑板前的Bug语法讲师

要在 WordPress 的搜索结果中包含自定义文章类型,你需要遵循以下步骤:

1. 安装并配置自定义文章类型

首先,确保你的网站已经安装了自定义文章类型插件。如果你还没有,你可以从 WordPress 插件商店下载并安装一个自定义文章类型插件。

示例代码:

<div class="custom-post-type">
    <h2><?php _e( 'Custom Post Type', 'your-plugin' ); ?></h2>
    <?php the_posts_pagination(); ?>
    <div class="post-list">
        <?php wp_list_categories(array('orderby' => 'name', 'order' => 'ASC')); ?>
    </div>
</div>

这个示例代码创建了一个包含自定义文章类型的列表页面,它会列出当前主题的所有分类。

2. 在 WordPress 中设置自定义文章类型

接下来,在 WordPress 的主题文件中设置自定义文章类型。这通常涉及到修改 functions.php 文件或 wp-config.php 文件中的相关部分。

示例代码:

假设我们有一个名为 my_custom_post_type 的自定义文章类型,并且你想将其命名为 “My Custom Post”.

functions.php 或者 wp-config.php 中添加如下代码:

function my_custom_post_type_setup() {
    register_post_type(
        'my_custom_post_type',
        array(
            'labels' => array(
                'name' => __( 'My Custom Post Type' ),
                'singular_name' => __( 'My Custom Post' ),
                'add_new' => __( 'Add New' ),
                'add_new_item' => __( 'Add New My Custom Post' ),
                'edit_item' => __( 'Edit My Custom Post' ),
                'new_item' => __( 'New My Custom Post' ),
                'view_item' => __( 'View My Custom Post' ),
                'search_items' => __( 'Search My Custom Posts' ),
                'not_found' => __( 'No My Custom Posts found' ),
                'not_found_in_trash' => __( 'No My Custom Posts found in Trash' )
            ),
            'public' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'my-custom-post-type' ),
            'capability_type' => 'post',
            'has_archive' => true,
            'menu_icon' => 'dashicons-format-image',
            'description' => __( 'This is a custom post type.' )
        )
    );
}
add_action( 'after_setup_theme', 'my_custom_post_type_setup' );

在这个例子中,我们将自定义文章类型命名为 “My Custom Post” 并设置了其描述和类别。

3. 更新 WordPress 主题样式

为了使自定义文章类型看起来更像原生文章,需要更新相应主题的样式。这可能涉及修改 .css 文件、使用 CSS 类等。

示例代码:

在您的主题目录下的 .css 文件中,查找有关自定义文章类型的类名,然后删除不相关的类。例如:

.my-custom-post-type { /* 删除与自定义文章类型无关的类 */}

4. 测试自定义文章类型

最后一步是测试自定义文章类型是否按预期工作。可以使用以下代码来验证:

function test_my_custom_post_type() {
    global $wpdb;
    $args = array(
        'post_type' => 'my_custom_post_type',
        'posts_per_page' => -1,
    );
    $result = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type='my_custom_post_type'");
    if (count($result) > 0) {
        echo "Test passed: The custom post type is working as expected.";
    } else {
        echo "Test failed: There are no posts of this custom post type.";
    }
}

test_my_custom_post_type();

总结

通过以上步骤,你可以在 WordPress 的搜索结果中包含自定义文章类型。记住,每个自定义文章类型的实现都是独一无二的,具体取决于你的需求和网站设计。