WordPress自定义帖子类型未显示在搜索结果中

编程入门 行业动态 更新时间:2024-10-12 03:19:55
本文介绍了WordPress自定义帖子类型未显示在搜索结果中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对自定义帖子类型(测验)有疑问。在WordPress中搜索。自定义帖子类型未显示在我的搜索结果页面中。我的搜索结果中仅显示默认的帖子内容。

I have an issue with custom post types(Quiz) & search in WordPress. Custom post types are not showing in my search result page. Only the default post content is showing in my search result.

以下是我使用的代码

functions.php function create_posttype(){

functions.php function create_posttype() {

register_post_type( 'compassquiz', array( 'labels' => array( 'name' => __( 'Compass Quiz' ), 'singular_name' => __( 'Compass Quiz' ) ), 'taxonomies' => array('post_tag'), 'public' => true, 'publicly_queryable' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'free-quiz-bank-exam'), 'query_var' => true, 'exclude_from_search' => false, ) );} add_action( 'init', 'create_posttype' );

functions.php中的附加代码

Additional code in functions.php

//Read Custom Post types in Search function filter_search($query) { if ($query->is_search) { $query->set('post_type', array('post', 'compassquiz')); }; return $query; }; add_filter('pre_get_posts', 'filter_search');

search.php

<?php if ( have_posts() ) : ?> <header class="page-header"> <h1 class="page-title"> <?php printf( esc_html__( 'Search Results for: %s', 'compass' ), '<span>' . get_search_query() . '</span>' ); ?> </h1> </header> <?php while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content', 'search' ); endwhile; the_posts_navigation(); else : get_template_part( 'template-parts/content', 'none' ); endif; ?>

content-search.php

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> </header> <div class="entry-summary"> <?php echo substr(get_the_excerpt(), 0,150) . '...'; echo ('<br><a href="' .get_permalink() . '" class="moretag">Read More &raquo;</a>'); ?> </div><!-- .entry-summary --> </article><!-- #post-## -->

我尝试了WP Fourm&在其他stackoverflow问题中也是如此。

I have tried lots of code found in WP Fourm & also in other stackoverflow questions. But couldn't find a solution that actually work.

推荐答案

在搜索表单中添加此代码。在隐藏字段值中设置自定义帖子类型。

Add this code in search form. set custom post type in hidden field value.

<input type="hidden" name="post_type" value="post type name" />

搜索表单中的多个自定义帖子类型

multiple custom post type in search form

<input type="hidden" name="post_type" value="posttype1, posttype2, posttype3" />

更多推荐

WordPress自定义帖子类型未显示在搜索结果中

本文发布于:2023-11-28 12:49:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   搜索结果   类型   帖子   WordPress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!