3

我的网站上有三个页面。我们称它们为 home、page2 和 page3。我的“主页”设置为静态首页。我的“page2”设置为博客页面。

我想要的是以下内容:

我希望 page2 显示具有特定类别(其中 ID 已知)的博客文章。

我希望 page3 显示具有特定类别(其 ID 已知)的博客文章。

仅显示具有特定类别的帖子(或者实际上在我的情况下,显示不包括两个类别的帖子)的 PHP 代码如下:

<?php query_posts($query_string . '&cat=-3,-8'); ?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
        <h3><a href="<?php the_permalink() ?>" rel="bookmark"
            title="Permanent Link to <?php the_title_attribute(); ?>">
            <?php the_title(); ?></a></h3>
        <?php the_excerpt('Read the rest of this entry &raquo;'); ?>
        </div><!-- /.post-->

现在,在我的 page.php 中,我有以下代码来显示一个类别的帖子:

<?php
    // BEGIN IF PAGE is newspaper articles page
    if ( is_page('newspaper') ) {

        //BEGIN POST REGION

        query_posts($query_string . '&cat=8'); ?>
        <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="post" id="post-<?php the_ID(); ?>">
                <h3><?php the_title(); ?></h3>

                <?php the_content('Read more &raquo;'); ?>


                </div><!-- /.post-->

            <?php endwhile; ?>

        <?php else : ?>

        <?php endif; ?>

        <?php

    } //end if is_page
?>

但它没有在报纸页面(或这个问题中的第 3 页)上显示正确的帖子。但是,它确实适用于文章页面(主 index.php 博客页面)。

编辑:我也尝试了以下方法(但它不起作用)。我把它放在 index.php 文件中:

<?php
if ( is_page('newspaper') || is_home() ) { // START if is home

?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <h3><a href="<?php the_permalink() ?>" rel="bookmark"
                title="Permanent Link to
                <?php the_title_attribute(); ?>">
                <?php the_title(); ?></a></h3>

            <!--<p><?php the_time('F jS, Y') ?> <?php //the_author() ?></p>-->

            <?php the_excerpt('Read the rest of this entry &raquo;'); ?>


        </div><!-- /.post-->

    <?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

<?php
} //end if is_home() or is_page()
?>

同样,这会在主博客页面上显示帖子,但不会在报纸页面上显示任何帖子......

因此,问题很简单(我认为)。我如何在博客主页面以外的其他页面上显示帖子?

谢谢!阿米特

4

4 回答 4

3

与其排除类别和排除页面并更改标准的 Wordpress 循环,不如使用新查询,如下所示:

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a></h3>
<?php the_excerpt('Read the rest of this entry &raquo;'); ?>
<?php endwhile; ?>

这可以在标准 WP 循环中使用,并且可以在页面/帖子或页面模板中多次使用而不会发生冲突。(启用 php 执行以在页面/帖子编辑器中使用它)。函数参考/WP 查询 « WordPress Codex

这也适用于使用页面模板创建带有博客文章的不同页面:页面模板 « WordPress Codex,但不要忘记 WP 也使用类别页面,具体取决于您的主题:类别模板 « WordPress Codex。

于 2010-08-02T13:26:12.657 回答
0

我认为您必须为不同的页面制作不同的模板。检查此链接http://codex.wordpress.org/Pages

于 2010-08-02T08:48:06.460 回答
0

我认为这个线程回答了这个问题并做你想做的事。 http://wordpress.org/support/topic/show-only-x-category-posts-on-page?replies=9#post-1053767

于 2010-08-02T09:01:20.973 回答
0

在 is_page('newspaper') 中使用字符串 'newspaper' 是问题的潜在根源。它可能很容易拼写错误。您是否尝试过使用页面 ID?就像是

is_page('999')
于 2010-08-02T09:04:39.530 回答