方法 #1
当您需要显示内容、摘录、自定义字段或任何与文章相关的超出其链接和标题的内容时,此函数很有用。如果您只需要链接标题列表,请参见下一种方法。将以下函数放到 functions.php 中。
function recent_posts($no_posts = 10, $excerpts = true) {
global $wpdb;
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
$posts = $wpdb->get_results($request);
if($posts) {
foreach ($posts as $posts) {
$post_title = stripslashes($posts->post_title);
$permalink = get_permalink($posts->ID);
$output .= '<li><h2><a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a></h2>';
if($excerpts) {
$output.= '<br />' . stripslashes($posts->post_excerpt);
}
$output .= '</li>';
}
} else {
$output .= '<li>No posts found</li>';
}
echo $output;
}
用法
创建好函数后。将以下内容放到侧边栏或您想要显示近期文章的任何位置。
<?php recent_posts(); ?>
您可以提供两个参数,第一个是文章数量,第二个是您是否想要显示摘录。因此 recent_posts(2, false) 将显示最近的两个文章标题。
方法 #2
<?php wp_get_archives( array(
'type' => 'postbypost', // or daily, weekly, monthly, yearly
'limit' => 10, // maximum number shown
'format' => 'html', // or select (dropdown), link, or custom (then need to also pass before and after params for custom tags
'show_post_count' => false, // show number of posts per link
'echo' => 1 // display results or return array
) ); ?>
方法 #3
方法 #1 的更简洁版本,也包含更标准化的查询字符串。
<?php
$recentposts = get_posts('numberposts=12&category=4');
foreach ($recentposts as $post) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
当我将 WordPress 升级到另一个新版本时...
functions.php 会被更新覆盖吗?需要重新添加上面的代码吗?
请给出建议...
mans! weh dah dah la tu dow ng website ko.haha..
@chris,感谢这位仁兄
快速问题 - 如何将日期添加到函数中?
谢谢
@mansur,不会的,functions.php 文件是您主题的一部分,在 WordPress 更新期间不会更新,只有在更新主题时才会更新。
functions.php 文件只有在使用默认 WordPress 主题时才会在 WordPress 更新期间更新,即使您可能已经对其进行了自定义。
如果您当前正在使用默认主题,解决方案是从 style.css 文件中重命名主题,您还应该重命名主题文件夹(非常重要!)。
我是一个新手...我想知道您如何确定何时实施上述帖子...因为它听起来很酷...但是您如何知道“何时”
好吧,我有一个疯狂的问题。我的一个客户想要在他们的主页上显示最新的 3 篇文章。我该如何将每篇文章分解成一个单独的 div 以进行样式设置?
格式看起来像下面这样
显示最近的帖子 / 显示第二个最近的帖子 / 显示第三个最近的帖子
上述每个都在单独的 DIV 中,希望这在某种程度上有所帮助...
提前感谢!
Aaron
它在我的博客上不起作用 :(
打开您的 Cpanel 或 FileZilla。
找到 functions.php 并点击编辑。插入上面提供的代码
在页面底部,点击保存。
现在回到您想要显示近期文章的位置
只需输入
希望有帮助。
@interviewdate 发布您实现的代码示例,也许我可以提供帮助(如果您还没有解决它)。
请帮帮我...我想在我的 index.php 中显示最近的帖子...而不是在 WordPress 中
这是我用来输入文章的代码
但我不知道如何只获取最近的帖子,例如最近的 8 篇帖子.....
php
$re= “select * from tableaux
LIMIT 0 , 7″;
$k = mysql_query($re);
while($s=mysql_fetch_row($k))
{
$t = substr($s[2] , 0 , 300 );
$t=$t.” …”;
echo ‘
a href=”‘.$s[3].'”title=”‘.$s[1].'” ></a
}
效果很好。但我如何显示“文章日期”???
我应该把代码
<?php recent_posts(); ?>
放在哪里?非常感谢,这些教程和循环很棒
要包含日期,请执行以下操作
在函数中替换此
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
使用此
$request = "SELECT ID, post_title, post_excerpt, post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
并将其添加到您的输出中,类似于这样
$output .= '<a href="' . $permalink . '" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '" rel="nofollow">' . htmlspecialchars($post_title) . '(' . htmlspecialchars($post_date) . ')' . '</a>';
或者,要将日期格式化为 2014 年 4 月 24 日的格式,请将查询中的
post_date
更改为DATE_FORMAT( post_date, '%D %M %Y' ) AS post_date
您如何控制字体大小?
非常感谢您,它真的非常有帮助
除了帖子和页面,我还有一个不同的页面,比如 - https://example.com/lyrics
我想从这个页面上显示最近的歌词帖子。你能告诉我,我应该怎么做?