• Resolved citronks

    (@citronks)


    Hi,

    I’m no really good at php, and I’m fighting to have the post of a category displayed, by featured image.
    To get the featured image, I use a code found on the codex, which is the only one that auto-crops the featured image (without me resinzing it with photoshop), and I succeeded to modify it to make it work the way I want, but when I apply it to one of my categories (with a template for each category like category-test.php), it shows EVERY post that have a featured image.

    I tried almost everything I found on the codex : get_gategory_by_ID, by_slug, the_category, get_the_category, is_category, in_category, query_post… And I tried it at different place in my code.

    So, I now need help, and sorry if this has been solved somewhere, I didn’t found it.

    Here is my code in my template category.

    <?php if ( have_posts() ) : ?>
    
    			</header><!-- .archive-header -->
    
    <div class="category-titles"> Tous mes <?php single_cat_title(); ?> et articles mode</div>
    
    				<ul id="posts-thumb-list">
    <?php /* The loop */ ?>
    
    <?php
    $thumbnails = get_posts( 'numberposts=1000' );
    foreach ( $thumbnails as $thumbnail ) {
    	if ( has_post_thumbnail( $thumbnail->ID ) ) {
    		echo '<div class="thumb-box"><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    		echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
    		echo '<div class="thumb-title-div"> <span class="thumb-title">' . get_the_title ( $thumbnail->ID ) . '</span></div>';
    		echo '</a></div>';
    	}
    }
    ?>
    				</ul>
    
    		<?php else : ?>
    			<?php get_template_part( 'content', 'none' ); ?>
    		<?php endif; ?>

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • the line with get_posts() is overwriting the category query;

    try to replace this section:

    <?php /* The loop */ ?>
    
    <?php
    $thumbnails = get_posts( 'numberposts=1000' );
    foreach ( $thumbnails as $thumbnail ) {
    	if ( has_post_thumbnail( $thumbnail->ID ) ) {
    		echo '<div class="thumb-box"><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    		echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
    		echo '<div class="thumb-title-div"> <span class="thumb-title">' . get_the_title ( $thumbnail->ID ) . '</span></div>';
    		echo '</a></div>';
    	}
    }
    ?>

    with:

    <?php /* The loop */ ?>
    
    <?php
    while( have_posts() { the_post();
    	if ( has_post_thumbnail( $thumbnail->ID ) ) {
    		echo '<li><div class="thumb-box"><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    		echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
    		echo '<div class="thumb-title-div"> <span class="thumb-title">' . get_the_title ( $thumbnail->ID ) . '</span></div>';
    		echo '</a></div></li>';
    	}
    }
    ?>

    and add this to functions.php of your theme:

    function category_archive_posts_number($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ( $query->is_category ) {
          $query->set( 'posts_per_page', 1000 );
        }
      }
    }
    
    add_action('pre_get_posts','category_archive_posts_number');
    Thread Starter citronks

    (@citronks)

    Thanks a lot for your help Alchymyst.
    I works ! With a very little change : it made a syntax error with the first “{” of

    while( have_posts() { the_post();

    so I added parenthesis and here is the final code :

    <?php /* The loop */ ?>
    
    <?php
    while( have_posts()) { )the_post());
    	if ( has_post_thumbnail( $thumbnail->ID ) ) {
    		echo '<li><div class="thumb-box"><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    		echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
    		echo '<div class="thumb-title-div"> <span class="thumb-title">' . get_the_title ( $thumbnail->ID ) . '</span></div>';
    		echo '</a></div></li>';
    	}
    }
    ?>

    Thanks again ! Hope it helps

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show category posts featured image’ is closed to new replies.