Skip to main content

三、wordpress列表页模板主题开发(聚合页)调用标签

1、wordpress列表页循环调用标签

<?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <li class=" homebk1-item"> 
      <a href="<?php the_permalink(); ?>">    //链接
       <div class="homebk1-img"> 
        <img src="<?php $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); echo $full_image_url[0]; ?>" />  //缩略图(特色图片)
       </div> 
        <h3><?php the_title();?></h3>  //标题方法一
        <h3><?php  wp_trim_words( get_the_title(), 10 );?></h3> //标题方法二 可限制字数
        <p><? the_excerpt(); ?></p>   //简介方法一
        <p><?php  wp_trim_words( get_the_excerpt(), 20 );?></p> //简介方法二 可限制字数
      </a> 
	   <p><?php the_date_xml()?> </p>
     </li> 
  <?php endwhile;?>
 <?php endif; ?>

 

2、当前分类栏目名、分类栏目id、栏目链接

<?
	$category_title= single_cat_title('', false );
	$category_id = get_cat_ID($category_title);
	$category_link = get_category_link( $category_id );
	echo $category_title; //输出当前分类名
	echo $category_id;    //输出当前分类id
	echo $category_link   //输出当前分类链接
?>

 

3、当前分类栏目简介描述(Description)

<?php echo category_description(); ?>

 

4、当前分类所属的顶级分类栏目的分类名、分类链接

<a href="<?php echo get_category_link(get_category_root_id($cat)); ?>">
<?php echo get_cat_name(get_category_root_id($cat)); ?></a>

 

5、wordpress分页标签 (方法三:functions添加分页代码)

<? posts_nav_link(); ?>  //方法一  官方默认调用方法
<?php if(function_exists('wp_page_numbers')) : wp_page_numbers(); endif; ?> //方法二 需用插件 wp-page-numbers
<?php kriesi_pagination($query_string); ?>  //方法三:自定义分页代码,可以根据需要更改分页代码-需在functions添加分页代码

 

6、栏目页面包屑调用标签 

//方法一 直接在需要放置面包屑的地方添加如下代码
<a href="<? bloginfo('url'); ?>">首页</a></li>&gt; 
		<?
		if(is_category()){single_cat_title();}
		elseif(is_search()){echo $s;}
		elseif(is_single()){
			$cat=get_the_category();
			$cat=$cat[0];
			echo '<a href="'.get_category_link($cat).'">'.$cat->name. ' </a>';
		}elseif(is_page()){ 
			the_title();
		}elseif(is_404()){echo '404错误页面';}
 
?>
 
方法二  在functions.php文件下添加如下代码 
 
function wz(){
$cat=get_the_category();
$cat=$cat[0];
$positions = '<li><a href="'.get_category_link($cat).'">'.$cat->name. '</a></li>&gt;'; 
if(!is_home() ){ 
	echo '<li><a href="'. get_settings('home') .'">'. '首页&gt;</a></li>';  
if(is_category()){
	echo $positions;
}
elseif(is_single()){
	echo $positions ;
    echo  the_title();
}
elseif(is_search()){echo $s;}
elseif(is_page()){ 
	the_title();
}elseif(is_404()){echo '404错误页面';}
 
 } 
}
 
//前台调用代码  <? wz(); ?>

 

7、调用当前栏目子菜单

在需要调用当前顶级分类栏目的子分类处添加如下代码
<?php
if(is_single()||is_category()) { //如果是文件页面或分类页
 if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" ) {//如果有子分类
	echo '<ul class="sidebar-list1">';
	echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
	echo '</ul>';
  }else{ 
	    //如果没有获取顶级分类
	   }
}
?>

 

8、不同分类栏目调用不同的模板 

category.php       //默认分类模板
方法一
category-10.php    //10是分类id categor后跟-id 系统会自动获取与之对应的模板 没有对应模板就找对应模板
方法二
category-别名.php  //后台分类目录名称后有一个别名  分类模板category-别名会自动调用别名相同的模板