Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok i have a custom post type on my blog called videos, where i only post a video. There's a sceen cap below

enter image description here

On the right are the latest post from the custom post type, on the left is the video, and under the video is the date and time, category and tags. But the problem is that it is getting the wrong, Tags, Categories, and date. How do i fix this?

here is the code of that template page below

<?php
/*
Template Name: Single Videos
*/
?>
<?php get_header() ?>

<div id="wrapper">

<div id="container">

<div id="contentfull">



    <?php the_post() ?>

        <div class="entry-wide">
            <center><h2 class="page-title2"><?php the_title() ?></h2>     </center>

            <div class="entry-videoo">
 <?php the_content() ?>



 <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'wpbx' ) . '&after=</div>') ?>


            </div>

 <div id="videosidebar">

 <?php
 $queryObject = new WP_Query( 'post_type=videos&posts_per_page=20&orderby=rand' );
 // The Loop!
 if ($queryObject->have_posts()) {
?>

<?php
while ($queryObject->have_posts()) {
    $queryObject->the_post();

    ?>

 <table width="100%" border="0" cellspacing="0" cellpadding="0">

 <tbody>

 <tr>

 <td valign="top" width="1%">
 <div id="videoimg"><a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?>">
                    <?php the_post_thumbnail('video-post'); ?>
                    </a></div>
 </td>

 <td valign="top" width="90%">
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
 </td>

 </tr>


 </table>
 <?php
 }
 ?>
 <?php
 }
 ?>
 </div>

 <div class="entry-info">

 <div class="entry-meta-top">
                    <span class="entry-date"><font color="#e60288"><b><?php the_time(__('F jS, Y', 'kubrick')) ?></b></font></span>
                    <span class="entry-meta-sep">|</span>
                    <span class="entry-cat">Published in: <?php the_category(', '); ?> </span>


 <div id="sharing">
 <span class='st_facebook_hcount' st_title='<?php the_title(); ?>' st_url='<?php the_permalink(); ?>' displayText='share'></span><span class='st_twitter_hcount' st_title='<?php the_title(); ?>' st_url='<?php the_permalink(); ?>' displayText='share'></span><span class='st_plusone_hcount' st_title='<?php the_title(); ?>' st_url='<?php the_permalink(); ?>' displayText='share'></span></div>
            </div>

 <br>
 <?php the_tags( __( '<span class="tag-links"><strong>More On:</strong> ', 'wpbx' ), ", ", "</span>\n" ) ?>


 <div class="entry-content">
                    <?php the_excerpt(); ?>
                </div>


 </div>

 <div class="entry-commm">

 <?php comments_template(); ?></div>


        </div><!-- entry -->





</div><!-- #contentfull -->
</div><!-- #container -->
</div><!-- #wrapper -->

 <?php get_footer() ?>
share|improve this question
    
Please try to follow the progress of your other questions, before posting new ones. Thanks. –  kaiser Mar 8 '12 at 14:42

2 Answers 2

up vote 1 down vote accepted

The problem lies in how you are displaying the videos on the side. Somewhere you are looping through some posts to produce that list. Notice the last post on that list in the screenshot is an interview with once upon a time - and thats what the categories and tags are related to.

Why is this happening?

WordPress stores a global variable $post which is suppose to be the post (object) being viewed, and it's this that the WordPress functions use to display stuff like categories and tags etc related to that post.

In this case, the global $post has been changed when displaying the sidebar lists. Probably because you've used WP_Query or (worse) query_posts (see this warning) or you've used (recommended) get_posts but as you've gone through the loop, you've declared the current post to be global (either by declaring global $post or using the_post() method).

So as you go through the loop, you are over-riding the global $post variable, until you get to the last one - and then it stays on that post. This is why you are seeing that post's categories and tags.

How to fix it...

It depends on how you've made the loop. You can use wp_reset_postdata if you've used the WP_Query object. Or, if you've used the get_posts, don't declare the post as global and instead pass its IDs to the functions. Alternatively, you can store the original global $post (i.e. the main post) in a temp variable and reset it after the loop. See an example of that here.

Hope that helps!

share|improve this answer
    
Hi Stephen thanks for your answer, as you can see i placed my whole php file above, and there you ca see the query and other things, i just don't know which line to change or how :( –  Terrell Anderson Mar 8 '12 at 10:12
    
You'll want to use wp_reset_postdata after your $queryObject loop. Just above <div class="entry-info"> will do. –  Stephen Harris Mar 8 '12 at 10:20
    
THANK YOU :) that worked –  Terrell Anderson Mar 8 '12 at 17:39

Thanks to steven, i found out the correct place to enter the wp_reset_postdata

<?php
}
?>
<?php
}
?>

and here is where you add the code

<?php
}
?>
<?php wp_reset_postdata()
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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