ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-在两页上显示WordPress帖子

2019-12-09 22:41:55  阅读:373  来源: 互联网

标签:loops posts wordpress php


我的网站上有三页.我们称它们为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.

标签:loops,posts,wordpress,php
来源: https://codeday.me/bug/20191209/2097474.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有