繁体   English   中英

在WP查询中按类别名称检索帖子

[英]Retrieve posts by category name issue in WP Query

因此,我为“主题”页面创建了一个自定义页面模板。

我想要做的是将一些PHP添加到“主题”页面使用的自定义页面模板中,以从所选类别中检索3个最新帖子的永久链接。

E.g.
(From post category 1)

--> Permalink for post 1

--> Permalink for post 2

--> Permalink for post 3

到目前为止,我的代码如下:

<?php if (have_posts()) : ?>
           <?php while (have_posts()) : the_post(); ?>    
           <ul>
    <?php
    $category_posts = new WP_Query('cat=consumer-trust&showposts=3');
    while ($category_posts ->have_posts()) : $category_posts->the_post();?>
    <li>
    <a class="yourclass" href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></li>
    <?php endwhile; ?>

但是,问题在于,在WP_Query中更改cat似乎没有任何区别。 我尝试过使用数字和类别名称,但均无效。

有人可以建议吗? 对于三个不同的类别,此代码将在预期的页面上出现3次。

采用

query_posts( array ( 'category_name' => 'cat_name','meta_key'=>'_pr_date', 'orderby' => 'meta_value','order'=>'DESC') );

感谢所有帮助,我设法找到了答案:

<?php global $post; // required
$args = array('category' => 18); // include category 18
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);

// put here what you want to appear for each post like:
//the title:
the_title();   

endforeach;

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM