如何在 WordPress 3.0 主题中显示内容数据库中的随机帖子?

发布于 2024-11-10 10:59:23 字数 511 浏览 0 评论 0原文

我正在 http://www.knowledgenation.us 开发一个网页,目前该页面上有大约 500 个帖子。我相信帖子太多了,无法指望有人读完,但我确实相信我的页面具有返回价值。我希望人们定期返回该网页,并始终从该网站获得新的东西。

话虽这么说,我想将我的数据库中的三个随机帖子发布到我的主题网页的正文中。我还想知道如何使该代码模块化,以便我可以将其重用于该网站的新版本,该网站将从我的朋友正在开发的两个网站的 RSS 提要中提取内容。

总而言之,最重要的是,如何将随机帖子发布到网站上,代码是什么样的,请善意解释,因为我对 PHP 编程很陌生,并且不理解代码的大部分内容。我最近刚刚获得了一个 http://www.lynda.com 帐户,我将学习有关 PHP 的所有知识,但是目前我了解甚少。

我预先感谢您在这方面对我的帮助。

I am developing a webpage at http://www.knowledgenation.us and currently I have roughly 500 posts on the page. I believe that is far too many posts to expect someone to read through but I do believe that my page has return value. I want people to return to the webpage on a regular basis and always get something new from the site.

That being said, I would like to post three random posts from my database to the body of the web page that is the theme that I have. I would also like to know how to make that code modular so that I can reuse it for a new incarnation of this website that is going to pull in content from RSS feeds from two websites that friends of mine are developing.

That all being said, bottom line, how do you post random posts to a website, what would the code look like and please be kind in explaining because I am quite new to PHP programming and would not understand most of what the code is about. I just recently got a http://www.lynda.com account and am going to be learning all about PHP but for right now I understand little.

I thank you in advance for helping me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-11-17 10:59:23

当您查询帖子时,您可以传递查询属性,例如类别、包含/排除的帖子 ID、限制和偏移量等。您还可以定义结果的排序方式 - 按哪个字段和哪个方向排序(上升/下降)。

order_by 参数可以是常规字段名称,例如 titledate,也可以是 randrandom 中那样获取随机帖子。

这是一个在循环外部使用的 示例,用于获取五个随机帖子:

<ul>
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

还有一个示例常规循环:

<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
query_posts($args);

while (have_posts()) : the_post();
  the_content('Read the full post »');
  // And so forth…
endwhile;
?>

希望你能看到照片……

When you query your posts, you can pass on query attributes such as a category, included/excluded post ids, limits and offsets, etc.. You can also define how your results will be ordered — by which field and into which direction (ASC/DESC).

The order_by parameter can be a regular field names like title or date, and also rand as in random to fetch random posts.

Here's an example to use outside the loop, fetching five random posts:

<ul>
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

And another example for a regular loop:

<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
query_posts($args);

while (have_posts()) : the_post();
  the_content('Read the full post »');
  // And so forth…
endwhile;
?>

Hope you get the picture…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文