阿里云服务器

我们在做wordpress网站的时候,常常会新增不同的文章类型来拓展网站的功能。

今天给大家分享如何调用自定义文章类型的相关文章。

只需要在自定义文章类型的文章页调用下面代码就可以实现功能:

<?php
// 获取自定义文章类型的分类项目
$custom_taxterms = wp_get_object_terms( $post->ID,'product-category', array('fields' => 'ids') );
// 参数
$args = array(
'post_type' => 'product',// 文章类型
'post_status' => 'publish',
'posts_per_page' => 12, // 文章数量
'orderby' => 'rand', // 随机排序
'tax_query' => array(
    array(
        'taxonomy' => 'product-category', // 分类法
        'field' => 'id',
        'terms' => $custom_taxterms
    )
),
'post__not_in' => array ($post->ID), // 排除当前文章
);
$related_items = new WP_Query( $args );
// 查询循环
if ($related_items->have_posts()) :
    echo '';
    while ( $related_items->have_posts() ) : $related_items->the_post();
					$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

?>
				
<li class="amazingcarousel-item">
<div class="amazingcarousel-item-container">
<div class="amazingcarousel-image">
<a href="<?php  the_permalink();?>" title="Multi Station"  class="" ><img src="<?php echo $url ;?>"  alt="Multi Station"  width="240" height="180"/></a></div>
<div class="amazingcarousel-title"><a rel="external" href="<?php  the_permalink();?>"><?php the_title(); ?></a></div></div>
</li>
				
				<?php
    endwhile;
    echo '</ul>';
endif;
// 重置文章数据
wp_reset_postdata();
?>			

只需要替换上面的文章类型和分类法即可,调用数目自己修改。

显示效果如下:

related.jpg

相关阅读:
  • wordpress编辑框不让span过滤的方法
  • wordpress搜索为空时仍然返回结果问题
  • wordpress注册页取消邮箱的方法
  • wordpress用户头像在线调用代码
  • wordpress自动隐藏email注册,直接用户名密码注册
  • wordpress设置游客只能访问指定页面的方法
  • wordpress列表分页显示404页面的解决办法
  • wordpress页面默认排序修改的方法
  • wordpress新版本5.4 “Adderley” 发布上线,更新多项功能
  • wordpress默认编辑器功能增加的方法