【发布时间】:2013-10-04 00:38:22
【问题描述】:
我对这个有点难过。我想创建一个页面,该页面是某个类别的所有帖子的存档 - 并显示摘录。但是,通过阅读 Wordpress 文档,他们说页面不能是帖子或与类别相关联。
所以,我现在想知道这是否可能,或者是否有解决方法。我真的很想把它放在一个页面上,因为我也想有一个自定义侧边栏。这可以通过自定义页面模板实现吗?还有其他我没有想到的方法吗?
谢谢!
【问题讨论】:
标签: wordpress categories archive
我对这个有点难过。我想创建一个页面,该页面是某个类别的所有帖子的存档 - 并显示摘录。但是,通过阅读 Wordpress 文档,他们说页面不能是帖子或与类别相关联。
所以,我现在想知道这是否可能,或者是否有解决方法。我真的很想把它放在一个页面上,因为我也想有一个自定义侧边栏。这可以通过自定义页面模板实现吗?还有其他我没有想到的方法吗?
谢谢!
【问题讨论】:
标签: wordpress categories archive
您可以将此函数添加到functions.php,然后您可以从任何页面模板调用它。
如果您需要为侧边栏创建新的页面模板。
FTP,下载page.php 将 page.php 重命名为 page-custom.php(自定义可以是任何东西,只要确保它的 page-whatever.php 在页面-custom.php 将 cmets 部分替换为(自定义模板名称可以是任何你想要的)
/**
* Template Name: Custom Template Name
*
*/
将新模板中的循环替换为调用get_posts_custom('catSlug');
然后将其添加到您的functions.php
if(!function_exists('get_posts_custom')){
function get_posts_custom($catSlug){
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $catSlug,
'ignore_sticky_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article class="post">
<time class="date"><?php the_time('m.d.y') ?></time>
<p><strong><?php the_title(); ?></strong>
<?php the_excerpt(); ?>
</p>
</article>
<?php
endwhile;
}
wp_reset_query();
}
}
参考http://codex.wordpress.org/Class_Reference/WP_Query
我没有完全测试这个功能,但我使用的是相同代码的修改版本。我的没有过滤类别 slug,所以可能需要调整,但我做了测试以确保它没有破坏 functions.php
【讨论】:
创建一个页面,例如lipsum。
在本示例中,使用 page-[标题].php、page-lipsum.php 创建一个 PHP 文件。
在该文件中写入your loop。
将该文件 FTP 到您的主题目录。
当您转到该页面的 URL 时,结果应该就是您要查找的结果。
【讨论】: