WordPress.org

Support

Support » Themes and Templates » Set Default Featured Image and Thumbnail for New Posts

Set Default Featured Image and Thumbnail for New Posts

  • Hello,

    We use the same Featured Image for all of our new posts, and I am trying to edit my template code so that it automatically sets the featured image. I know the URL of the image I want to use, but could someone please help me with editing the code?

    Code in Blog Page Template:

    <?php $post_image_size = of_get_option('post_image_size'); ?>
          <?php if($post_image_size=='' || $post_image_size=='normal'){ ?>
            <?php if(has_post_thumbnail()) {
              echo '<a href="'; the_permalink(); echo '">';
              echo '<div class="featured-thumbnail"><div class="img-wrap">'; the_post_thumbnail(); echo '</div></div>';
              echo '</a>';
              }
            ?>
          <?php } else { ?>
            <?php if(has_post_thumbnail()) {
              echo '<a href="'; the_permalink(); echo '">';
              echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">'; the_post_thumbnail('post-thumbnail-xl'); echo '</div></div></div>';
              echo '</a>';
              }
            ?>
          <?php } ?>

    Code in Single Post:

    <?php $single_image_size = of_get_option('single_image_size'); ?>
    				<?php if($single_image_size=='' || $single_image_size=='normal'){ ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail"><div class="img-wrap">'; the_post_thumbnail(); echo '</div></div>';
                }
              ?>
            <?php } else { ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">'; the_post_thumbnail('post-thumbnail-xl'); echo '</div></div></div>';
                }
              ?>
            <?php } ?>

    Any help would be much appreciated.

