简体   繁体   English

WordPress-在通过博客帖子进行的搜索中包括高级自定义字段(ACF),并在搜索结果中显示这些字段

[英]WordPress - Including an Advanced Custom Field (ACF) in a search through blog posts and displaying the fields in the search results

I have a particularly tricky question to put forwards. 我有一个特别棘手的问题要提出。 Hopefully this doesn't become too niche that it cannot help everyone. 希望这不会成为小众市场,以至于不能帮助所有人。

I have created a custom search that will only display results from a specified category. 我创建了一个自定义搜索,将仅显示指定类别的结果。 If you would like to do the same follow steps 1 & 2. 如果您要这样做,请按照步骤1和2进行操作。

  1. Create a form for a search: 创建搜索表单:

Save it as advanced.php in the root of your theme / child theme. 将其另存为theme / child主题的根目录下的advanced.php。 You will already have a search.php file in this directory. 您将在此目录中已经有一个search.php文件。

/blog/ is the route to your blog posts - if you receive no results check this. / blog /是通往博客帖子的路径-如果未收到结果,请选中此复选框。

The value of cat_slug becomes your category in which it will search. 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>
  1. Add to functions.php: 添加到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'); add_action('pre_get_posts','advanced_search_query');

Now the search results are narrowed down. 现在缩小搜索范围。 I need to adjust which elements of the post I can see. 我需要调整可以看到的帖子元素。

At this point I have created my blogs relevant to this search with a custom template utilising ACF as you can see in my form the category has the slug 'case-study'. 此时,我已经使用ACF使用自定义模板创建了与此搜索相关的博客,如您在我的表格中所见,该类别具有“ case-study”。 We don't require all the fields I have used. 我们不需要我使用的所有字段。 The fields relevant to the search are: repeater - case_study_page_content sub_fields - title sub_fields - author sub_fields - content 与搜索相关的字段是:中继器-case_study_page_content子字段-标题子字段-作者子字段-内容

I have began editing my content.php file contained within the template-parts directory to display the results, this is looking like this: 我已经开始编辑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>

This is where I have ran into my complication as I am unable to just call the_field('author'); 这是我遇到麻烦的地方,因为我无法仅调用the_field('author');。 into the results loop. 进入结果循环。

Has anyone else been able to successfully pull values from within ACF's used in a custom blog page template into the search results page? 其他人是否能够将自定义博客页面模板中使用的ACF中的值成功提取到搜索结果页面中?

Thanks so much in advance, this is a little above my brains capacity and any help provided will be massively appreciated. 在此先多谢,这超出了我的能力范围,任何帮助将不胜感激。 Jason. 杰森。

Thanks to the direction of @Stender and @siddhesh - I have snapped out of my insanity. 感谢@Stender和@siddhesh的指导-我已经摆脱了精神错乱。

I was under the impression I had to allow ACF to be searchable when in fact you can call them already. 我的印象是必须允许ACF进行搜索,而实际上您已经可以调用它们。 My content.php file now looks like this: 我的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在自定义搜索中包含高级自定义字段 (ACF) - Wordpress - Including Advanced Custom Fields (ACF) in a Custom Search - Wordpress 自定义分类法中通过ACF字段的Wordpress搜索表单和搜索结果 - Wordpress search form and search result through ACF field in custom taxonomy 添加 ACF 字段到搜索结果页面 WordPress - Add ACF fields to search results page WordPress 如何使用“高级自定义字段”(ACF wordpress插件)在Google地图中的指定区域或城市内搜索地点 - How to search for a place within specified area or city in Google Maps Using Advanced Custom Fields (ACF wordpress plugin) WordPress高级自定义字段(ACF)-设置关系字段的样式-CSS - WordPress Advanced Custom Fields (ACF) - Styling the Relationship Field - CSS WordPress高级自定义字段(ACF)未保存 - WordPress Advanced Custom Fields (ACF) not saved 在 Wordpress 中显示带有 ACF(高级自定义字段)的自定义帖子标题的下拉菜单 - Displaying a dropdown menu of custom post titles with ACF (Advanced Custom Fields) in Wordpress ACF Wordpress按类别和自定义字段获取帖子 - ACF Wordpress get posts by category and custom fields WordPress:高级搜索自定义帖子类型和自定义字段 - WordPress: Advanced search for custom post types and custom fields 通过博客循环传递高级自定义字段 - Pass Advanced Custom Fields Through Blog Loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM