php - WordPress - 在搜索博客文章中包含高级自定义字段 (ACF) 并在搜索结果中显示字段
问题描述
我有一个特别棘手的问题要提出。希望这不会变得太小众,以至于无法帮助所有人。
我创建了一个自定义搜索,它只显示指定类别的结果。如果您想执行相同的操作,请执行步骤 1 和 2。
- 为搜索创建一个表单:
在您的主题/子主题的根目录中将其保存为 advanced.php。您将在此目录中拥有一个 search.php 文件。
/blog/ 是您博客文章的路径 - 如果您没有收到任何结果,请检查此。
cat_slug 的值将成为您要搜索的类别。
<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/blog/' ) ); ?>">
<input type="hidden" name="search" value="post">
<input id="search-case-study" class="search-case-study" type="text" value="" placeholder="Search..." name="s" />
<input name="cat_slug" value="case-study" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
添加到functions.php:
function advanced_search_query( $query ) { // check if search AND if "cat_slug" input was present if( $query->is_search() && ! empty( $_GET['cat_slug'] ) ) { // find the category by the slug passed in the input $term = get_category_by_slug( $_GET['cat_slug'] ); // defensive check, in case the category could not be found if ( ! empty( $term->term_id ) ) { // get the category ID $cat_id = $term->term_id; // set the query argument to search within the category $query->set( 'cat', $cat_id ); } } }
add_action('pre_get_posts', 'advanced_search_query');
现在搜索结果被缩小了。我需要调整我可以看到的帖子的哪些元素。
至此,我已经使用 ACF 的自定义模板创建了与此搜索相关的博客,正如您在我的表单中看到的那样,该类别具有 slug 'case-study'。我们不需要我使用的所有字段。与搜索相关的字段是:repeater - case_study_page_content sub_fields - title sub_fields - author sub_fields - content
我已经开始编辑包含在 template-parts 目录中的 content.php 文件以显示结果,如下所示:
<article <?php post_class(); ?> class="blog-post">
<a href="<?php the_permalink(); ?>" class="box-link"></a>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" />
<h2>
<?php the_title(); ?>
</h2>
<h3>
<?php the_field('author'); ?>
</h3>
<!--
<div>
<?php the_excerpt(); ?>
</div>
-->
</article>
这就是我遇到麻烦的地方,因为我不能只调用 the_field('author'); 进入结果循环。
是否有其他人能够成功地将自定义博客页面模板中使用的 ACF 中的值提取到搜索结果页面中?
提前非常感谢,这有点超出我的大脑能力,提供的任何帮助将不胜感激。杰森。
解决方案
感谢@Stender 和@siddhesh 的指导——我已经摆脱了精神错乱。
我的印象是我必须允许 ACF 可搜索,而实际上您已经可以调用它们。我的 content.php 文件现在看起来像这样:
<article <?php post_class(); ?> class="blog-post">
<a href="<?php the_permalink(); ?>" class="box-link"></a>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" />
<h2>
<?php the_title(); ?>
</h2>
<?php
$case_study = get_field('case_study_page_content');
?>
<?php if( $case_study ): ?>
<?php while( have_rows('case_study_page_content') ): the_row();
$case_study_author = get_sub_field('author');
?>
<h3>
<?php echo $case_study_author; ?>
</h3>
<?php endwhile; ?>
<?php endif; ?>
</article>
推荐阅读
- android - 在 Image Asset Studio 中创建启动器图标时出现 anydpi-v26 问题
- java - Java SortMap Comparator 在字母键之后排序数字键
- r - 数据框行名布尔解决方案?
- python - 如果用户输入字符串匹配,则删除文本文件中的某一行
- python-3.x - 尝试使用 Gmail API 发送电子邮件
- haskell - 在 macOS 上构建堆栈
- javascript - 使用 svg.js 清除并重新绘制
- abstract-syntax-tree - 评估 CallExpression
- swift - 列表为空时SwiftUI不执行onInsert?
- powerbi - 无法将报告发布到 Web,无权创建嵌入代码