Skip to content

Instantly share code, notes, and snippets.

@WenLiangTseng
WenLiangTseng / translate_latin1_to_utf8.php
Created July 22, 2013 06:10
將資料庫由latin1轉換為utf-8
<?php
define('DB_NAME', 'putyourdbnamehere'); // 數據庫名
define('DB_USER', 'usernamehere'); // MySQL用戶名
define('DB_PASSWORD', 'yourpasswordhere'); // 密碼
define('DB_HOST', 'localhost'); // 很大可能你無需修改此項
function UTF8_DB_Converter_DoIt() {
$tables = array();
$tables_with_fields = array();
@WenLiangTseng
WenLiangTseng / insert_wp_shortcode_into_php.php
Created July 22, 2013 06:08
將 Wordpress 的 shortcode 插入 PHP 的語法
<?php echo do_shortcode('[shortcode option1="value1" option2="value2"]'); ?>
//加入 functions.php
function add_themescript(){
if(!is_admin()){
wp_enqueue_script('jquery');
wp_enqueue_script('thickbox',null,array('jquery'));
wp_enqueue_style('thickbox.css', '/'.WPINC.'/js/thickbox/thickbox.css', null, '1.0');
}
}
@WenLiangTseng
WenLiangTseng / wordpress_show_5_newest_posts_of_all_category.css
Last active December 20, 2015 01:49
讓Wordpress顯示出所有目錄的最新5篇文章
#page-not-found dl {
width: 200px;
float: left;
padding: 0 18px 0 0;
height: 250px;
}
#page-not-found dt {
font-weight: bold;
font-size: 1.1em;
@WenLiangTseng
WenLiangTseng / wordpress_custom_excerpt.php
Created July 22, 2013 06:03
Wordpress客製化摘要,自訂摘要及內文
<?php
function my_excerpt($limit) {
$excerpt = explode('', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode("",$excerpt).'...';
} else {
$excerpt = implode("",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
@WenLiangTseng
WenLiangTseng / wordpress_add_elements_to_menu.php
Created July 22, 2013 06:02
Wordpress增加element到選單上,增加搜尋框到WP選單上、增加日期、增加部落格標題
<?php
//----- add a search bar to your nav menu
add_filter('wp_nav_menu_items','add_search_box', 10, 2);
function add_search_box($items, $args) {
ob_start();
get_search_form();
$searchform = ob_get_contents();
ob_end_clean();
@WenLiangTseng
WenLiangTseng / wordpress_add_login_link_to_menu.php
Created July 22, 2013 06:01
Wordpress-選單加上登入連結
<?php
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
//簡單寫法
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
@WenLiangTseng
WenLiangTseng / wordpress_change_the_loop_query.php
Created July 22, 2013 05:59
wordpress-改變 the Loop 的 query
<?php
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
//簡單寫法
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
@WenLiangTseng
WenLiangTseng / wordpress_add_items_before_and_after_menu.php
Created July 22, 2013 05:58
Wordpress-加項目到選單前後
<?php
//加項目到Wordpress選單前後
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
$homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
$feedlink = '<li class="feed"><a href="' . get_bloginfo_rss('rss2_url') . '">' . __('RSS') . '</a></li>';
$random_text = '<li class="rainbows"><a href="#">OMG ... Double Rainbows!</a></li>';
$items = $homelink . $items;
$items = $items . $feedlink;