1. 后台内容要能添加, 通过add_meta_box实现
在functions.php中添加如下代码
function post_meta_boxes_setup() { /* Add meta boxes on the 'add_meta_boxes' hook. */ add_action( 'add_meta_boxes', 'add_post_meta_boxes' ); /* Save post meta on the 'save_post' hook. */ add_action( 'save_post', 'save_post_source_meta', 10, 2 ); } function add_post_meta_boxes() { add_meta_box( 'post-source', // Unique ID esc_html__( '文章来源/作者', 'low-text' ), // Title 'post_source_meta_box', // Callback function 'post', // Admin page (or post type) 'normal', // Context 'high' // Priority ); } function post_source_meta_box( $object, $box ) { ?> <?php wp_nonce_field( basename( __FILE__ ), 'post_source_nonce' ); ?> <p> <label>文章来源</label><br/> <input class="widefat" type="text" name="post-source" id="post-source" value="<?php echo esc_html__( get_post_meta( $object->ID, 'post_source', true ) ); ?>" size="30" /> <label>文章作者</label><br/> <input class="widefat" type="text" name="post-author" id="post-autohr" value="<?php echo esc_html__( get_post_meta( $object->ID, 'post_author', true ) ); ?>" size="30" /> </p> <?php } function save_post_source_meta( $post_id, $post ) { /* Verify the nonce before proceeding. */ if ( !isset( $_POST['post_source_nonce'] ) || !wp_verify_nonce( $_POST['post_source_nonce'], basename( __FILE__ ) ) ) return $post_id; /* Get the post type object. */ $post_type = get_post_type_object( $post->post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST['post-source'] ) ? balanceTags( $_POST['post-source'] ) : '' ); /* Get the meta key. */ $meta_key = 'post_source'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST['post-author'] ) ? balanceTags( $_POST['post-author'] ) : '' ); /* Get the meta key. */ $meta_key = 'post_author'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); }
在wp-admin/post.php, wp-admin/post-new.php中靠前的地方, 调用勾子函数
post_meta_boxes_setup();
2. 简单的读取wordpress的独立库
<?php define("DB_HOST","127.0.0.1:3306"); define("DB_USER","root"); define("DB_PWD","2014pachong!@#"); define("DB_NAME","wordpress"); class CcbddSite{ private $db; public function CcbddSite(){ $this->db=@mysql_connect(DB_HOST,DB_USER,DB_PWD) or die("error connecting"); mysql_select_db(DB_NAME,$this->db); mysql_query("set names 'utf8mb4'",$this->db); } public function getNewsList($termId,$page=1,$pageSize=10){ $start=($page-1)*$pageSize; $sql="SELECT * FROM wp_term_relationships a left join wp_posts b on a.object_id=b.ID left join wp_terms c on a.term_taxonomy_id=c.term_id where a.term_taxonomy_id={$termId} and b.post_status='publish' order by a.object_id desc limit {$start},{$pageSize}"; $rows=array(); $rs = mysql_query($sql,$this->db); while($row = mysql_fetch_assoc($rs)){ $row['date']=substr($row['post_date'],0,10); $rows[]=$row; } return $rows; } public function getTermInfo($id){ $sql="SELECT * FROM wp_terms where term_id={$id}"; $rs=mysql_query($sql,$this->db); if($row=mysql_fetch_assoc($rs)){ return $row; } return null; } public function getNewsListPaginationInfo($termId,$page=1,$pageSize=10){ $sql="SELECT count(*) as total FROM wp_term_relationships a left join wp_posts b on a.object_id=b.ID left join wp_terms c on a.term_taxonomy_id=c.term_id where a.term_taxonomy_id={$termId} and b.post_status='publish'"; $total=0; $rs=mysql_query($sql,$this->db); if($row=mysql_fetch_assoc($rs)) $total=$row['total']; $totalPage=ceil($total/$pageSize); $pageItems=array(); $startPage=$page-4; $endPage=$page+4; if($startPage<1) $startPage=1; if($endPage>$totalPage) $endPage=$totalPage; for($i=$startPage; $i<=$endPage;$i++){ $pageItems[]=$i; } return array( 'page'=>$page, 'totalPage'=>$totalPage, 'total'=>$total, 'pageItems'=>$pageItems ); } public function getNewsDetail($id){ $sql="update wp_posts set view_count=view_count+1 where ID={$id}"; mysql_query($sql,$this->db); $sql="SELECT * FROM wp_posts where ID={$id}"; $rs=mysql_query($sql,$this->db); if($row=mysql_fetch_assoc($rs)){ if($row['post_status']!='publish') return null; return $row; } return null; } public function getPostMeta($id,$name){ $sql="SELECT meta_value FROM wp_postmeta where post_id={$id} and meta_key='{$name}';"; $rs=mysql_query($sql,$this->db); if($row=mysql_fetch_assoc($rs)) return $row['meta_value']; return '未知'; } }
3. 前端代码
很简单的话可以直接用如下格式:
<?php xxxxx ?> <html> .... </html>