WP_RESET_POSTData不适用于类别页面

时间:2016-08-10 作者:Manolo

我有一个类别模板,它显示一些帖子和插件模板生成的一些内容,插件模板使用the_permalink() 以引用当前url。类别模板如下所示(category.php):

<?php $categoryQuery = get_the_category(); ?>
<?php $parentCategory = get_term_by(\'id\', $categoryQuery[0]->parent, \'category\') ?>
<?php if ($parentCategory->slug !== \'teams\' && $categoryQuery[0]->slug !== \'teams\') {
    get_template_part( \'archive\', get_post_format() );
} else {

    get_header(); ?>

    <div class="container main-outer">

        <?php set_query_var( \'categorySlug\', $categoryQuery[0]->slug ); ?>
        <?php set_query_var( \'categoryName\', $categoryQuery[0]->name ); ?>
        <?php get_template_part( \'teams-header\', get_post_format() ); ?>
<?php } ?>

...

// Here goes the plugin template

...
以及teams-header.php 文件如下所示:

...

<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$featpost = new WP_Query(array(
    \'category_name\' => $categorySlug,
    \'showposts\' => 5,
    \'paged\' => $paged,
));
$newnum = 1;
$maxNumPages = $featpost->max_num_pages;
while($featpost->have_posts()) : $featpost->the_post();
    ...
    $newnum++;
endwhile; ?>
<?php wp_reset_postdata() ?>
<div class="pagination-links">
    <br />
    <?php next_posts_link(\'&laquo; Older entries\', $maxNumPages) ?>
    &nbsp;&nbsp;&nbsp;
    <?php previous_posts_link(\'Recent entries &raquo;\') ?>
</div>
问题是插件模板将第一个显示的帖子url显示为当前url(带有the_permalink()), 而不是第一类。wp_reset_postdata() 应该重置当前的帖子数据,但可能我遗漏了什么。有什么想法吗?

2 个回复
最合适的回答,由SO网友:Manolo 整理而成

我已经解决了$postnull posts渲染后:

<?php $categoryQuery = get_the_category(); ?>
<?php $parentCategory = get_term_by(\'id\', $categoryQuery[0]->parent, \'category\') ?>
<?php if ($parentCategory->slug !== \'teams\' && $categoryQuery[0]->slug !== \'teams\') {
    get_template_part( \'archive\', get_post_format() );
} else {

    get_header(); ?>

    <div class="container main-outer">

        <?php set_query_var( \'categorySlug\', $categoryQuery[0]->slug ); ?>
        <?php set_query_var( \'categoryName\', $categoryQuery[0]->name ); ?>
        <?php get_template_part( \'teams-header\', get_post_format() ); ?>

        <?php $post = null; // This solves the problem and the_permalink() is the category page ?>

<?php } ?>

...

// Here goes the plugin template

...

SO网友:ma_dev_15

您可以使用get\\u category\\u链接($id)获取类别id和获取永久链接;

相关推荐