在两页上显示Wordpress帖子
我在我的网站上有三个页面。我们称他们为home,page2和page3。 我的'主页'页面设置为静态首页。我的'page2'被设置为博客页面。在两页上显示Wordpress帖子
我想是这样的:
我想第2页显示与特定类别(其中ID是已知的)的博客文章。
AND
我想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 »'); ?>
</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 »'); ?>
</div><!-- /.post-->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php
} //end if is_page
?>
但它不显示(在这个问题上还是第3页)本报页面上的适当的职位。但是,它的确适用于文章页面(main 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 »'); ?>
</div><!-- /.post-->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php
} //end if is_home() or is_page()
?>
这再次显示了主要的博客页面上的帖子,但不显示报纸页面上的任何职位......
的问题是因此很简单(我认为)。如何在除主博客页面以外的其他页面上显示帖子?
谢谢! Amit
东西,而不是排除类别和排除网页和更改标准的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执行以在页面/帖子编辑器中使用它)。 Function Reference/WP Query « WordPress Codex
这也能很好地使用页面模板来创建博客文章不同的网页:Page Templates « WordPress Codex,但不要忘了,WP也使用类别页面也一样,这取决于你的主题:Category Templates « WordPress Codex.
在is_page('newspaper')中使用字符串'newspaper'是问题的潜在来源。它可能很容易拼错。 你有没有尝试过使用页面ID?像
is_page('999')
这不是一个问题拼写错误。我也尝试过page_id以及 – Amit 2010-08-02 15:42:38
让我试试你的方式。我必须说,我已经阅读过Wordpress论坛,使用两个不同的页面来显示两种不同类型的帖子是不可能的。相反,您可以使用类别,其中显示的页面实际上是类别的存档。但是,如果您的解决方案有效,那么您为我节省了大量工作。 – Amit 2010-08-02 15:43:34
不幸的是,使用类别不适合我,因为需要在其中包含帖子的两个页面是父链接的下拉页面(并且我使用的是动态导航,而不是硬编码)......没有办法给一个类别一个父页面(只有一个父类别)。 – Amit 2010-08-02 15:45:02
这工作。你是一个天才。 – Amit 2010-08-02 15:48:31