WordPress.org

Support

Support » How-To and Troubleshooting » How to get post ID in the loop in the custom template for page?

How to get post ID in the loop in the custom template for page?

  • Hello,

    I can’t get current post id in the loop. Its give me the id of the page 🙁 What i do wrong?

    <?php
    /**
     * Template Name: Page of work
     *
     * Selectable from a dropdown menu on the edit page screen.
     */
    define('WP_USE_THEMES', false); get_header(); ?>
    <script src="<?php echo get_home_url(); ?>/wp-content/themes/lowe/js/slides.min.jqueryWork.js"></script>
    <link rel="stylesheet" href="<?php echo get_home_url(); ?>/wp-content/themes/lowe/css/slideW.css">
    
    <?php $thumb_id = get_post_thumbnail_id( $page->ID);?>
    <?php $image = wp_get_attachment_image_src( $thumb_id,'full' ); ?>
    
    <div id="container" style="height: 741px; background-image: url('<?php echo $image[0]; ?>')">
    				<div id="slides">
    					<div class="slides_container">
    
    						<div class="slide">
    
    							<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    								<?php $thumb_ids = get_post_thumbnail_id(  $post->ID );?>
    								<?php $images = wp_get_attachment_image_src( $thumb_ids,'full' ); ?>
    								<div class="item">
    									<div class="ipop"></div>
    									<a href=""><img width="260px" height="137px" class='imgpop' tekst='<?php echo get_post_meta(42, 'caption_lt', true); ?>' src="<?php echo $images[0]; ?>"></a>
    								</div>
    
    							<?php endwhile;  ?>
    							<?php endif; ?>
    						</div>
    
    						<div class="slide">
    
    						</div>
    
    						<div class="slide">
    
    						</div>
    
    					</div>
    				</div>
    			</div>			
    
    <?php get_footer(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • <?php $thumb_ids = get_post_thumbnail_id( $post->ID );?>

    In this line the loop gives me not the current post ID but the page ID. 🙁

    <?php global $post; ?>
    						<?php $my_query = get_posts('category=5'); ?>
    
    						<?php foreach($my_query as $post) : ?>
    							<?php $thumb_ids = get_post_thumbnail_id(  $post->ID ); ?>
    							<?php $images = wp_get_attachment_image_src( $thumb_ids,'full' ); ?>
    							<div class="item">
    									<div class="ipop"></div>
    									<a href=""><img width="260px" height="137px" class='imgpop' tekst='<?php echo get_post_meta($post->ID, 'caption_lt', true); ?>' src="<?php echo $images[0]; ?>"></a>
    							</div>
    						<?php endforeach; ?>

    I used this loop instead of this and evrything works now 🙂

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    								<?php $thumb_ids = get_post_thumbnail_id(  $post->ID );?>
    								<?php $images = wp_get_attachment_image_src( $thumb_ids,'full' ); ?>
    								<div class="item">
    									<div class="ipop"></div>
    									<a href=""><img width="260px" height="137px" class='imgpop' tekst='<?php echo get_post_meta(42, 'caption_lt', true); ?>' src="<?php echo $images[0]; ?>"></a>
    								</div>
    
    							<?php endwhile;  ?>
    							<?php endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get post ID in the loop in the custom template for page?’ is closed to new replies.
Skip to toolbar