首篇文章獨立格式

繼上次的 query_posts 可以讓你抓出特定範圍內文章的教學後,這次是在外觀做更特殊的變化,一般來說一頁多篇文章時每篇文章的css樣式都是一樣的,當然每篇文章顯示的資料如標題、內容、發表日期等都是一樣的,但是如何再加點小程式碼就可以讓第一篇文章顯示的樣式與之後的文章是不一樣的喔!!!

效果就像上圖那樣,可以第一篇顯示文章縮圖及標題,但是之後的文章都只顯示標題就像是清單顯示似的,其原理很簡單,簡單的說就是利用迴圈增加數值來判斷抓出最初的文章。

首先我以default這個主題做範例,在index.php尋找出以下類似迴圈程式碼

13
<?php while (have_posts()) : the_post(); ?>

 

然後在那段程式碼上面加入下面這段程式碼

1
<?php $post_number=1; ?>

 

並且在迴圈程式碼下面加入下面這段程式碼

1
2
3
4
<?php
	if ( $post_number==1)
	{
?>

 

改完後的樣子就像這樣

1
2
3
4
5
6
<?php $post_number=1; ?>
<?php while (have_posts()) : the_post(); ?>
	<?php
		if ( $post_number==1)
		{
	?>

 

再來是找出下面這段結束迴圈的程式碼

26
<?php endwhile; ?>

 

並且在其上面加入以下這段程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
	}
	else
	{
?>
			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
				<h2><a href="<?php&phpMyAdmin=93d1103296b5108d2a2431f18e2f0775 the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
 
				<div class="entry">
					<?php the_content('Read the rest of this entry &raquo;'); ?>
				</div>
 
				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
			</div>
<?php
	}
	$post_number++;
	?>

 

if ( $post_number==1) { ... } 包住的內容就是第一篇文章的程式碼,而 else { ... } 的部份就是第二篇文章之後的程式碼啦,接下來就隨你改囉~~

目前沒有任何文章。
下一篇文章 » »
自定義新增 widget(模組)
標籤: ,