【问题标题】:wordpress Query posts from specific category with post thumbnail and excerpt [duplicate]wordpress 使用帖子缩略图和摘录查询特定类别的帖子[重复]
【发布时间】:2012-08-22 07:20:39
【问题描述】:
【问题讨论】:
标签:
php
html
wordpress
loops
wordpress-theming
【解决方案1】:
您可以通过将这段代码包含在您希望图像出现的位置的循环中来添加特色图像:
<?php if ( has_post_thumbnail()) : ?><?php the_post_thumbnail(); ?><?php endif; ?>
您可以通过将 the_content() 函数替换为 the_exerpt() 来添加摘录。
假设您的循环中有这段代码:
<?php the_content(); ?>
你将用这个替换它:
<?php the_exerpt(); ?>
除了添加和替换之外,还有很多其他内容,但我认为这可以帮助您入门。
希望这会有所帮助 =]。
您应该将所有内容都放入循环中。
这是一个完整的循环,其中包含标题、缩略图和要在您的部分中使用的内容......
<?php $my_query = new WP_Query('category_name=news&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<h2 id="yourTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div id="yourImage">
<?php if ( has_post_thumbnail()) : ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<p id="yourContent"><?php the_exerpt(); ?></p>
<?php endwhile;?>
在这里,我们告诉查询显示您的新闻类别帖子并仅显示 1 个帖子 ('category_name=news&posts_per_page=1')
【讨论】:
-
我现在有这个,我知道它不起作用,但你能看到我在做什么吗? ` 'news', 'posts_per_page' => -1 ) ); // 循环 while ( have_posts() ) : the_post();回声'- ';标题();回声'
';结束; // 重置查询 wp_reset_query(); ?>`
-
-