【问题标题】:wordpress Query posts from specific category with post thumbnail and excerpt [duplicate]wordpress 使用帖子缩略图和摘录查询特定类别的帖子[重复]
【发布时间】:2012-08-22 07:20:39
【问题描述】:

可能重复:
Wordpress array to show post from certain category and show post excerpt and feature ing

大家好,正在编写一个 WP 主题。我在主页上有一个滑块来显示“新闻”类别中的最新帖子。我已经研究了如何在来自猫的滑块中显示帖子,但我不知道如何包含帖子特征图像,除了. 有人可以请教。

这是我正在进行的 WP 网站。这是“最新消息”滑块。

http://www.garyrevell.co.uk/student-i-wp/

谢谢

【问题讨论】:

    标签: 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&amp;posts_per_page=1')

    【讨论】:

    • 我现在有这个,我知道它不起作用,但你能看到我在做什么吗? ` 'news', 'posts_per_page' => -1 ) ); // 循环 while ( have_posts() ) : the_post();回声'
    • ';标题();回声'
    • ';结束; // 重置查询 wp_reset_query(); ?>`
  • 我会扩展答案...希望对您有所帮助。
  • 谢谢.. php 会导致错误。啊男人
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签