WordPress主題修改

自動設定第1張圖片為特色圖片

主題 functions.php 加入:

//Automatically Set the Featured Image in WordPress
function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

 

解決文章頁面重複顯示特色圖:

主題single.php 刪除

<?php if( has_post_thumbnail()){?>
  <divclass="post-thumbnail">
    <?php the_post_thumbnail('single-image');?>
  </div>
  <!-- END post-thumbnail -->
<?php }?>

解決首頁新文章及置頂文章重覆

index.php 刪除

<code>

// Show only one Sticky Post
$sticky = get_option( ‘sticky_posts’ );
$args = array(
‘posts_per_page’ => 1,
‘post__in’ => $sticky,
‘ignore_sticky_posts’ => 1
);
query_posts( $args );
if ( count($sticky)>0 ) {
while (have_posts()) : the_post();
get_template_part( ‘loop’, ‘index’ );
endwhile;
}
wp_reset_query();

</code>