0

What I'm Trying To Do

I have set up a custom post type for 'movies' and below is how I set up the template. Basically, I am trying to get a.thickbox (in the code below) to open a modal window with more info on the movie. Notice the addition of &pid=<?php echo $post->ID; ?> at the end of the href.

<?php get_header();?>

    <section id="content">
        <h2>Our Movies</h2> 
        <ul id="all-movies">

            <?php
                $c=0;
                $i=1;

                $temp = $wp_query;
                $wp_query = null;
                $wp_query = new WP_Query();
                $wp_query->query('post_type=movies' . '&paged=' . $paged . '&posts_per_page=6');

                while ( $wp_query->have_posts() ) : $wp_query->the_post(); $c++;

                $custom = get_post_custom($post->ID);
                $movie_info = $custom["movie_info"][0];

            ?>

            <li id="movie-<?php the_ID(); ?>">
                 <?php if (!empty($movie_info)) { echo $movie_info ?>
                 <a class="thickbox" href="<?php bloginfo('url'); ?>/movies-modal?KeepThis=true&TB_iframe=true&height=820&width=610&pid=<?php echo $post->ID; ?>">View more info</a>
            </li>

            <?php
                endwhile;
                my_paginate_links();
                $wp_query = null;
                $wp_query = $temp;
                wp_reset_query();
            ?>

        </ul>
    </section>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

My (Failed) Attempt

What I tried to do was create a separate template (below), create a page called "Movies Modal" in the dashboard, and attribute the template to the page. Then, I tried to grab the POST ID from the a.thickbox link from the template above (&pid=<?php echo $post->ID; ?>) so only that post's info will show in the modal template below with this line: $wp_query->query( 'post_type=movies&p=' . $_GET['pid']);

<?php
/*
Template Name: Movies (Modal)
*/
?>

<?php get_header('thickbox'); ?>

    <?php
        $c=0;
        $i=1;

        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query( 'post_type=movies&p=' . $_GET['pid']);

        while ( $wp_query->have_posts() ) : $wp_query->the_post(); $c++;

        $custom = get_post_custom($post->ID);
        $movie_info = $custom["movie_info"][0];
        $actor_bio = $custom["actor_bio"][0];
        $actor_info = $custom["actor_info"][0];

    ?>

    <div id="container">
         <?php if (!empty($movie_info)) { echo $movie_info ?>
         <?php if (!empty($actor_bio)) { echo $actor_bio ?>
         <?php if (!empty($actor_info)) { echo $actor_info ?>
    </div>

    <?php
        endwhile; 
        $wp_query = null;
        $wp_query = $temp;
        wp_reset_query();
    ?>

</body>
</html>

All this did, however, was bring up a modal window with the info for all posts and not just the info for the post the link is in, like this:

modal window

I can't figure out how to get the info for only the post I clicked on. Can someone please help me out or push me in the right direction?

Updates

pid is not set\ngettype: NULLpid intval: 0Array
(
    [post] => post
    [page] => page
    [attachment] => attachment
    [revision] => revision
    [nav_menu_item] => nav_menu_item
    [product] => product
    [product_variation] => product_variation
    [shop_order] => shop_order
    [shop_coupon] => shop_coupon
    [wpcf7_contact_form] => wpcf7_contact_form
    [movies] => movies
    [psdtv] => psdtv
)
Wordpress version: 3.5

Changed this block:

<li id="movie-<?php the_ID(); ?>">
    <?php if (!empty($movie_info)) { echo '<span>Summary: '. $movie_info .'</span>'; } ?>
    <a class="thickbox" href="<?php bloginfo('url'); ?>/movies-modal/?KeepThis=true&TB_iframe=true&height=820&width=610&pid=<?php the_ID(); ?>">View more info</a>
</li>

1 Answer 1

1

It would appear your use of Thickbox is the culprit. When opening a thickbox via the anchor, the thickbox parameters must always be last, else the other parameters will be removed, e.g. your movies Post ID

http://devll.wordpress.com/2009/10/01/jquery-iframe-thickbox-removes-parameter/

Firstly, your use of queries is incorrect, what's more you never tell your query which post to look for.

Instead you should do a proper query, e.g.:

$movie_query = new WP_Query( array(
    'post_type' => 'movies',
    'p' => $_GET['pid']
));
if($movie_query->have_posts()){
    while ( $movie_query->have_posts() ) {
        $movie_query->the_post();

    $custom = get_post_custom($post->ID);
    $movie_info = $custom["movie_info"][0];
    $actor_bio = $custom["actor_bio"][0];
    $actor_info = $custom["actor_info"][0];

?>

<div id="container">
     <?php if (!empty($movie_info)) { echo $movie_info ?>
     <?php if (!empty($actor_bio)) { echo $actor_bio ?>
     <?php if (!empty($actor_info)) { echo $actor_info ?>
</div>

<?php
    }
}
wp_reset_postdata();

Before you continue, you will need a grasp of how to do a basic query cleanly, and why and when to use it. For this I give you the following:

  • Never modify the main query directly. ( the only time to modify it is when it's passed into a pre_get_posts filter )
  • Avoid query_posts
  • Read this quick presentation by Andrew Nacin, You don't know Query, it will tell you what to use, how to use it, and why. It is considered by many necessary reading.

edit:

Please paste the following snippet above my modified version of your query:

echo '<pre>';
if(isset($_GET['pid'])){
    echo 'pid is set\n';
} else {
    echo 'pid is not set\n';
}
echo 'gettype: '. gettype($_GET['pid']);
echo 'pid intval: '.intval($_GET['pid']);
$post_types=get_post_types();
print_r($post_types);
echo 'Wordpress version: '.get_bloginfo('version');
echo '</pre>';

edit:

This code in your main listing is broken:

        <li id="movie-<?php the_ID(); ?>">
             <?php if (!empty($movie_info)) { echo $movie_info ?>
             <a class="thickbox" href="<?php bloginfo('url'); ?>/movies-modal?KeepThis=true&TB_iframe=true&height=820&width=610&pid=<?php echo $post->ID; ?>">View more info</a>
        </li>

You never added a closing bracket for the if statement or a semi colon. I suggest you also replaced echo $post->ID; with the_ID(); and verify it is indeed printing the correct ID. I'd also suggest putting a '/' between the ?KeepThis and the movies-model

12
  • Thank you, Tom. I am reading through that now. I tried your code (in the modal template); however I am still met with the same outcome. All posts show in the modal window instead of just the one I queried for. Is there more to the code that needs to be added?
    – Desi
    Jan 14, 2013 at 10:33
  • I've copy pasted a snippet of code that should reveal some important information, if you could place it after get_header and report back what the results are
    – Tom J Nowell
    Jan 14, 2013 at 11:42
  • Hey Tom, I posted it in the original post. Thanks.
    – Desi
    Jan 14, 2013 at 11:58
  • There you go, pid is not set, that's the source of your problem. btw you're aware that your first code block in your question would generate a 500 error? You've got an if statement that you never close, and a function call with a missing semicolon
    – Tom J Nowell
    Jan 14, 2013 at 12:05
  • see updated answer
    – Tom J Nowell
    Jan 14, 2013 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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