wordpress常用代码分享

jiathis.com获取‘分享代码’代码
-----------------------------------------------------------
//加分类页面跳转代码:(子分类也能这样获取)
  
    $cat_gg=get_category_by_slug('classic'); 
    $cat_links_gg=get_category_link($cat_gg->term_id);
?> 
    <a href="">查看更多>>a>

-----------------------------------------------------------
本地图片地址前面要加: bloginfo('template_directory'); ?>/
-----------------------------------------------------------
分页
<div class="container-fluid text-center">
    <div class="row">
        <div class="col-md-10 col-md-offset-1 col-xs-12 blue_list">
            <div class="col-md-12 col-xs-12 col-sm-12">
                 pagination($query_string); ?>
            div>
        div>
    div>
div>  
//此分页只对当前分类目录下的文章起作用
-----------------------------------------------------------

 query_posts('category_name=industry'); //取yewu所有置顶的文章?> 
         while (have_posts()) : the_post(); ?>
         if(is_sticky($id)==true) {?>
        <li><a href=""> the_title(); ?><span> the_time("Y-m-d"); ?>span>a>li>
         }?>
         endwhile;?> 
         wp_reset_query(); ?>  
特别注意:在当前分类下就不要用 query_posts('category_name=industry');否则分页不起作用
如果当前分类下有多个分类的文章列表,这些分类一定要是当前分类的子分类,不然分页无效
----------------------------------------------------------- 

 the_post_thumbnail(); ?>//特色图片
-----------------------------------------------------------

 query_posts('showposts=1&category_name=xxzyjs'); //获取学校专业介绍前1条数据?>    

-----------------------------------------------------------
//点击跳转首页:
<script>
    window.onload = function(){ 
        var obj = document.getElementById('ttt'); 
    obj.onclick=function(){   
        window.location.href="http://localhost/wordpress/";             
         } 
    }
script>
-----------------------------------------------------------

//普通文章列表页
     while(have_posts()) : the_post(); ?>
        <li>
        <a href="">
             the_title(); ?>
            <span style="display:block;float:right; text-align:right;"> the_time("Y-m-d"); ?>span>
        a>    
        li>
      endwhile;?>
-----------------------------------------------------------

//文章详细内容
 echo $post->post_content;?>

------------------------------------------------------------
//分类模板插件

/*
 * Template Name: XXX分类模版
 */
?>
------------------------------------------------------------
//取文章前多少字
 echo wp_trim_words(get_the_content(), 80,'...' ) ?>

-----------------------------------------------------------
//特色图片不响应:
//index.css写样式,把样式加到特色图片
.content-img img{
    width:100%;
    height:auto;
    display:block;
}

-----------------------------------------------------------
获取文章中第一张图片或视频
在function中写函数
//获取第一张图片
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];
  if(empty($first_img)){ //Defines a default image
    $first_img = "http://yourwebsite/default.jpg";
  }
  return $first_img;
}
或
function catch_the_image( $id ) {
  // global $post, $posts;
  $first_img = '';
  // 如果设置了缩略图
  $post_thumbnail_id = get_post_thumbnail_id( $id );
  if ( $post_thumbnail_id ) {
    $output = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
    $first_img = $output[0];
  }
  else { // 没有缩略图,查找文章中的第一幅图片
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    if(empty($first_img)){ // 既没有缩略图,文中也没有图,设置一幅默认的图片
      $first_img = "http://yourdomain.tld/images/default.jpg";
    }
  }
  return $first_img;
}//我用了无效

//获取第一个视频
function catch_that_video() {
  global $post, $posts;
  $first_video = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<embed.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_video = $matches [1] [0];

  return $first_video;
}
图片调用:<img src="" alt="" />
视频调用:<iframe style="height:250px;width:100%;" src="" >iframe>
-----------------------------------------------------------
//直系子分类
 $tax_terms = get_terms('category', array('parent' => $cat));?>
   foreach($tax_terms as $tax_term){ ?> 
    <h3> echo $tax_term->name;?>h3>//分类名称
    <p> echo $tax_term->description; ?>p>//分类描述

 } ?>
-----------------------------------------------------------
 $category = get_queried_object_id();?>//获取当前分类ID
 echo category_description($category); ?>//获取当前分类描述
-----------------------------------------------------------

 single_cat_title(); ?>//获取当前分类名   

-----------------------------------------------------------

//获取子分类下的子分类:
 $tax_terms = get_terms('category', array('parent' => $cat));?>//第一级子分类
   foreach($tax_terms as $tax_term){ ?> 
<div class="dropdown">
<dd class="dropbtn d-dd"><a style="color:black;" href="term_id); ?>"> echo $tax_term->name;?>a><span >>>span>dd>
    <div class="dropdown-content">
     $tax_cterms = get_terms('category', array('parent' => $tax_term->term_id));?>第二级子分类
       foreach($tax_cterms as $tax_cterm){ ?>    
    <a href="term_id);?>"> echo $tax_cterm->name;?>a>
     } ?>      

    div>
div>
 } ?>  

