• Hi, after struggling and googling for last 2-3 days I have failed to find solution to my problem. I want to display my post text and images seperately, like in two different columns. As a newbie to wordpress I was not sure how to do that so I followed dew tricks found online. Unfortunatly, they work but not the way I want them to. Now its displayign all the images present in media instead of the images which I inserted in the current post. I found the following snippet in almost every solution related to this topic. As I am not advance on wordpress I am not sure if I am missing something or it is not going to do my job anyway.
    Anyone helping will be really a savior as I need this feature on my curent project which is due to go live very soon.

    Thanks in advance.

    Code:

    <?php
    $args = array( 'post_type' => 'attachment', 'post_status' => 'any', 'post_parent' =>  $post->ID );
    $attachments = get_post($args);
    if ($attachments) {
    foreach ( $attachments as $attachment ) {
    echo '<div class="col-md-4 col-sm-4 col-xs-6">';
    the_attachment_link( $attachment->ID , flase );
    echo '</div>';
    }
    }
    wp_reset_postdata();
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi suparna chowdhury

    In what theme template file are you using this and are you using this inside the loop?
    https://developer.wordpress.org/themes/basics/the-loop/

    I’m asking because it seems the $post object is not available from where you are using this.

    Try echoing the $post ID like this in the same theme template file:

    echo 'post ID = ' . $post->ID;

    Thread Starter suparna chowdhury

    (@suparna-chowdhury)

    Hi keesiemeijer
    Thanks for your prompt reply. I am using this for a custom post template and yes I am calling it inside a loop. I tried doing what you suggested with no luck. Right not it is not showing any images at all. But could it be as I installed a lightbox plugin? My coding is not advanced so I can not detect where I am going wrong.
    Here is the full loop where I am calling this.

    <?php
    // Apps single post
    ?>
    <?php get_header();?>
    <div class="container-fluid content-area">
    <?php
    if(have_posts()): while(have_posts()): the_post();
    ?>
    	<div id="app">
    		<div class="row space">
    	    	<div class="col-md-12 col-sm-12 col-xs-12">
    		        <h4 class="app-title">
    		            <?php the_title();?>
    		        </h4>
    	    	</div>
    
    			<div class="col-md-8 col-sm-8 col-xs-12 app-info">
    				<p>
    					<?php echo wp_strip_all_tags( get_the_content() );?>
    				</p>
    				<div class="space app-desc">
    					<?php echo get_post_meta(get_the_ID(), 'app_desc', true);?>
    				</div>
    			</div>
    
    			<div class="col-md-4 col-sm-4 col-xs-12">
    				<div>
    					<?php echo 'post ID = ' . $post->ID;?>
    					<?php $post_thumbnail_attr = array(
    						'class' => 'img-responsive single-post-img',
    						'alt' => 'Post featured image',
    						'title' => 'Post featured image'
    						);
    
    						if(has_post_thumbnail()) :
    							the_post_thumbnail('full', $post_thumbnail_attr);
    						endif;
    					?>
    				</div>
    				<div>
    					<?php $field_name="app_store"; $field_value = get_post_meta($post->ID, $field_name, true);?>
    					<a>" title="Visit <?php the_title_attribute();?> at Google Playstore" class="btn get-app-btn">
    						Get app now!
    					</a>
    				</div>
    			</div>
    		</div>
    
    		<div class="row space" id="app-assets">
    			<div class="col-md-6 col-sm-6 col-xs-12">
    				<?php echo 'post ID = ' . $post->ID;?>
    				<?php
    					$args = array( 'post_type' => 'attachment', 'post_status' => null, 'post_parent' =>  $post->ID );
    					$attachments = get_post($args);
    					if ($attachments) {
    					foreach ( $attachments as $attachment ) {
    						echo '<div class="col-md-4 col-sm-4 col-xs-6">';
    						the_attachment_link( $attachment->ID , flase );
    						echo '</div>';
    						}
    					}
    					wp_reset_postdata();
    				?>
    			</div>
    			<div class="col-md-6 col-sm-6 col-xs-12">
    				<iframe width="560" height="315" src="https://www.youtube.com/embed/R8RNC8eOXzA?list=PLwEFOvXTN52CzUt_XUe26Z6Tgz-0laUrp" frameborder="0" allowfullscreen>
    				</iframe>
    			</div>
    		</div>
    	</div>
    
    	<div class="tags">
    		<?php wp_tag_cloud( $args ); ?>
    	</div>
    	<?php endwhile;endif;?>
    	<?php get_sidebar(); ?>
    
    	</div>
    </div>
    <?php get_footer();?>
    Moderator keesiemeijer

    (@keesiemeijer)

    Try changing this

    <?php
    					$args = array( 'post_type' => 'attachment', 'post_status' => null, 'post_parent' =>  $post->ID );
    					$attachments = get_post($args);
    					if ($attachments) {
    					foreach ( $attachments as $attachment ) {
    						echo '<div class="col-md-4 col-sm-4 col-xs-6">';
    						the_attachment_link( $attachment->ID , flase );
    						echo '</div>';
    						}
    					}
    					wp_reset_postdata();
    				?>

    to this:

    <?php
    					$args = array( 'post_type' => 'attachment', 'post_status' => null, 'post_parent' =>  $post->ID );
    					$attachments = get_posts($args);
    					if ($attachments) {
    					foreach ( $attachments as $attachment ) {
    						echo '<div class="col-md-4 col-sm-4 col-xs-6">';
    						the_attachment_link( $attachment->ID , false );
    						echo '</div>';
    						}
    					}
    					wp_reset_postdata();
    				?>

    Thread Starter suparna chowdhury

    (@suparna-chowdhury)

    Hi thanks for your reply. I tried as you suggested, with no luck :(. So I am dropping the idea for now.
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display images attached to current post only’ is closed to new replies.