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

时间:2018-11-05 12:53:22

标签: php wordpress search advanced-custom-fields

我有一个特别棘手的问题要提出。希望这不会成为小问题,以至于不能帮助所有人。

我创建了一个自定义搜索,该搜索将仅显示指定类别的结果。如果您要这样做,请按照步骤1和2进行操作。

  1. 创建搜索表单:

将其另存为您的主题/子主题的根目录下的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>
  1. 添加到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使用自定义模板创建了与此搜索相关的博客,正如您在我的表格中所看到的那样,该类别具有“案例研究”功能。我们不需要我使用的所有字段。与搜索相关的字段是: 中继器-case_study_page_content sub_fields-标题 sub_fields-作者 sub_fields-内容

我已经开始编辑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中的值成功提取到搜索结果页面中?

非常感谢,这超出了我的能力范围,我们将不胜感激。杰森。

1 个答案:

答案 0 :(得分:0)

感谢@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>