//千万注意:一定要该分类下有文章才会显示
-----------------------------------------------------------

//获取该页面分类下的直系子分类以及该分类下的文章名
 $tax_terms = get_terms('category', array('parent' => $cat));?>//直系子分类
   foreach($tax_terms as $tax_term){ ?> 
    <div class="dropdown">
        <dd class="dropbtn d-dd"><a style="color:black" href="term_id); ?>"> echo $tax_term->name;?>a><span >>>span>dd>
        <div class="dropdown-content">
         query_posts('cat='.$tax_term->term_id);?> //该分类下的文章    
         while (have_posts()) : the_post(); ?>
            <a href=""> the_title(); ?>a>
         endwhile;?> 
         wp_reset_query(); ?>      
        div>
    div>
 } ?>



-----------------------------------------------------------

//直系子分类列表
 $tax_terms = get_terms('category', array('parent' => $cat));?>
   foreach($tax_terms as $tax_term){ ?> 
<div class="row">
    <div class="col-md-12 col-xs-12 col-sm-12">
        <div class="row">
            <div class="col-md-5 col-xs-12 col-sm-12 content-img">
                <img src="term_id); ?>" />  //分类图片调用
            div>
            <div class="col-md-7 col-xs-12 col-sm-12 s-right">
                <h3> echo $tax_term->name;?>h3>
                    <p> echo $tax_term->description; ?>p>  //分类信息描述调用

                <button onclick="javascript:window.location.href='term_id); ?>'">点击了解button>
            div>
        div>
    div>
div>
<div class="row"> div>
 } ?>      

-----------------------------------------------------------
//文章页获取分类信息
 ob_start(); $cat_class=the_category_id();ob_clean(); //获取id值,the_category_id()会直接输出id,ob_start和clean为了清除此输出
$tax_terms = get_terms('category',array('parent' =>get_category_root_id($cat_class)));?>
   foreach($tax_terms as $tax_term){ ?> 
    <div class="dropdown">
        <dd class="dropbtn d-dd"><a style="color:black" href="term_id); ?>"> echo $tax_term->name;?>a><span >>>span>dd>
        <div class="dropdown-content">
             query_posts('cat='.$tax_term->term_id);?>     
             while (have_posts()) : the_post(); ?>
                <a href=""> the_title(); ?>a>
             endwhile;?> 
             wp_reset_query(); ?>      
        div>
    div>
 } ?>  

-----------------------------------------------------------
//子分类
$tax_term->term_id
//下一级子分类id
$tax_cterm->term_id    

-----------------------------------------------------------
//获取分类描述信息
 echo $tax_term->description; ?>
//获取分类图片
 echo z_taxonomy_image_url($tax_term->term_id); ?>    

-----------------------------------------------------------
//文章内容页加上一页和下一页
 previous_post_link("上一页: %link") ?>
 next_post_link("下一页: %link") ?>
$categories = get_the_category();
        $categoryIDS = array();
        foreach ($categories as $category) {
            array_push($categoryIDS, $category->term_id);
        }
        $categoryIDS = implode(",", $categoryIDS);
?>
 if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "已是最后文章";} ?>
 if (get_next_post($categoryIDS)) { next_post_link('下一篇: %link','%title',true);} else { echo "已是最新文章";} ?>

-----------------------------------------------------------

//文章页显示父分类名
 foreach((get_the_category()) as $category){echo $category->cat_name;}?>  
-----------------------------------------------------------

//wordpress调用不带超链接的Tag标签
function tagtext(){
    global $post;
    $gettags = get_the_tags($post->ID);
    if ($gettags) {
        foreach ($gettags as $tag) {
            $posttag[] = $tag->name;
        }
        $tags = implode( ',', $posttag );
        echo $tags;
    }
}

 tagtext();?>
----------------------------------------------------------- 

//获取文章指定自定义字段值
 $xzmeta = get_post_meta(get_the_ID(),'xzmeta',true);?>
-----------------------------------------------------------
//循环获取文章全部自定义字段和值
 $lianjie='#';
$custom_field_keys = get_post_custom_keys();
foreach ( $custom_field_keys as $key => $value ) {
$valuet = trim($value);
if ( '_' == $valuet{0} )
continue;
$f = get_post_meta($post->ID, $value, true);
if($value=="订购链接"){
$lianjie=$f;
}
if($value=="custom_post_template")
{
echo "";
}
else
{
echo "

".$value.":".$f."

"
; } } echo ""; ?>
----------------------------------------------------------- echo get_settings('home'); ?> //首页超链接 -----------------------------------------------------------

你可能感兴趣的:(wordpress)