私密笔记

  • 首页
  • 友情链接
  • 文章归档
  • 读者排行
  • 朋友圈
  • 订阅
  • https://simibiji.com/feed

WordPress:为不同文章形式选择不同的WordPress文章模板

  • captain
  • 2025-02-13
  • 0

在写文章的时候选择不同的文章形式,然后打开文章的时候会调用不同文章形式的模板。比如,文章形式为视频video ,就调用single-video.php模板,文章形式为突破image,就调用single-image.php,其它文章形式类似,可以添加多个文章样式。

//为不同文章形式的内容添加不同的single页面
add_action('template_include', 'load_single_template');
function load_single_template($template) {
  $new_template ='';
  // single post template
  if( is_single() ) {
    global $post;
    if ( has_post_format( 'video' )) {// 文章形式为video
      $new_template = locate_template(array('single-video.php' ));// 就调用single-video.php模板
    }
    if ( has_post_format( 'image' )) {// 文章形式为image
      $new_template = locate_template(array('single-image.php' ));// 就调用ssingle-image.php模板
    }
// 这里可以添加其他文章形式的模板
  }
  return (''!= $new_template) ? $new_template : $template;
}
将以上代码添加到functions.php文件中即可。

via:https://blog.csdn.net/jianzhanyes/article/details/130957712

© 2025 私密笔记
Theme by Wing
  • {{ item.name }}
  • {{ item.name }}