我正在尝试在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; ?>
这个ALMOST按我的意愿显示页面,但是页面上有一些元素,使它扩展等等。我无法弄清楚生成这个内容的文件是什么!
使用说明我创建了一个content-search.php并将代码行更改为此...
get_template_part( 'content', get_post_format() );
哪个有效...但它没有显示任何内容,因为我不知道在看到原文的内容中放入什么内容。
任何人都有任何线索?
答案 0 :(得分:6)
您可以使用名为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 names。
注意:我目前正在我的某个网站中使用此方法,并且在我的主题文件夹和我的每个模板文件中都有一个名为“post-entry.php”的文件(索引) .php,search.php等)我只是通过调用
来使用这个文件<?php get_template_part( 'post' , 'entry') ?>