loading...

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 &raquo;’; } ?>
<?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”);

=========================

相关插件

More Fields

Custom Shortcodes

还搜到了帕兰印象的 20+Wordpress自定义字段使用技巧和插件/Custom Fields ,可以研究下~



本站文章未特殊注明均为原创,转载需经本人同意,请善用以下链接

Trackback: http://www.iros.me/wordpress-custom-fields.html/trackback

Google书签 新浪ViVi 365Key网摘 天极网摘 我摘 POCO网摘 博采网摘 YouNote网摘 和讯网摘 博拉网 igooi网摘 I2Key网摘 天下图摘 百特门网摘 Del.icio.us Yahoo书签 奇贴 QQ娱乐摘 添加到Digg! 添加到Facebook!

No Responses to "WordPress Custom Fields 自定义字段使用"

Comment Form

*

[鼻血] [黑线] [鬼脸] [面条泪] [阴笑] [贼笑] [羞] [神气] [礼物] [生气] [满足] [游魂] [汗] [死光] [期待] [晕了] [无神] [扁] [感动] [怒了] [心心眼] [得意] [开心] [大泪] [大惊] [嘟嘴] [唱歌] [哈] [呵欠] [呜呜] [呆] [吓] [吐] [凸] [不爽] [ZZZ] [NANI?] [FUFU] [CHU]



Web Share

About Me

    Enjoy Work!&& Enjoy Life!

  • 年过半百的一半却只能通过绑定来实现社会化的不想宅却没有办法的挺宅的向往B型却只是O型的不会游泳所以只能沉沦腐海的爱K歌但还不到麦霸程度的现在貌似挺稀有的白羊挨踢代码女
  • 硬盘控、下载强迫症、HTC大脸人、伪谷粉
  • Money是好物!Wordpress是好物!Jquery是好物!

Latest Comments

  • 前端开发: 我看了下原文,horizontally 是水平的意思,希望博主尽早发现并修改
  • 前端开发: 第30条跟《高性能网站进阶指南》有冲突,书上是说css精灵要排成横的,而
  • amazon is my niche Keyword.: ... [Trackback]... [...] Read More: iros.me/2009-css-naked-day-jin-tian-shi-css-lu
  • My Homepage: ... [Trackback]... [...] Find More Informations here: iros.me/2009-css-naked-day-j
  • 在线k歌: 有意思 。。
  • liugod: 很好,作为参考
  • Modern Warfare 3: Modern Warfare 3... I saw this really good post today so I had to repost..........
  • En Vivo: Twitter Instantly connect to what's most important to you. Follow your friends, experts, favorite
  • 洋洋: 这个码字得多累啊。。
  • Chong.Freeman: 第一次见这么详细的关于 query_posts 函数的讲解。 学习了! 有这函数,做

疼讯微博口水地

My Place

    Posts 98  Comments 186  
    Blog stared at 2008.10.21
    Blog open at 2008.11.21   Free Traffic Counter
ponytail1985@hotmail.com 358589691 ponie325@gmail.com Ajax CommentLuv Enabled 275ca7ef21959162b094a66262fa328f

无觅相关文章插件,快速提升流量