1

我对这个有点难过。我想创建一个页面,该页面是某个类别的所有帖子的存档 - 并显示摘录。但是,通过阅读 Wordpress 文档,他们说页面不能是帖子或与类别相关联。

所以,我现在想知道这是否可能,或者是否有解决方法。我真的很想把它放在一个页面上,因为我也想有一个自定义侧边栏。这可以通过自定义页面模板实现吗?我没有想到的任何其他方法可以做到这一点?

谢谢!

4

2 回答 2

1

您可以将此函数添加到functions.php,然后您可以从任何页面模板调用它。

如果您需要为侧边栏创建新的页面模板。

FTP,下载 page.php 将 page.php 重命名为 page-custom.php (custom 可以是任何东西,只要确保它的 page-whatever.php 在 page-custom.php 中将评论部分替换为(自定义模板名称可以是任何东西你要)

/**
 * 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

于 2013-10-04T02:25:11.870 回答
1

创建一个页面,例如lipsum。
为本示例创建一个带有 page-[标题].php、page-lipsum.php 的 PHP 文件。在那个文件中
写下你的循环。
将该文件 FTP 到您的主题目录。
当您转到该页面的 URL 时,结果应该是您正在寻找的结果。

于 2013-10-04T01:54:30.293 回答