如何使用循环在 WordPress 模板中显示图像?
我正在 WordPress 中创建页面,显示带有与页面标题相同的标签的所有帖子(例如,名为“dogs”的页面的所有“狗”帖子。目标是能够快速创建类别页面。
我的代码下面成功显示了帖子的标题和文本,但我还想显示每个帖子的图像(如果帖子有任何图像)...
如何使用 WordPress 显示每个帖子一张图像?
- 在循环中与文本一起实现上述内容?
<?php
/**
* Template Name: Category Page Template
* Description: A Page Template for Categories
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php $catname = wp_title('', false); ?>
<?php query_posts("category_name=$catname&showposts=3"); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
foreach ($posts as $post) : include(loop.php);?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I am creating pages in Wordpress that display all posts with a tag the same as the page title (all 'dog' posts for a page named 'dogs' for example. The goal is to be able to quickly create category pages.
The code I have bellow successfully shows the title and text of the post, but I want to also display an image for each post (if the post has any images). 2 questions...
-How to display one image per post with wordpress?
-How to implement the above alongside the text in a loop?
<?php
/**
* Template Name: Category Page Template
* Description: A Page Template for Categories
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php $catname = wp_title('', false); ?>
<?php query_posts("category_name=$catname&showposts=3"); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
foreach ($posts as $post) : include(loop.php);?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
到你的functions.php中,你可能还想同时设置图像大小,查看set_post_thumbail_size()和add_image_size()来做到这一点
,并在你的循环中添加类似的东西
add
to your functions.php, you might also want to set image sizes at the same time look into set_post_thumbail_size() and add_image_size() to do that
and in your loop add something like
1 获取帖子图片请参考this,调用:
2.将文本放入循环中,做类似的事情:
1 to get the image for post refer to this, call:
2.to put text in a loop, do something like: