【问题标题】:Wordpress: url to all posts for home.phpWordpress:home.php 的所有帖子的网址
【发布时间】:2018-03-05 22:08:51
【问题描述】:

我正在为 WordPress 开发一个主题,我为原始循环创建了 index.php,并为 4 创建了 home.php最新帖子,现在我需要放一个链接以显示所有帖子,我该怎么做?

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    我会在 while 循环中使用 WordPress 的 have_posts() 函数而不是显示所有帖子的链接(首先检查是否有要显示的帖子,如果有,则退出循环并显示其他内容)。您可以查看 WP 的函数文档,甚至可以查看用于默认页面模板 (page.php) 的文件。

    为了只显示最新的 4 篇文章,您可以简单地添加一个计数器变量,并使用 if 语句检查是否 count == 4 来结束每个 while 循环迭代。

    另外,请注意,home.php 有时优先于 index.php,因此要么将 home.php 嵌套在内部目录中,要么将您的“4 个最新帖子”页面称为“posts.php”或类似名称。

    Wordpress 文档:https://codex.wordpress.org/Function_Reference/have_posts

    【讨论】:

    • 这不是我的答案
    【解决方案2】:
    To get latest post with url:
    <?php
            $args = array( 'numberposts' => '1', 'category' => CAT_ID );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){
            echo '<a href="' . get_permalink($recent["ID"]) . '">Latest Post</a>';`enter code here`
            }
        ?>
    Hope this helps .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 2013-07-16
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多