wordpress网站分类列表页、详情页、单页、模板

1、公共头部菜单导航 

get_header();

2、wordpress网站面包屑导航

3、wordpress网站侧边栏

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、当前列表栏目子分类 (functions添加获取当前分类子分类列表代码)

<?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{ 
	    //如果没有获取顶级分类
	   }
}
?>

6、当前列表分类的栏目名_栏目id_栏目链接

<?php echo category_description( $category_id ); ?>  //当前分类描述
<?php echo category_description(); ?>                //当前分类描述
<?echo trim(strip_tags(category_description())); ?>  //当前分类描述 不带默认p标签
<?
	$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   //输出当前分类链接
?>

7、列表循环、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; ?>

8、分页列表标签 (方法三:functions添加分页代码)

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

wordpress的默认分页标签使用起来比较方便,但是如果想做一些样式上的定制,或者做一些修改就比较麻烦了,如果需要定制分页建议使用第方法三 

9、分类栏目自定义字段调用

//$cat 默认为当前分类id   seo-title自定义字段
<? 
$post_id = "category_".$cat; 
$value = get_field( 'seo-title', $post_id );
echo $value;
?>
 
//输出图片字段
<? $post_id = "category_".$cat; echo get_field('img_ioc',$post_id);?>
 
案例
<? $post_id = "category_".$cat; ?>
<title><?php  echo get_field( 'seo-title', $post_id ); ?></title>
<meta name="keywords" content="<?php  echo get_field( 'seo-keywords', $post_id ); ?>"/>
<meta name="description" content="<?php  echo get_field( 'seo-description', $post_id ); ?>"/>

10、WordPress中获取指定分类及其子分类下的文章数目

$category = get_category( $category_id ); 
$category_post_count = $category->category_count;

11、wordpress获取分类下文章列表四种方法

wordpress获取分类下文章列表四种方法 | 511遇见

<?php query_posts('cat=20&posts_per_page=4');while('have_posts()'):the_post();?>
<li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>

其中cat=20代表分类ID,posts_per_page=4代表显示几条记录

12、搜索框

1)、默认的wordpress搜索代码

<?php get_search_form(); ?>

<style>
    .search-field{
        line-height: 35px;
        height: 35px;
        padding: 0;
        margin: 0;
        border: 1px solid #0d66c2;
    }

    .search-submit{
        height: 35px;
        padding:0 15px;
        margin: 0;
        box-sizing: border-box;
        background-color: #0d66c2;
        border: 0;
        color: white;
    }
</style>

2)、自定义的html结构,这一步也很简单,根据上一步wordpress主题自带的获取搜索生成的结构,查看源代码修改即可:

<form role="search"  method="get" class="hx_serch" action="<?php bloginfo('url'); ?>">
    <input type="search" class="hx_input_search" value="" name="s" />
    <input type="submit" class="search-submit" value="搜索" />
</form>

这里两个注意点:

a、wordpress的搜索表单method一定要为get方式;

b、搜索输出框的name属性值一定要为s

3) 高级搜索筛选,支持选择分类筛选:

<?php
   $arg = array('search'=>get_search_query());
   $categories = get_categories($arg);
?>
<form role="search"  method="get" class="hx_serch" action="<?php bloginfo('url'); ?>">
    <select name="cat">
        <?php     
        if(!empty($categories)){
        foreach( $categories as $category ){
      ?>
        <option name="cat" value="<?php echo $category->name?>"><?php echo $category->name?></option>
        <?php   }} ?>
    </select>
    <input type="search" class="hx_input_search" value="" name="s" />
    <input type="submit" class="search-submit" value="搜索" />
</form>

这里有一个注意事项,分类的select的下拉框name属性名要为cat,还有这里默认没有文章的分类是不会显示在下拉中的,如果想显示全部分类,参考获取wordpress所有分类。

13、获取最新文章

<?php
$args = array(
    'numberposts' => 10,  //获取的文章数量,此例中显示的是10篇文章
    'offset' => 0,  //从默认顺序里的第几篇文章开始获取,默认是0,就是从头开始,如果要从第二篇,就可以将此参数修改成为1,这个参数适用于文章分列,或者首篇文章不同于其他文章显示
    'category' => 0,   //分类的ID,多个用逗号将分类编号隔开,用来指定显示某个分类的文章
    'orderby' => 'post_date',  //排序规则,此例为按照时间顺序,默认也是时间顺序
    'order' => 'DESC',   //’ASC’升序,’DESC’ 降序
    'include' => , //获取要显示文章的ID,多个用顿号分开
    'exclude' => ,  //排除文章的ID,多个用顿号分开
    'meta_key' => ,  //自定义字段名称
    'meta_value' =>, //自定义字段的值
    'post_type' => 'post',   //获取的类型,比如文章或者页面
    'post_status' => 'draft, publish, future, pending, private', //文章的 mime 类型
    'suppress_filters' => true  
    );
?>

返回最近5篇文章
<h2>Recent Posts</h2>
<ul>
<?php
 $args = array( 'numberposts' => '5' );
 $recent_posts = wp_get_recent_posts( $args );
 foreach( $recent_posts as $recent ){
  echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
 }
?>
</ul>


14、调用指定文章

<?php
    // 获取文章ID编号为10的标题名称,返回字段关联数组数据格式
    $post_id = 100;
    $post = get_post($post_id, ARRAY_A); // 这样返回的值变成了数组形式
    $post_title = $post['post_title'];
    $post_date = $post['post_date'];
    $post_content = $post['post_content'];
?>


ID(文章ID编号)
post_author(文章作者编号)
post_date(文章发表的日期和时间[格式:年-月-日 时-分-秒])
post_data_gmt(文章发表的格林尼治标准时间[缩写:GMT,格式:年-月-日 时-分-秒])
post_content(文章内容)
post_title(文章标题)
post_category(文章分类编号[注:在2.1之后的版本值总为0。所以定义文章的分类可使用get_the_category()函数])
post_excerpt(文章摘要)
post_status(文章状态包括已发布,准备发布,草稿,私人文章等等)
comment_status(评论状态包括开启评论,关闭评论,只要注册用户可以评论)
ping_status(pingback/trackback状态[关闭或者开启])
post_password(文章密码)
post_name(文章的URL嵌套)
to_ping(要引用文章的URL链接)
pinged(引用过的文章链接)
post_modified(文章最后修改时间[格式:年-月-日 时-分-秒])
post_modified_gmt(文章最后修改格林尼治标准时间[缩写:GMT,格式:年-月-日 时-分-秒])
post_type(文章类型包括页面,文章,附件)
comment_count(评论总数)

其中图片可以通过这种方式获取:
<?php $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(25), 'full'); echo $full_image_url[0]; ?>

15、底部

 get_footer();

参考资料:

1、WordPress不同的分类_单页_文章页指定自定义不同的模板_wordpress 单页指定模板_zzsi的博客-CSDN博客

2、

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值