资讯文章类wordpress博客模板
时间 : 2023-12-26 10:45:02 声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性

最佳答案
以下是一个适用于资讯文章类WordPress博客的模板:
/*
Theme Name: 资讯文章类
Theme URI: http://example.com/
Author: 你的名字
Author URI: http://yourwebsite.com/
Description: 适用于资讯文章类的WordPress博客模板。
Version: 1.0
*/
// 注册并加载所需的样式表和脚本
function news_template_scripts() {
// 主题样式表
wp_enqueue_style( 'news-template-style', get_stylesheet_uri() );
// 自定义脚本文件
wp_enqueue_script( 'news-template-script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'news_template_scripts' );
// 定义文章布局
function news_template_content_layout() {
// 显示文章特色图像
if ( has_post_thumbnail() ) {
echo '<div class="featured-image">';
the_post_thumbnail();
echo '</div>';
}
// 显示文章标题
echo '<h2 class="entry-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
// 显示文章内容
echo '<div class="entry-content">';
the_excerpt(); // 或者使用 the_content() 函数
echo '</div>';
// 显示文章元数据
echo '<div class="entry-meta">';
echo '作者:' . get_the_author() . ' | ';
echo '发布于:' . get_the_date() . ' | ';
echo '分类:' . get_the_category_list(', ') . ' | ';
echo '标签:' . get_the_tag_list('', ', ');
echo '</div>';
}
// 自定义文章摘要长度
function news_template_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'news_template_excerpt_length' );
// 自定义摘要结尾省略符号
function news_template_excerpt_more( $more ) {
return '...';
}
add_filter( 'excerpt_more', 'news_template_excerpt_more' );
// 自定义文章模板
function news_template_custom_loop() {
// 自定义循环参数
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
);
// 开始循环
$custom_loop = new WP_Query( $args );
if ( $custom_loop->have_posts() ) {
while ( $custom_loop->have_posts() ) {
$custom_loop->the_post();
news_template_content_layout();
}
} else {
echo '没有文章可以显示。';
}
wp_reset_postdata();
}
// 创建自定义页面模板
function news_template_page_template( $templates ) {
$templates['news-template.php'] = '资讯文章模板';
return $templates;
}
add_filter( 'theme_page_templates', 'news_template_page_template' );
// 添加WordPress编辑器样式
function news_template_editor_styles() {
add_editor_style( 'editor-style.css' );
}
add_action( 'admin_init', 'news_template_editor_styles' );
// 添加侧边栏小工具
function news_template_widgets_init() {
register_sidebar( array(
'name' => '侧边栏',
'id' => 'sidebar-widget-area',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'news_template_widgets_init' );
其他答案
以下是一个适用于资讯文章类的WordPress博客模板的示例:
```php
<?php
/**
* Template Name: 资讯文章模板
* Description: 适用于资讯类文章的模板
*/
get_header(); // 载入顶部导航栏和头部布局
// 查询资讯文章类型的文章
$args = array(
'post_type' => 'post',
'category_name' => '资讯',
'posts_per_page' => 10
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('article'); ?>>
<header class="article-header">
<h2 class="article-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<div class="article-meta">
<span class="article-date"><?php the_date(); ?></span>
<span class="article-category"><?php the_category(', '); ?></span>
</div>
</header>
<div class="article-content">
<?php the_excerpt(); ?>
</div>
<footer class="article-footer">
<a href="<?php the_permalink(); ?>" class="read-more">阅读更多</a>
</footer>
</article>
<?php endwhile;
// 分页链接
the_posts_pagination(array(
'prev_text' => '«',
'next_text' => '»'
));
else :
echo '<p>暂无相关文章。
'; // 如果没有符合条件的文章则显示此提示endif;
wp_reset_postdata(); // 重置查询
get_footer(); // 载入底部布局和脚本
此模板包含一个自定义的页面模板,您可以在WordPress后台新建一个页面并选择该模板,然后保存并发布。在这个模板中,我们使用了一个查询来获取分类为“资讯”且每页显示10篇文章的文章列表。然后,我们逐篇输出文章的标题、发布日期、分类和摘要,并提供了一个阅读更多的链接。最后,我们展示了文章的分页链接。
您可以根据自己的需求对模板进行自定义调整,比如样式和布局。希望这个示例对您有所帮助!
上一篇
帝国cms修改首页方案前台无反应
https/SSL证书广告优选IDC>>
推荐主题模板更多>>
推荐文章







