JadedEric
JadedEric

Reputation: 2083

WordPress: Not able to get current ID.

I'm very new to WordPress, and I've been pulling hair trying to figure this one out.

I have a custom theme, the theme is very simple, which contains an image carousel built up from a collection of images in a gallery.

If I remove the carousel include file, the post ID on the rest of the page returns correctly, as soon as I add the carousel back, it uses the ID of the last category pulled in the code.

I removed all 'global' references, as I assumed this will override the ID for the rest of the page, but it's still wrong.

The code from the carousel.php file:

<div id="carousel">

    <?php

        $args = array(
            'post_type'         =>  'gallery',
            'post_status'       =>  'publish',
            'name'              =>  $wp_query->query_vars['name'],
            'posts_per_page'    =>  1
        );

        $second_query = new WP_Query($args);
        $gllr_options = get_option('gllr_options');
        $gllr_download_link_title = addslashes(__('Download high resolution image', 'gallery'));

        if ($second_query->have_posts()) : while ($second_query->have_posts()) : $second_query->the_post(); ?>

    <div class="carousel-holder">

        <?php
            the_content();
            $galleries = get_posts(array(
                'showposts'         =>  -1,
                'what_to_show'      =>  'posts',
                'post_status'       =>  'inherit',
                'post_type'         =>  'attachment',
                'orderby'           =>  $gllr_options['order_by'],
                'order'             =>  $gllr_options['order'],
                'post_mime_type'    =>  'image/jpeg,image/gif,image/jpg,image/png',
                'post_parent'       =>  $post->ID
            ));

        if (count($galleries) > 0) { ?>

        <ul id="carousel-gallery">

            <?php foreach ($galleries as $attachment) {

                $key = 'gllr_image_text';
                $link_key = 'gllr_link_url';
                $alt_tag_key = 'gllr_image_alt_tag';
                $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'photo-thumb' );
                $image_attributes_large = wp_get_attachment_image_src( $attachment->ID, 'large' );
                $image_attributes_full = wp_get_attachment_image_src( $attachment->ID, 'full' );

                if ( 1 == $gllr_options['border_images'] ) {
                    $gllr_border = 'border-width: ' . $gllr_options['border_images_width'] . 'px; border-color:' . $gllr_options['border_images_color'] . '';
                    $gllr_border_images = $gllr_options['border_images_width'] * 2;
                } else {
                    $gllr_border = '';
                    $gllr_border_images = 0;
                }

                if (($url_for_link = get_post_meta($attachment->ID, $link_key, true)) != "") { ?>

            <li>
                <img alt="<?php echo get_post_meta($attachment->ID, $alt_tag_key, true); ?>" title="<?php echo get_post_meta( $attachment->ID, $key, true ); ?>" src="<?php echo $url_for_link; ?>" />
            </li>

                <?php } else { ?>

            <li rel="gallery_fancybox<?php if ( 0 == $gllr_options['single_lightbox_for_multiple_galleries'] ) echo '_' . $post->ID; ?>">
                <img alt="<?php echo get_post_meta($attachment->ID, $alt_tag_key, true); ?>" title="<?php echo get_post_meta( $attachment->ID, $key, true ); ?>" src="<?php echo $image_attributes_large[0]; ?>" />
            </li>

                <?php }
                $count_image_block++;
            } ?>

        </ul>
        <div class="clearfix"></div>
        <div id="arrows" class="arrows">
            <a id="prev" class="prev" href="#"></a>
            <a id="next" class="next" href="#"></a>
        </div>
        <div id="paginator" class="paginator"></div>
        <?php } ?>
    </div>
        <?php endwhile; else: endif;?>
</div>

The code that is returning the erroneous ID:

<div class="left sidebar">
     <?php echo the_ID(); ?>
</div>

This is within a template page, that looks like this:

<?php
/**
 * Template Name: 2 Column Left
 */
get_header();
?>

    <div id="maincontent">
        <div id="content" class="site-content" role="main">
            <div class="left sidebar">
                <?php
                    echo the_ID();
                ?>
            </div>
        </div>
    </div>

<?php get_footer(); ?>

Carousel.php is defined within header.php, and then gets called when get_header(); is called.

So, assuming I'm on the About page, with an ID of 16, and the carousel calls a gallery with page ID of 8, the echo in left-sidebar always returns 8.

I'm at a loss here, I've search high and low for an answer to this problem, but I've come up with nothing.

Upvotes: 0

Views: 77

Answers (1)

Jay Bhatt
Jay Bhatt

Reputation: 5651

Try resetting the loop using the wp_reset_query() method.

Place this right after your carousel loop ends.

<?php wp_reset_query(); ?>

Upvotes: 2

Related Questions