在两个页面上显示 WordPress 帖子

发布于 2024-09-12 14:15:56 字数 2690 浏览 2 评论 0原文

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

同样,这显示了博客主页上的帖子,但没有显示报纸页面上的任何帖子......

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

谢谢! 阿米特

I have three pages on my site. Let's call them home, page2, and page3.
My 'home' page is set as a static front page. My 'page2' is set up as the blog page.

What I want is the following:

I want page2 to display blog posts with a certain category (of which ID is known).

AND

I want page3 to display blog posts with a certain category (of which ID is known).

The PHP code to only show posts with a certain category (or actually in my case, show posts excluding two categories) is the following:

<?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 »'); ?>
        </div><!-- /.post-->

Now, in my page.php, I have the following code to display posts with one category:

<?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 »'); ?>


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

            <?php endwhile; ?>

        <?php else : ?>

        <?php endif; ?>

        <?php

    } //end if is_page
?>

But it doesn't show the proper posts on the newspaper page (or page3 in this question). It does, however, work for the articles page (main index.php blog page).

EDIT: I've also tried the following (but it doesn't work). I put this in the index.php file:

<?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 »'); ?>


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

    <?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

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

Again, this shows the posts on the main blog page, but doesn't show any posts on the newspaper page...

The question is therefore simple (I think). How do I show posts on another page OTHER than the main blog page?

Thanks!
Amit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇见了你 2024-09-19 14:15:56

不要排除类别和排除页面并更改标准 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 »'); ?>
<?php endwhile; ?>

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

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

Rather than exclude categories and exclude pages and change the standard Wordpress loop, use a new query, like this:

<?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 »'); ?>
<?php endwhile; ?>

This can be used inside the standard WP loop and be used mutiple times in a page/post or page template without conflicting. (Enable php execution to use it in the page/post editor). Function Reference/WP Query « WordPress Codex

This also works well to use page templates to create different pages with blog posts: Page Templates « WordPress Codex, but don't forget that WP also uses category pages, too, depending on your theme: Category Templates « WordPress Codex.

打小就很酷 2024-09-19 14:15:56

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

I think you have to make different templates for different page. Check this link http://codex.wordpress.org/Pages

晌融 2024-09-19 14:15:56

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

I think this thread answers the question and does what you want.
http://wordpress.org/support/topic/show-only-x-category-posts-on-page?replies=9#post-1053767

吹梦到西洲 2024-09-19 14:15:56

在 is_page('newspaper') 中使用字符串 'newspaper' 是问题的潜在根源。可能很容易拼写错误。
您尝试过使用页面 id 吗?像这样的东西

is_page('999')

Using the string 'newspaper' in is_page('newspaper') is a potential source of the problem. It might be misspelled easily.
Have you ever tried using the page id? Something like

is_page('999')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文