如何使用循环在 WordPress 模板中显示图像?

发布于 2024-12-18 02:03:43 字数 921 浏览 4 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(2

夏末的微笑 2024-12-25 02:03:43

添加

add_theme_support('post_thumbnails');
add_image_size('homepage-thumbnail',300, 200, true);

到你的functions.php中,你可能还想同时设置图像大小,查看set_post_thumbail_size()和add_image_size()来做到这一点

,并在你的循环中添加类似的东西

if (has_post_thumbnail()){
  the_post_thumbnail('homepage-thumbnail');
}

add

add_theme_support('post_thumbnails');
add_image_size('homepage-thumbnail',300, 200, true);

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

if (has_post_thumbnail()){
  the_post_thumbnail('homepage-thumbnail');
}
一片旧的回忆 2024-12-25 02:03:43

1 获取帖子图片请参考this,调用:

wp_get_attachment_image($attachment_id, $size, $icon, $attr)

2.将文本放入循环中,做类似的事情:

<?php
query_posts("category_name=$catname&posts_per_page=3"); 
while ( have_posts() ) : the_post();
   the_title();
   the_excerpt();
endwhile;
wp_reset_query();
?>

1 to get the image for post refer to this, call:

wp_get_attachment_image($attachment_id, $size, $icon, $attr)

2.to put text in a loop, do something like:

<?php
query_posts("category_name=$catname&posts_per_page=3"); 
while ( have_posts() ) : the_post();
   the_title();
   the_excerpt();
endwhile;
wp_reset_query();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文