WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Calling the featured image (5 posts)

  1. tab0429
    Member
    Posted 9 months ago #

    I'm trying to create a category page that lists posts by featured image thumbnails and titles only. I'm using the code:

    <?php
    // Show a selected number of posts per row
    $posts_per_row = 4;
    $posts_per_page = 20;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'posts_per_page' => $posts_per_page,
       'paged' => $paged,
    );
    query_posts($args);
    if (have_posts()) {
       while (have_posts()) {
          the_post();
          if ((++$post_counter % $posts_per_row) == 1  || $posts_per_row == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of post_row -->\n";  // End previous row
             }
             echo "<div class='post_row'>\n";  // Start a new row
          }
          echo "<div class='post_class'>\n";  // Start one post
          // Output post data here
          echo '  <a href="<?php the_permalink() ?>"> <?php the_post_thumbnail(); ?></a>
    			  <h3><a href="<?php the_permalink() ?>"> <?php the_title(); ?></a></h3>';
          echo "</div><!-- End of post_class -->\n";  // End of post
       } ?>
    
          </div><!-- End of post_row -->
          <div class='clear'></div>
          <div class="navigation">
          	<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
          	<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
          </div>
       <?php } else {
       }
    ?>

    But it's not working. What am I doing wrong? Can anyone help please?

  2. alchymyth
    Forum Moderator
    Posted 9 months ago #

    what is the result?
    can you post a link to illustrate how it is not working?

    what template are you using?

    what theme are you working with?

  3. tab0429
    Member
    Posted 9 months ago #

    The result is blank. I'm guessing it's because of the code in the end

    <?php } else {
       }
    ?>

    I'm writing my own theme (yes, it's my first!). Here's the link:

    Category Page

    Thanks!

  4. alchymyth
    Forum Moderator
    Posted 9 months ago #

    if you check the page source in the browser, there is some output which looks like un-rendered php code.

    remove the 'echo' from this line and add the respective php tags:

    // Output post data here
          echo '  <a href="<?php the_permalink() ?>"> <?php the_post_thumbnail(); ?></a>
    			  <h3><a href="<?php the_permalink() ?>"> <?php the_title(); ?></a></h3>';

    i.e. change it to:

    // Output post data here ?>
          <a href="<?php the_permalink() ?>"> <?php the_post_thumbnail(); ?></a>
    			  <h3><a href="<?php the_permalink() ?>"> <?php the_title(); ?></a></h3>
    <?php

    or alternatively, if you want to work with the 'echo', try:

    // Output post data here
          echo '  <a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a>
    			  <h3><a href="' . get_permalink() .'">' . get_the_title() . '</a></h3>';

    (not tested)

    also, do not add a custom query to the category template;
    i.e. remove this section:

    $posts_per_page = 20;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'posts_per_page' => $posts_per_page,
       'paged' => $paged,
    );
    query_posts($args);

    just set the 'posts per page' from within the dashboard - settings - reading

    if you want your category archive page to have a different number of posts than the reset of your site, review the use of 'pre_get_posts'; http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  5. tab0429
    Member
    Posted 9 months ago #

    Perfect! That worked like a charm. Thank you so much for your help!!

Reply

You must log in to post.

About this Topic