【问题标题】:Display recent posts based on their category in Wordpress根据 Wordpress 中的类别显示最近的帖子
【发布时间】:2012-11-15 07:03:14
【问题描述】:

我使用一个在 content.php 页面上生成缩略图的模板,如下所示:

<article <?php post_class('single-entry clearfix'); ?>>  
<?php
// Test if post has a featured image
if( has_post_thumbnail() ) { 
    // Get resize and show featured image : refer to functions/img_defaults.php for default values
    $wpex_entry_img = aq_resize( wp_get_attachment_url( get_post_thumbnail_id(), 'full' ),  wpex_img( 'blog_entry_width' ), wpex_img( 'blog_entry_height' ), wpex_img( 'blog_entry_crop' ) );
?>
    <div class="single-entry-thumbnail">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $wpex_entry_img; ?>" alt="<?php echo the_title(); ?>" /></a>
    </div><!-- /single-entry-thumbnail -->
<?php } ?>
<div class="entry-text clearfix">
    <header>
        <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    </header>
</div><!-- /entry-text -->

我刚开始使用 Wordpress 和 php,我想为此添加一个参数,因此只有类别为“展示”的帖子才会显示。任何人的想法?

【问题讨论】:

  • 您需要在帖子查询中添加一个参数,该参数发生在您共享的代码之上或之前。你能找到任何相关的query_postsWP_Queryget_posts 粘贴到这里吗?
  • 感谢@crowjonah 调查它。这就是我遇到的问题。此代码似乎与“典型”WP 循环不同。此 .php 文件或其他 .php 文件中没有此类参数。

标签: php wordpress filter categories posts


【解决方案1】:

您可以使用 in_category() 函数来测试该帖子属于它。

if ( in_category( 'showcase' ) ) {
    ...
}

in_category() 文档 - http://codex.wordpress.org/Function_Reference/in_category

【讨论】:

  • 是的,这段代码必须在 The Loop 内并围绕
    标签。
  • 将这行代码放在
    标记周围不起作用。我最终在文章标签inside 中添加了代码,它就像一个魅力!非常感谢@Vladimir!这也适用于 tags 吗?只显示带有特定标签的文章?
  • 是的,这段代码可以使用标签。但是你应该使用 has_term (codex.wordpress.org/Function_Reference/has_term) 函数而不是 in_category。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多