【问题标题】:Wordpress: Show categories the searched posts are inWordpress:显示搜索帖子所在的类别
【发布时间】:2015-02-22 19:12:20
【问题描述】:

在查看了我发现的有关此问题的每个网站后,我将发布此问题。没有一个答案给了我解决这个问题的线索,而且谷歌结果被完全不同的主题所污染。

我想要实现的是显示搜索查询返回的帖子的类别列表。 最终的想法是实现这样的目标。 (请记住,这些城市中的每一个都是一个类别)

用户将搜索帖子,然后转到显示结果的结果。我需要做的是找出所有返回的帖子属于哪些类别,并尝试对每个帖子进行 post_count 以显示该特定类别中有多少帖子(所有返回的)。

提前致谢。

【问题讨论】:

    标签: wordpress search categories


    【解决方案1】:

    由于您没有提供任何代码,我尝试了我的方式。我想,这不是实现它的最佳方式 但到目前为止,您可以这样做。

    在这里,我使用PHP 来实现您想要的结果

    我用过二十四主题的search.php

    这是我的代码,用于二十四岁主题的search.php,您可以在您的search file 中实现:

        <?php
        /**
         * The template for displaying Search Results pages
         *
         * @package WordPress
         * @subpackage Twenty_Fourteen
         * @since Twenty Fourteen 1.0
         */
    
        get_header(); ?>
    
            <section id="primary" class="content-area">
                <div id="content" class="site-content" role="main">
    
                    <?php if ( have_posts() ) : ?>
    
                    <header class="page-header">
                        <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyfourteen' ), get_search_query() ); ?></h1>
                    </header><!-- .page-header -->
    
                        <?php 
    
                            // Start the Loop.
                            $post_id_array = array(); //Blank array to store all Post IDs
    
                            while ( have_posts() ) : the_post(); 
    
                                array_push($post_id_array, $post->ID); //store ID to array
    
                                /*
                                 * Include the post format-specific template for the content. If you want to
                                 * use this in a child theme, then include a file called called content-___.php
                                 * (where ___ is the post format) and that will be used instead.
                                 */
                                get_template_part( 'content', get_post_format() );
    
                            endwhile;
                            // Previous/next post navigation.
                            twentyfourteen_paging_nav();
    
                            $catarray = array(); //Blank array to store all categories
                            $k=1;
                            foreach($post_id_array as $my_post){
                                $my_category = get_the_category( $my_post );
    
                                foreach($my_category as $my_cat){
                                    array_push($catarray, $my_cat->cat_name); //Store category in array
                                }
    
                                $something = array_count_values($catarray); //Count total number of element in category array
    
                                foreach(array_unique($catarray) as $some){
                                    if($k == sizeof($my_category) ): //Condition to print all thing if only it is last element of an array.
                                        echo $some."(". $something[$some].")<br>";
                                    endif;
                                }
                                $k++;
                            }
                        else :
                            // If no content, include the "No posts found" template.
                            get_template_part( 'content', 'none' );
    
                        endif;
                    ?>
    
                </div><!-- #content -->
            </section><!-- #primary -->
    
        <?php
        get_sidebar( 'content' );
        get_sidebar();
        get_footer();
    

    在上面的代码中,我已经完成了:

    第 1 步:使用一个空白数组来存储搜索查询给出的帖子ID

    Step-2 : 使用 PHP 的 array_push 函数将所有 ID 推入创建的 空白数组注意:确保您已将其放入while loop 以获取所有ID

    两步之后,现在您有了一个所有帖子 ID 的数组

    Step-3:取另一个空白数组来存储帖子的相关类别名称

    Step-4 :取一个变量并将其值赋予1,这在最后很有用。(我已经采取$k = 1)。

    第 5 步:制作一个foreach 帖子 ID 循环。并获取带有wordpress功能的帖子的类别名称get_the_category.It will fetch category based on post ID

    第 6 步:现在是时候使用我们为类别名称创建的空白数组了。所以只需loop类别名称和 store 类别名称到 array 中。

    Step-7计数类别数组中的总名称,使用 PHP 的 array_count_values 函数并为其分配变量。(我使用了 $something)。

    step-7之后,我们有一个类别名称数组注意:在此数组中,类别名称可能存在重复。要删除重复,请执行下一步。

    Step-8:使用 PHP 的 'array_unique' 函数从数组中仅获取 唯一值

    第 9 步:创建一个类别名称loop

    如果你现在echoit will print categories to total number of post times

    所以现在是时候使用我们在foreach 循环帖子 ID($k=1) 之前获取的变量了。

    最后一步:现在一个条件最后一个帖子打印所有类别

    因此,我使用了if($k == sizeof($my_category) ),这意味着,它只会在类别数组的长度等于变量K时打印(简而言之,最后)。

    增加$k的值。

    希望对您有所帮助。如果您有任何疑问,请咨询我。

    【讨论】:

    • 您好 Rohil,非常感谢您提供如此详细的回答。是的,我没有输入任何代码,因为我实际上一无所知,所以在我的问题上加上一些东西比帮助更令人困惑。我更喜欢让事情从标准循环开始,以使事情变得更容易。我已经测试了代码,它还没有为我工作,我删除了所有我拥有的并粘贴了它,就像你发送它一样,我只是替换了对内容模板的调用,因为我没有它们,但什么也没发生。我得到的只是循环产生的帖子,仅此而已。不过我会继续努力的。
    • 好的,我现在遇到的问题是我在“洛杉矶”类别下获得了很多搜索结果,但我得到的只是“洛杉矶 (1)”
    • 嗨 Rohil,如果您不介意,我有两个疑问。为了将此代码放在循环之外(我的过滤器首先出现),我应该更改什么以及如何包含到类别的链接(我以前从未使用过 array_push() 或 array_unique())。
    • get_category_link 将帮助您获取类别的链接。我们在array 中有类别名称,您可以使用它们获取类别ID,并从ID 中获取链接。抱歉,我没有得到你的第一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多