我有一段代码,它运行得很好,只是它没有按类别ID获取帖子。从我看到的示例来看,我希望这能起作用——我是否遗漏了什么?
<?php
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $args = array(
        \'category\' => 172,
        \'post_type\' => \'post\',
        \'posts_per_page\' => 10,
        \'paged\' => $paged
    );
    $wp_query = new WP_Query($args);
    while ( have_posts() ) : the_post();
?>
    <h3 class="publication-title"><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title() ?></a></h3>
    <p><strong><small><?php the_date(); ?></small></strong></p>
    <p><?php the_excerpt(); ?></p>
    <hr>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( \'← Older posts\', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( \'Newer posts →\' ); ?>
 
                    最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
                    当然不行了。哪些示例正在使用此代码?
让我们看看Category Parameters 属于WP_Query
共有5个类别参数:
cat(int)-使用类别id。category\\u名称(字符串)-使用类别slug
category\\u和(数组)-使用类别id。category\\u in(数组)-使用类别id。category\\u not\\u in(数组)-使用类别id。没有category 参数,因此您的参数未知WP_Query 忽略它。那么它应该是什么样子呢?
...
$args = array(
    \'cat\' => 172,
    \'post_type\' => \'post\',
    \'posts_per_page\' => 10,
    \'paged\' => $paged
);
...
 另外。。。你不应该使用
the_title() 在HTML属性中-它不会正确转义。因此,如果标题包含引号字符,那么它将破坏您的HTML代码。。。