wordpress常用代码分享

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

-----------------------------------------------------------
本地图片地址前面要加:<?php 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">
                <?php pagination($query_string); ?>
            </div>
        </div>
    </div>
</div>  
//此分页只对当前分类目录下的文章起作用
-----------------------------------------------------------

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

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

<?php 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>
-----------------------------------------------------------

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

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

------------------------------------------------------------
//分类模板插件
<?php
/*
 * Template Name: XXX分类模版
 */
?>
------------------------------------------------------------
//取文章前多少字
<?php 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="<?php echo catch_that_image() ?>" alt="" />
视频调用:<iframe style="height:250px;width:100%;" src="<?php echo catch_that_video() ?>" ></iframe>
-----------------------------------------------------------
//直系子分类
<?php $tax_terms = get_terms('category', array('parent' => $cat));?>
<?php   foreach($tax_terms as $tax_term){ ?> 
    <h3><?php echo $tax_term->name;?></h3>//分类名称
    <p><?php echo $tax_term->description; ?></p>//分类描述

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

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

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

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

    </div>
</div>
<?php } ?>  

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

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



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

//直系子分类列表
<?php $tax_terms = get_terms('category', array('parent' => $cat));?>
<?php   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="<?php echo z_taxonomy_image_url($tax_term->term_id); ?>" />  //分类图片调用
            </div>
            <div class="col-md-7 col-xs-12 col-sm-12 s-right">
                <h3><?php echo $tax_term->name;?></h3>
                    <p><?php echo $tax_term->description; ?></p>  //分类信息描述调用

                <button onclick="javascript:window.location.href='<?php echo get_category_link($tax_term->term_id); ?>'">点击了解</button>
            </div>
        </div>
    </div>
</div>
<div class="row">&nbsp;</div>
<?php } ?>      

-----------------------------------------------------------
//文章页获取分类信息
<?php 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)));?>
<?php   foreach($tax_terms as $tax_term){ ?> 
    <div class="dropdown">
        <dd class="dropbtn d-dd"><a style="color:black" href="<?php echo get_category_link($tax_term->term_id); ?>"><?php echo $tax_term->name;?></a><span >>></span></dd>
        <div class="dropdown-content">
            <?php query_posts('cat='.$tax_term->term_id);?>     
            <?php while (have_posts()) : the_post(); ?>
                <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
            <?php endwhile;?> 
            <?php wp_reset_query(); ?>      
        </div>
    </div>
<?php } ?>  

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

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

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

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

//文章页显示父分类名
<?php 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;
    }
}

<?php tagtext();?>
----------------------------------------------------------- 

//获取文章指定自定义字段值
<?php $xzmeta = get_post_meta(get_the_ID(),'xzmeta',true);?>
-----------------------------------------------------------
//循环获取文章全部自定义字段和值
<?php $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 "<p>".$value.":".$f."</p>";
}
}
echo "<div id='".lianjie."'><a href='".$lianjie."'>订购链接</a></div>";
?>
-----------------------------------------------------------
<?php echo get_settings('home'); ?>
//首页超链接
-----------------------------------------------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值