WordPress.org

Forums

[resolved] Images are not showing in my blog (10 posts)

  1. iversoncru
    Member
    Posted 1 year ago #

    Here is my code for the blog, but the images attached to every post are not showing.
    Can anyone help me?

    <article>
    		<?php
    			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    			$temp = $wp_query;
    			$wp_query= null;
    			$wp_query = new WP_Query();
    			$args = array(
    				'posts_per_page' => 3,
    				'cat'            => 7,
    				'paged'          => $paged,
    			);
    			var_dump($args);
    			$wp_query->query( $args );
    			while ( $wp_query->have_posts() ) : $wp_query->the_post();				$post_id = get_the_ID();
    		?>
    		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>   <?php the_time('F jS, Y') ?></h2>
    		<?php
    			$args = array(
    				'post_type' => 'attachment',
    				'order' => 'ASC',
    				'numberposts' => 1,
    				'post_mime_type' => 'image',
    				'post_parent' => $post_id
    			);
    			var_dump($args);
    			$images = get_posts($args);
    			if ($images) {
    				foreach ($images as $image) {
    					echo wp_get_attachment_image( $image->ID, 'thumbnail' );
    				}
    			}
    		?>
    		<p>
    		<?php
    			$excerpt = get_the_excerpt();
      			echo string_limit_words($excerpt,45);
    		?>
    		</p>
    		<p class="read_more">Read the full post <a href="<?php the_permalink(); ?>">here</a></p>
    		<?php endwhile; ?>
    		<?php if ( $paged > 1 ) { ?>
    			<nav id="nav-posts">
    			<div><?php next_posts_link( ' Previous' ); ?></div>
    			<div><?php previous_posts_link( 'Next' ); ?></div>
    			</nav>
    		<?php } else { ?>
    			<nav id="nav-posts">
    			<div><?php previous_posts_link( 'Previous ' ); ?></div>
    		    </nav>
    		<?php } ?>
    		<?php wp_reset_postdata(); ?>
    		<?php $wp_query = null; $wp_query = $temp; ?>
    </article>
  2. iversoncru
    Member
    Posted 1 year ago #

    maybe i can use also some advice. This is the first time i try to do a blog, and im not sure if i did right using query(), for example.

  3. Krishna
    Volunteer Moderator
    Posted 1 year ago #

    Temporarily deactivate all plugins, revert to Twenty Twelve theme and see if images are showing up. If they do, it's a problem with a plugin or the theme.

  4. frizax
    Member
    Posted 1 year ago #

    I have just checked the code shared by you, it working fine and showing images to me.

    Please do some more debugging in your code by:

    1. add "var_dump($post_id);" above "$images = get_posts($args);" to check is there is any post or not. If there is no record, it means the arguments passed in first $args are not correct.
    2. add "var_dump($images);" after "$images = get_posts($args);" to check is there are images or not. If empty, check there is any image attached to the post.

  5. iversoncru
    Member
    Posted 1 year ago #

    hi! thanks a lot for your answers and your time.

    frizax, I have done your suggested debugging and in the sencond one, it says 'empty'.
    But i have an image attached to the post (attached when i created the post in WP admin panel), and actually i can see it when i see the single post.
    weird :S

  6. frizax
    Member
    Posted 1 year ago #

    Just go to the Media section from Admin interface. Here you can see which image is attached with which post.

    Note: An image will attached with post only if you upload it from the edit screen of that particular post. If you select image from media library, it will not linked with your post (just display in post).

  7. iversoncru
    Member
    Posted 1 year ago #

    You are right, frizax.
    When i went to the Media Library, there is no image attached to the post, but my doubt now is:
    I could attached the image when i created the post, and i still can see it. When i preview the post, i see the image as well, but it is not in the media library.
    What is happening?

  8. iversoncru
    Member
    Posted 1 year ago #

    oh, i see!! thanks a lot for the explanation.

    by the way, since the image is in the Media library, i attach it to the post, and everything works perfect!!

    Thankssssssssss

  9. iversoncru
    Member
    Posted 1 year ago #

    I have figure out sth different to do the same thing.

    <article>
    	<?php
    		$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    		$temp = $wp_query;
    		$wp_query= null;
    		$wp_query = new WP_Query();
    		$args = array(
    		'posts_per_page' => 2,
    		'cat'            => 7,
    		'paged'          => $paged,
    		);
    		$wp_query->query( $args );
    		while ( $wp_query->have_posts() ) : $wp_query->the_post();
    		$post_id = get_the_ID();
    	?>
    	<?php
    		$title = get_the_title();
    		if (strlen($title) > 20) {
    			$title = substr($title, 0, 18) . '...';
    		}
    	?>
    	<h2><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a>   <?php the_time('F jS, Y') ?></h2>
    	<?php the_post_thumbnail('thumbnail');?>
    	<p>
    	<?php
    		$excerpt = get_the_excerpt();
      		echo string_limit_words($excerpt,56);
    	?>
    	</p>
    
    	<p class="read_more">Read the full post <a href="<?php the_permalink(); ?>">here</a></p>
    	<?php endwhile;
    		$page_navi = function_exists('wp_pagenavi') ? true : false ;
    		($page_navi) ? wp_pagenavi() : posts_nav_link();
    	?>
    	<?php wp_reset_postdata(); ?>
    	<?php $wp_query = null; $wp_query = $temp; ?>
    </article>

    to use the_post_thumbnail(), i had to add to my functions.php:
    add_theme_support( 'post-thumbnails' );

    And change the way to attach images to the posts: inside the post, click no "Set featured image".

    I hope it helps :) this is where i took it from... http://wordpress.org/support/topic/no-image-showing-in-single-post?replies=5

  10. iversoncru
    Member
    Posted 1 year ago #

    i think we can make the topic Resolved

Topic Closed

This topic has been closed to new replies.

About this Topic