1

I have a category template which displays some posts and some content generated by a plugin template, which uses the_permalink() to refer to the current url. The category template looks like this (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

...

And the teams-header.php file looks like this:

...

<?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>

The problem is that the plugin template is showing the first displayed post url as the current url (with the_permalink()), and not the category one. wp_reset_postdata() should reset the current post data, but maybe I'm missing something. Any idea?

0

You can get the category id and the fetch the permalink by using get_category_link($Id);

  • The plugin template should not be modified, and it's what is using the_permalink(). – Manolo Aug 10 '16 at 17:11
  • You can use condition in function.php and modify the_permalink() with get_category_link() if current page is category page – ma_dev_15 Aug 10 '16 at 17:15
  • This may works but also seems a bit weird. I think that it would be better to reset the post data. – Manolo Aug 10 '16 at 18:23
  • you need to share me more details so I can look into further. – ma_dev_15 Aug 11 '16 at 6:48
  • Well, here are all relevant details. The one left is just the plugin template call which uses the_pemalink(): leaguemanager_matches(5) from LeagueManager plugin. – Manolo Aug 11 '16 at 7:32
0

I've solved it just setting $post to null after the posts render:

<?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

...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.