使用wordpress架設網站的人。
對設計美感的追求若是很高。
一定不滿足於wordpress死板板的版面。

 
即使有可視化編輯外掛,也不OK。
一定會想以自創php的頁面。結合頁面來編輯。
可是在WORDPRESS的原生設定中,頁面是無法執行PHP代碼
又不像小工具可以添加代碼處理。
這邊有一個很棒的方法
打開你主題的functions.php
貼入以下代碼
 
  1. /*
  2. 頁面運行php的方法
  3.  */
  4.   function php_include( $attr ) {
  5.     $file = $attr['file'];
  6.     $upload_dir = wp_upload_dir();
  7.     $folder = $upload_dir['basedir'] . '/php-content'. "/$file.php";
  8.     ob_start();
  9.     include ( $folder );
  10.     return ob_get_clean();
  11. }
  12. add_shortcode( 'phpcode', 'php_include' );
第二步、在
  1. wp-content/uploads/
建立一個資料夾
  1. php-content
第三步、在資料夾裡面建立xxxxx.php 把你要運行的php代碼貼入 譬如,我要運行「頁面顯示分類文章列表」 php代碼是
 
  1. query_posts('showposts=10&cat=4'); //cat=1为调用ID为1的分类下文章
  2. while(have_posts()) : the_post(); ?>
  3. ""title="<?php the_title(); ?>"> 
是文章id
 
  1. cat=4
是文章id
 
  1. showposts=10
代表文章數量10
EX:我是建立post4.php 於是你的簡碼是
 
  1. [phpcode file="post4"]
你就可以貼在頁面裡面,就會運行了
相關閱讀