gpt4 book ai didi

php - 在两个页面上显示 Wordpress 帖子

转载 作者:搜寻专家 更新时间:2023-10-31 20:55:16 25 4
gpt4 key购买 nike

我的网站上有三个页面。我们称它们为 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 执行以在页面/帖子编辑器中使用它)。 Function Reference/WP Query « WordPress Codex

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

关于php - 在两个页面上显示 Wordpress 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3386154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com