Viewing 15 replies - 1 through 15 (of 20 total)
  • Moderator Andrew Nevins

    @anevins

    Forum moderator

    How is the image getting in the page at the moment? Is it through CSS as a background image?

    Hi,

    Thanks for getting back to me.
    I’m adding the Featured image to each post manually, using the ‘Featured Image / Set Featured Image’ option from the Add New Post Screen, and then selecting the image manually.

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    If you want the image hard-coded in (static), create a child theme of your current theme.

    Then make a copy of which ever functions outputs the image (in both pages) and edit them to replace the following:
    the_post_thumbnail();
    With a HTML

    <img src="<?php bloginfo('template_directory'); ?>/your-directory/your-image.png" />

    Thanks again.
    How do I create a child theme?
    Is there no way to just edit the code in my existing files?

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    How do I create a child theme?

    http://codex.wordpress.org/Child_Themes

    Is there no way to just edit the code in my existing files?

    You can, but when your theme is updated, your code will be removed and efforts lost forever.

    It’s less heartbreaking this way.

    I have edited my WordPress version a number of times and the code has remained unchanged.

    For example:
    http://www.actrecruit.com/employment/food-and-drinks-could-serve-up-30000-new-jobs-in-ireland/

    I have changed the Single Post file to include my custom share buttons and added code to the bottom to include a form to subscribe to our mailing list, which has always remained even when I have updated WordPress.

    If I was to hardcode it, do I add the code to ‘Blog Page’, ‘Single Post’ or both? Where in the code would I insert it?

    Many thanks again.

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    single post template

    Would I do this?

    <?php $single_image_size = of_get_option('single_image_size'); ?>
    				<?php if($single_image_size=='' || $single_image_size=='normal'){ ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail"><div class="img-wrap">'; the_post_thumbnail(); echo '</div></div>';
                }
              ?>
            <?php } else { ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">'; <img src="<?php bloginfo('template_directory'); ?>/your-directory/your-image.png" />'); echo '</div></div></div>';
                }
              ?>
            <?php } ?>
    Moderator Andrew Nevins

    @anevins

    Forum moderator

    Yes and you would need to change the other occurrence of the_post_thumbnail() to your image.

    Okay, so the code should be like this? Sorry for all the questions and confirmations:

    <?php $single_image_size = of_get_option('single_image_size'); ?>
    				<?php if($single_image_size=='' || $single_image_size=='normal'){ ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail"><div class="img-wrap">'; <img src="<?php bloginfo('template_directory'); ?>/your-directory/your-image.png" />'); echo '</div></div>';
                }
              ?>
            <?php } else { ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">'; <img src="<?php bloginfo('template_directory'); ?>/your-directory/your-image.png" />'); echo '</div></div></div>';
                }
              ?>

    <?php } ?>

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    Yes just remember to change the image sources to your actual image.

    I used the code, and no image is displayed on the blog page and got this error on the post page:

    Parse error: syntax error, unexpected ‘<‘ in /home/actrecru/public_html/wp-content/themes/theme1312/single.php on line 13

    The entire code on the page is:

    <?php get_header(); ?>
    <div id="content" class="grid_12 <?php echo of_get_option('blog_sidebar_pos') ?>">
    	<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class('post'); ?>>
          <article class="single-post">
            <header>
              <h1><?php the_title(); ?></h1>
            </header>
    
            <?php $single_image_size = of_get_option('single_image_size'); ?>
    				<?php if($single_image_size=='' || $single_image_size=='normal'){ ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail"><div class="img-wrap">'; <img src="<?php bloginfo('template_directory'); ?>https://www.actrecruit.com/wp-content/uploads/2012/05/act_recruit_news.jpg" />'); echo '</div></div>';
                }
              ?>
            <?php } else { ?>
              <?php if(has_post_thumbnail()) {
                echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">'; <img src="<?php bloginfo('template_directory'); ?>https://www.actrecruit.com/wp-content/uploads/2012/05/act_recruit_news.jpg" />'); echo '</div></div></div>';
                }
              ?>
    <?php } ?>
    
    <!-- AddThis Button BEGIN -->
    <div class="addthis_toolbox addthis_default_style ">
    <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
    <a class="addthis_button_tweet"></a>
    <a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
    <a class="addthis_counter addthis_pill_style"></a>
    </div>
    <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4f3c2fc23ed53536"></script>
    <!-- AddThis Button END --><br><br>
    <?php the_tags( $before, $sep, $after ); ?><br><br>
            <div class="post-content">
              <?php the_content(); ?>
              <?php wp_link_pages('before=<div class="pagination">&after=</div>'); ?>
            </div><!--.post-content-->
          </article>
    
        </div><!-- #post-## -->
    
    <form action="https://nbge.createsend.com/t/j/s/yuhhuk/" method="post" id="subForm">
    <table cellspacing="5" cellpadding="5" border="5" bordercolor="#09F"rules="none" frame="box" width="100%">
    
    <tr valign="top">
      <td colspan="2" align="left" valign="middle"><h2>Subscribe to our mailing list</h2>To keep up to date with all the latest news and promotions, subscribe to our mailing list.<br></td>
      </tr>
    <tr valign="top">
    <td align="right">Your
      <label for="yuhhuk-yuhhuk">Email address:</label></td>
    <td><input type="text" name="cm-yuhhuk-yuhhuk" id="yuhhuk-yuhhuk" size="30" /></td>
    </tr>
    <tr>
      <td></td>
    </tr>
    <tr valign="top">
    <td></td>
    <td><input type="submit" value="Subscribe" /></td>
    </tr>
    </table>
    </form>
    <br><br>
    
        <nav class="oldernewer">
          <div class="older">
            <?php previous_post_link('%link', '&laquo; Previous post') ?>
          </div><!--.older-->
          <div class="newer">
            <?php next_post_link('%link', 'Next Post &raquo;') ?>
          </div><!--.newer-->
        </nav><!--.oldernewer-->
    
        <?php comments_template( '', true ); ?>
    
      <?php endwhile; /* end loop */ ?>
    </div><!--#content-->
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    Moderator Andrew Nevins

    @anevins

    Forum moderator

    I no longer get the error, but there is still no image:

    http://www.actrecruit.com/news/

    http://www.actrecruit.com/act-recruit/test-3/

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    1 second

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Set Default Featured Image and Thumbnail for New Posts’ is closed to new replies.
Skip to toolbar