wordpress模板制作代码段(三)

站点关键字,描述代码:

<?php   
if (is_home() || is_page()) {   
    // 将以下引号中的内容改成你的主页description   
    $description = "露兜博客描述";   
  
    // 将以下引号中的内容改成你的主页keywords   
    $keywords = "WordPress, 博客, 编程,php,ludou";   
}   
elseif (is_single()) {   
    $description1 = get_post_meta($post->ID, "description", true);   
    $description2 = mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200, "…");   
  
    // 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前200字作为描述   
    $description = $description1 ? $description1 : $description2;   
      
    // 填写自定义字段keywords时显示自定义字段的内容,否则使用文章tags作为关键词   
    $keywords = get_post_meta($post->ID, "keywords", true);   
    if($keywords == '') {   
        $tags = wp_get_post_tags($post->ID);       
        foreach ($tags as $tag ) {           
            $keywords = $keywords . $tag->name . ", ";       
        }   
        $keywords = rtrim($keywords, ', ');   
    }   
}   
elseif (is_category()) {   
    $description = category_description();   
    $keywords = single_cat_title('', false);   
}   
elseif (is_tag()){   
    $description = tag_description();   
    $keywords = single_tag_title('', false);   
}   
$description = trim(strip_tags($description));   
$keywords = trim(strip_tags($keywords));   
?>   
<meta name="description" content="<?php echo $description; ?>" />   
<meta name="keywords" content="<?php echo $keywords; ?>" />  

title标题内容代码:

<title>
    <?php if ( is_home() ) {
                bloginfo('name'); echo " - "; bloginfo('description');
            } elseif ( is_category() ) {
                single_cat_title(); echo " - "; bloginfo('name');
            } elseif (is_single() || is_page() ) {
                single_post_title();
            } elseif (is_search() ) {
                echo "搜索结果"; echo " - "; bloginfo('name');
            } elseif (is_404() ) {
                echo '页面未找到!';
            } else {
                wp_title('',true);
            }
    ?>
</title>

wordpress文章循环:

===========================================================
最简单的循环:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
   //显示文章
<?php endwhile; ?>
<?php endif; ?>
===========================================================
一个较为完整点的例子:
<!-- Start the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <!--单个文章的结构(如标题,内容或摘要,评论等等以及div+css) -->
 
 <?php endwhile; else: ?>

<!—如果没有内容显示如下内容 -->
 <p>对不起,没有文章显示</p>

 <!-- End the loop结束 -->
 <?php endif; ?>

导航栏制作代码:

<ul>
<li<?php if (is_home()) { echo ' class="current-cat"';} ?>><a title="Home" href="/">首页</a></li>
<?php
$currentcategory = '';
// 以下这行代码用于在导航栏添加分类列表,
if  (!is_page() && !is_home()) {
$catsy = get_the_category();
$myCat = $catsy[0]->cat_ID;
$currentcategory = '&current_category='.$myCat;
}
wp_list_categories('depth=1&title_li=&show_count=0&hide_empty=0&child_of=0'.$currentcategory);
// 以下这行代码用于在导航栏添加页面列表
// 如果你不想添加页面到导航中,请删除下面wp_list_pages这行。
wp_list_pages('depth=1&title_li=&sort_column=menu_order');
?>
</ul>

cms分类方式代码:

<?php
	  $display_categories = get_all_category_ids();

		foreach ($display_categories as $category) { ?>

<div class="art_content">
<?php query_posts("showposts=10&cat=$category");?>
<h2 class="art_title"><a href="<?php echo get_category_link($category);?>"><?php single_cat_title(); ?></a></h2>
    <ul class="bing_news">
<?php while (have_posts()) : the_post(); ?>
    <li><img src="<?php bloginfo('template_url');?>\images\list.jpg"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo mb_strimwidth(get_the_title(), 0, 45, '…'); ?></a></li>
<?php endwhile; ?>
    </ul>
</div>
<?php } wp_reset_query();?>

与模板相关的其他函数:

<?php _e(’Message’); ?> : 输出相应信息
<?php wp_register(); ?> : 显示注册链接
<?php wp_loginout(); ?> : 显示登录/注销链接
<!–next page–> : 将当前内容分页
<!–more–> : 将当前内容截断,以不在主页/目录页显示全部内容
<?php timer_stop(1); ?> : 网页加载时间(秒)
<?php echo get_num_queries(); ?> : 网页加载查询量