現在近期文章不能用了-自訂項目 ▸ 小工具 -PHP+HTML–>已經沒有這個小工具
所以改用另一個外掛 Code Widget
程式碼的寫法就要改用PHP的echo來輸出含有html的內容
首先要統一將Html裡面的引號都改成雙引號
這樣echo就可以統一用單引號,將Html內容以字串形式輸出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?php echo'<ul>'; echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" >'; echo'<a href="/category/%e5%90%9b%e5%ad%90%e4%b8%8d%e5%99%a8/">君子不器</a>'; echo'<ul class="children" style="padding-left: 2em;">'; $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 49, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ): echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;">'; echo'<a href="'.get_permalink($post_item['ID']).'">'.$post_item['post_title'].'</a>'; echo'</li>'; endforeach; echo'</ul>'; echo'</li>'; echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" >'; echo'<a href="/category/%e6%88%91%e7%9a%84%e7%9b%b8%e6%a9%9f/">我的相機</a>'; echo'<ul class="children" style="padding-left: 2em;">'; $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 137, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ): echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;">'; echo'<a href="'.get_permalink($post_item['ID']).'">'.$post_item['post_title'].'</a>'; echo'</li>'; endforeach; echo'</ul>'; echo'</li>'; echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" >'; echo'<a href="/category/%e8%87%aa%e5%ad%b8%e7%8e%a9%e7%8e%a9/">自學玩玩</a>'; echo'<ul class="children" style="padding-left: 2em;">'; $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 64, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ): echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;">'; echo'<a href="'.get_permalink($post_item['ID']).'">'.$post_item['post_title'].'</a>'; echo'</li>'; endforeach; echo'</ul>'; echo'</li>'; echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" >'; echo'<a href="/category/%e9%9a%a8%e6%89%8b%e5%af%ab%e5%af%ab/">隨手寫寫</a>'; echo'<ul class="children" style="padding-left: 2em;">'; $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 107, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ): echo'<li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;">'; echo'<a href="'.get_permalink($post_item['ID']).'">'.$post_item['post_title'].'</a>'; echo'</li>'; endforeach; echo'</ul>'; echo'</li>'; echo'</ul>'; ?> |
WordPress 其實也有內建的近期文章的功能
但是沒辦法顯示一次所有的分類
最後是透過外加程式碼的來達成客製化”近期文章”
並且將程式碼透過佈景主題提供的 自訂項目 ▸ 小工具 -PHP+HTML呈現在首頁的右側Sidebar
程式碼會依據文章的分類數而重複幾次,後來感覺跟內建的差不了多少….
程式碼說明如下
#1-#16 是一個文章分類透過ul,跟文章的li,形成一個項目階層,參考”文章分類”的設定
#2 文章分類的超連結
#5 wp_get_recent_posts( )是 WordPress內建函式,可以設定取回最近文章的參數
必須注意的是category只能填入一個分類ID
這也是我為什麼要寫4個 li (4個分類)的原因
透過$recent_posts來接收回傳值=該分類文章的所有資料,並在後面透過foreach迴圈取得資料
#10-14 利用foreach迴圈來取每一筆資料;get_permalink()也是內建函式,可以取得文章的連結
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<ul> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" ><a href="/category/%e5%90%9b%e5%ad%90%e4%b8%8d%e5%99%a8/">君子不器</a> <ul class="children" style="padding-left: 2em;"> <?php $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 49, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ) : ?> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;"> <a href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo $post_item['post_title'] ?></a> </li> <?php endforeach; ?> </ul> </li> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" ><a href="/category/%e6%88%91%e7%9a%84%e7%9b%b8%e6%a9%9f/">我的相機</a> <ul class="children" style="padding-left: 2em;"> <?php $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 137, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ) : ?> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;"> <a href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo $post_item['post_title'] ?></a> </li> <?php endforeach; ?> </ul> </li> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" ><a href="/category/%e8%87%aa%e5%ad%b8%e7%8e%a9%e7%8e%a9/">自學玩玩</a> <ul class="children" style="padding-left: 2em;"> <?php $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 64, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ) : ?> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;"> <a href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo $post_item['post_title'] ?></a> </li> <?php endforeach; ?> </ul> </li> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;" ><a href="/category/%e9%9a%a8%e6%89%8b%e5%af%ab%e5%af%ab/">隨手寫寫</a> <ul class="children" style="padding-left: 2em;"> <?php $recent_posts = wp_get_recent_posts(array( 'numberposts' => 4, // Number of recent posts thumbnails to display 'category' => 107, 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ) : ?> <li class="cat-item" style="border-top: 1px solid #e9e9e9;border-bottom: 1px solid #e9e9e9;padding: 6px 0;"> <a href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo $post_item['post_title'] ?></a> </li> <?php endforeach; ?> </ul> </li> </ul> |