繁体   English   中英

Wordpress:指向 home.php 的所有帖子的网址

[英]Wordpress: url to all posts for home.php

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

我将在 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

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 .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM