繁体   English   中英

在两个页面上显示Wordpress帖子

[英]Showing Wordpress posts on two pages

我的网站上有三页。 我们将它们称为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()
?>

再次,这显示了主要博客页面上的帖子,但没有在报纸页面上显示任何帖子......

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

谢谢! 阿米特

而不是排除类别和排除页面并更改标准的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。

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

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

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

is_page('999')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM