WordPress Custom Fields 自定义字段使用
by: ponytail @ 06 五, 2010 in: ┨Skill ,┨Stuuuuuuuudy
wordpress是好物,我的大爱,啥都能做了,最近算是转型期,不知道转不转的过弯来,有用的东西先记录一下~~
1. 设定过期时间
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values(‘expiration’);
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example…
the_title();
the_excerpt();
}
endwhile;
endif;?>
2.区分日志显示方式
<?php if (have_posts()) :
while (have_posts()) : the_post();
$customField = get_post_custom_values(“full”);
if (isset($customField[0])) {
//Custom field is set, display a full post
the_title();
the_content();
} else {
// No custom field set, let’s display an excerpt
the_title();
the_excerpt();
endwhile;
endif;
?>
3.设定页面META内容
<meta name=”description” content=”
<?php if ( (is_home()) || (is_front_page()) ) {
echo (‘Your main description goes here’);
} elseif(is_category()) {
echo category_description();
} elseif(is_tag()) {
echo ‘-tag archive page for this blog’ . single_tag_title();
} elseif(is_month()) {
echo ‘archive page for this blog’ . the_time(‘F, Y’);
} else {
echo get_post_meta($post->ID, “Metadescription”, true);
}?>”>
4.设置特殊的CSS
<?php if (is_single()) {
$css = get_post_meta($post->ID, ‘css’, true);
if (!empty($css)) { ?>
<style type=”text/css”>
<?php echo $css; ?>
<style>
<?php }
} ?>
5. SEO Title优化
<title>
<?php if (is_home () ) {
bloginfo(‘name’);
} elseif ( is_category() ) {
single_cat_title(); echo ‘ – ‘ ; bloginfo(‘name’);
} elseif (is_single() ) {
$customField = get_post_custom_values(“title”);
if (isset($customField[0])) {
echo $customField[0];
} else {
single_post_title();
}
} elseif (is_page() ) {
bloginfo(‘name’); echo ‘: ‘; single_post_title();
} else {
wp_title(”,true);
} ?>
</title>
6.设定不被搜索引擎搜索
<?php $cf = get_post_meta($post->ID, ‘noindex’, true);
if (!empty($cf)) {
echo ‘<meta name=”robots” content=”noindex”/>’;
}
?>
7.使用缩略图(2.9+自带了)
<?php $postimageurl = get_post_meta($post->ID, ‘post-img’, true);
if ($postimageurl) {?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark”>
<img src=”<?php echo $postimageurl; ?>” alt=”Post Pic” /></a>
<?php } else { ?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark”>
<img src=”<?php bloginfo(‘template_url’); ?>/images/wprecipes.gif” alt=”Screenshot” /></a>
<?php } ?>
8.个性化的read more
<?php $custommore = get_post_meta($post->ID, ‘custom_more’, true); ?>
<?php if (!$custommore) { $custommore = ‘Read More »’; } ?>
<?php the_content($custommore); ?>
9.设置个性化背景(与timthumb结合)
<?php if (is_page() || is_single()) {
<?php $background = get_post_meta($post->ID, ‘background’, true);
if ($background) {?>
<style type=”text/css”>background: url(<?php bloginfo(‘template_url’); ?>/timthumb/timthumb.php?w=1920&zc=1&src=<?php echo $background; ?>) fixed no-repeat;</style>
<?php }
}?>
=========================
相关技巧
1.获取循环外的 custom field
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, ‘customField’, true);
?>
2.获得第一张图片
<?php
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘/<img.+src=[\'"]([^\'"]+)[\'"].*>/i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = “/images/default.jpg”;
}
return $first_img;
}?>
3.筛选有某一自定义值的日志
query_posts(“meta_key=Camera_Specs”);
=========================
相关插件
还搜到了帕兰印象的 20+Wordpress自定义字段使用技巧和插件/Custom Fields ,可以研究下~



























