建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress网站开启文章形式及调用不同类型文章模板

GG网络技术分享 2025-03-18 16:16 0


WordPress网站开启文章形式:

//文章类型
add_theme_support( 'post-formats', array( 'aside','image'));

WordPress网站为不同文章形式的内容添加不同的single模板

//为不同文章形式的内容添加不同的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( 'image' )) {// 文章形式为image
$new_template = locate_template(array('shop-theme/single-shops.php' ));// 就调用single-shops.php模板
}
}
return (''!= $new_template) ? $new_template : $template;
}

调用显示指定类型的文章列表:

<?php
$posts = get_posts(array(
'numberposts' => '20',
'post_type' => 'post',
'tax_query'=>array(
array(
'taxonomy'=>'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
)
);
if($posts):
foreach($posts as $post):
?>
<li><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></li>
<?php
wp_reset_postdata();
endforeach;
endif;
?>

标签:

提交需求或反馈

Demand feedback