【问题标题】:Where is wordpress search page content generated?wordpress 搜索页面内容在哪里生成?
【发布时间】:2012-04-10 00:35:21
【问题描述】:

我正在尝试在 wordpress 中设置搜索页面的样式。在 search.php 中,我可以设置大部分页面的样式,但是下面的语句(我从原始未编辑页面中获得)生成内容。

                <?php
                    /* Include the Post-Format-specific template for the content.
                     * If you want to overload this in a child theme then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */
                    get_template_part( 'content', get_post_format() );
                ?>

            <?php endwhile; ?>

这几乎可以按我的意愿显示页面,但是页面外有一些元素,使其扩展等。我无法弄清楚生成此内容的文件是什么!

使用说明我创建了一个 content-search.php 并将代码行更改为此...

get_template_part( 'content', get_post_format() );

哪个有效...但它没有显示任何内容,因为我不知道在看到原始页面时该在我的页面中放置什么。

有人知道吗?

【问题讨论】:

    标签: wordpress search


    【解决方案1】:

    您可以使用名为 post-search.php 的模板部分,并可以在您的 search.php 文件中使用它,例如

    get_template_part( 'post' , 'search')
    

    但您必须在主题文件夹中创建一个 php 文件并将其命名为 post-search.php,然后在该文件中放入 WordPress 循环,即

    <?php while (have_posts()) : the_post(); ?>
    <div class="post-entry clearfix"> <!-- Main wrapper -->
        <div class="post-entry-content"> <!-- Post-entry-content -->
                <h2><a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
                    <div class="post-entry-date">Posted on <?php the_time('F Y') ?> with <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></div>
                        <?php the_excerpt(); ?>
                        <a href="<?php the_permalink(' ') ?>" class="post-entry-read-more" title="<?php the_title(); ?>">Read More ?</a>
        </div><!-- END of post-entry-content -->
    </div><!--End of main wrapper -->
    <?php endwhile; ?>
    

    你的 search.php 可能是这样的

    <?php get_header(' '); ?>
    <div id="post-wrap">  
        <div id="post-content"> 
        <?php if (have_posts()) : ?>
        <?php get_template_part( 'post' , 'search') ?> // This will load/include the file post-search.php and result will be displayed as formatted in this file
        <?php else : ?>
            <p>Sorry, it does not exist !</p>
        <?php endif; ?>
        </div><!-- END post-conten -->
    <?php get_sidebar(' '); ?>      
        </div><!-- END post-wrap -->        
    <?php get_footer(' '); ?>
    

    这只是一个例子,根据你的主题 css 更改 div/h2 id/class 名称。

    注意:我目前在我的一个站点中使用这种方法,并且在我的主题文件夹和每个模板文件(索引.php, search.php 等)我只是通过调用来使用这个文件

    <?php get_template_part( 'post' , 'entry') ?>
    

    【讨论】:

    • 宾果游戏!谢谢,这解决了它。我所做的唯一不同的事情是使用 content-search.php,但它做同样的事情 :) 现在我知道所有内容的来源,我可以让它看起来很漂亮:P
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多