2013年10月18日 星期五
程式自動加圖檔
來源:http://wordpress.stackexchange.com/questions/40301/how-do-i-set-a-featured-image-thumbnail-by-image-url-when-using-wp-insert-post
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
2012年4月19日 星期四
分類頁面和標籤頁面title發現中文全部是亂碼
一共有兩種方法,一個通過後台設置,另一個需要改動一下插件代碼。
方法一:後台設置法
進入All In One Seo Pack設置,把Rewrite Title複選框取消,這樣這個插件就不會重寫標題,標題則默認按照Wp默認標題顯示,那麼也不會出現亂碼了,但我們為了方便優化,建議高級用戶選擇方法二。
方法二:修改代碼法
1、進入插件,編輯,選擇All In One Seo Pack插件,然後編輯aioseop。class。php。
2、查找函數function capitalize($s)
3、將下面的一行代碼刪除當然也可以用"/* */"註釋掉。
$tokens[$key] = strtoupper(substr($tokens[$key], 0, 1)) 。substr($tokens[$key], 1);
4、完成之後,再將此文件中的函數:ucwords全部替換為:$this->capitalize
導致標題第一個字符亂碼的原因是:All In One Seo Pack插件總是嘗試將標題的第一個字符轉化為大寫。如果是英文字符,這樣做沒有問題;但大家都知道中文字符包含兩個字節,All In One Seo Pack會根據一個預設的映射表將第一個字節轉化為大寫,這樣就把這個漢字給破壞了。不過capitalize函數可以修改的更完善,不但可以保留原有的功能,而且也不會出現問題。
方法一:後台設置法
進入All In One Seo Pack設置,把Rewrite Title複選框取消,這樣這個插件就不會重寫標題,標題則默認按照Wp默認標題顯示,那麼也不會出現亂碼了,但我們為了方便優化,建議高級用戶選擇方法二。
方法二:修改代碼法
1、進入插件,編輯,選擇All In One Seo Pack插件,然後編輯aioseop。class。php。
2、查找函數function capitalize($s)
3、將下面的一行代碼刪除當然也可以用"/* */"註釋掉。
$tokens[$key] = strtoupper(substr($tokens[$key], 0, 1)) 。substr($tokens[$key], 1);
4、完成之後,再將此文件中的函數:ucwords全部替換為:$this->capitalize
導致標題第一個字符亂碼的原因是:All In One Seo Pack插件總是嘗試將標題的第一個字符轉化為大寫。如果是英文字符,這樣做沒有問題;但大家都知道中文字符包含兩個字節,All In One Seo Pack會根據一個預設的映射表將第一個字節轉化為大寫,這樣就把這個漢字給破壞了。不過capitalize函數可以修改的更完善,不但可以保留原有的功能,而且也不會出現問題。
2010年11月4日 星期四
文章新增欄位外掛custom field template
參考網站http://www.kevinleary.net/advanced-content-management-wordpress-custom-field-templates/
Add The getCustomField() Function to Functions.php
Open up your theme’s functions.php file. If you don’t have one, create it. Add the following PHP function to the file.
// Get Custom Field Template Values function getCustomField($theField) { global $post; $block = get_post_meta($post->ID, $theField); if($block){ foreach(($block) as $blocks) { echo $blocks; } } }
Load The Content into Your Theme
Next we need to take the content created by these new fields and add it to our template. To do this we’ll need some basic PHP.<div id="block-1" class="content-box">
<h2>Block #1</h2>
<div class="entry">
<?php getCustomField('Block #1'); ?>
</div>
</div>
<div id="block-2" class="content-box">
<h2>Block #2</h2>
<div class="entry">
<?php getCustomField('Block #2'); ?>
</div>
</div>
訂閱:
文章 (Atom)