G:首页调用“图片视频”的分类和文章(难点)

  

1:后台获取:自定义分类的ID (默认分类也可获取)

 2:动态获取“自定义分类的ID($cat)”

 $cat_title = single_cat_title(' ', false);                                      //获取分类名
$cats = get_term_by( 'name', $cat_title, 'cat_media' );    //通过分类名获取分类的对象
$cat= $cats->term_id;                                                                  //通过分类对象获得分类ID

声明:

name是固定参数,不变

cat_product是自定义分类

应用场景:在首页调用自定义分类(游戏截图)的内容

<?php

   $args = array(
        'post_type' => 'media', //自定义文章类型名称
        'showposts' => 9, //输出的文章数量,这个可以是缺省值,不用设置
        'tax_query' => array(
            array(
                'taxonomy' => 'cat_media',//自定义分类法名称
                'terms' =>10 //id为数字,也可是多个分类数组array(12,64)
                ),
            )
        );
 
    query_posts($args);  //无需这句,不然翻页失效
    while( have_posts() )  { 
        the_post();  
      $cat = get_the_category( get_the_ID() );  //本篇文章的分类数组
?>
     <li>
         <a href="<?php the_field("post_img",get_the_ID());?>" >
            <img src="<?php the_field("post_img",get_the_ID());?>"   width="249" height="146"/>
        </a>
     </li> 

<?php
    }
?>     

 应用场景:在首页按Tab显示各自定义分类的内容

 

<!--输出父类下的各自定义分类--->
<div class="hd">      
<ul class="list">
<?php
  
     $args=array(
       'hide_empty' => 0,
       'orderby'=>'ID',
       'taxonomy'=>'cat_media',
     );
  
     $categories = get_categories($args); 
     foreach ($categories as $cat) { 
?>
    <li>     
          <?php  echo $cat->cat_name;  ?>
          <a class="more" href="<?php echo get_category_link($cat->cat_ID) ?>" target="_blank" title="更多" >更多</a>   
    </li>
<?php } ?>
</ul>
</div>


<!--调用父分类下各子类的内容(自定义分类案例)--> <div class="bd clearfix"> <?php foreach ($categories as $cat) { ?> <ul class="list_pic"> <?php $cat_id = $cat->term_id; //根据“分类对象”获取分类的ID $args = array( 'post_type' => 'media', //自定义文章类型名称 'showposts' => 9, //输出的文章数量,这个可以是缺省值,不用设置 'tax_query' => array( array( 'taxonomy' => 'cat_media',//自定义分类法名称 'terms' =>$cat_id //id为64的分类。也可是多个分类array(12,64) ), ) ); query_posts($args); //本页不要这句,自定义分类才用 while( have_posts() ) { the_post(); //获取视频链接 $url = get_field("media_vedio",get_the_ID()); if(!$url){ //如果为空,赋值图片的地址 $url = get_field("media_img",get_the_ID()); } ?> <li> <a href="<?php echo $url; ?>" target="_blank"> <img src="<?php the_field("media_img",get_the_ID()) ?>" /> <i class="icon icon_play"></i> <em class="list_pic_bg"></em> </a> <span><?php the_title();?></span> </li> <?php } ?> </ul> <?php } ?> </div>
原文地址:https://www.cnblogs.com/roluce/p/6102334.html