WordPress single.php 文章模板开发详解

single.php是wordpress的文章模板,那么我们应该怎么开发呢?实现什么内容呢?该怎么实现呢?
一.文章内容的实现
我是标题:<?php the_title();?>

我是日期:<?php the_date();?>

我是内容:<?php the_content();?>

<?php

the_post_navigation( array(

'prev_text' => '← 上一篇:%title',

'next_text' => '下一篇:%title →',

) );

?>


二.阅读量的实现,页面刷新就加一

三.侧边栏的实现
functions.php里面加载下面的代码
function theme_register_sidebar() {

register_sidebar( array(

'name'          => '通用侧边栏',

'id'            => 'sidebar-main',

'description'   => '文章、页面侧边栏',

'before_widget' => '<div class="widget">',

'after_widget'  => '</div>',

'before_title'  => '<h3 class="widget-title">',

'after_title'   => '</h3>',

));

}

add_action( 'widgets_init', 'theme_register_sidebar' );
single.php加载
/*侧边栏从后台设置*/

if ( is_active_sidebar( 'sidebar-main' ) ) :

?>

<aside class="sidebar">

<?php dynamic_sidebar( 'sidebar-main' ); ?>

</aside>

<?php

endif;
后台设置


四.点赞和收藏
点赞一般是再文章底部添加点赞按钮。功能实现分为登录后点赞和不登录点赞。
1.登录后点赞,一般会记录点赞人id,和取消点赞的功能。点赞信息存放再postmeta表里面(收藏同理)

if (!is_user_logged_in()) {

echo json_encode(['status' => 0, 'msg' => '请先登录']);

exit;

}

$user_id = get_current_user_id();

$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;

if (!$post_id) {

echo json_encode(['status' => 0, 'msg' => '文章ID不能为空']);

exit;

}

// 每个用户唯一 KEY(非常关键)

$meta_key = "like_user_{$user_id}";

// 判断是否已经点赞

$has_like = get_post_meta($post_id, $meta_key, true);

if ($has_like) {

// 已经点赞 → 取消点赞(删除记录)

delete_post_meta($post_id, $meta_key);

} else {

// 未点赞 → 添加点赞(新增一条记录)

update_post_meta($post_id, $meta_key, 1);

}


2.不登录点赞,只需要记录一个点赞的总数据,可以存在options表,本地使用缓存记录今天是否已点赞(收藏同理,不过本地缓存时间会更长例如:30天)

// 1. 获取原有点赞总数

$like_total = get_option('site_post_like_total', 0);

$like_total = (int)$like_total;

// 2. 点赞数 +1

$like_total++;

// 3. 保存到 options 表

update_option('site_post_like_total', $like_total);


五.评论的实现

这个是wordpress最基础的文章评论

if (comments_open() || get_comments_number()) :

comments_template();

endif;
六.推荐文章

1.获取置顶文章,优点后台可以设置,缺点所有文章的推荐千篇一律。可优化的方式是给出多个推荐块(例如:最新文章、置顶文章、热门文章),比较适合网站流量比较的,发布文章频率高的网站。
// 获取置顶文章 ID

$sticky_ids = get_option('sticky_posts');

// 如果有置顶文章,才查询

if (!empty($sticky_ids)) {

$sticky_posts = get_posts([

'post__in'      => $sticky_ids,  // 只查置顶ID

'post_type'     => 'post',      // 文章类型

'posts_per_page'=> -1,          // 显示所有置顶

'orderby'       => 'post__in',  // 按后台置顶顺序排序

'post_status'   => 'publish'

]);

}

2.随机文章,优点是每次推荐都不一样,用户看到的有新鲜感,但是文章关联行不强。
$random_posts = get_posts([

'post_type'      => 'post',

'post_status'    => 'publish',

'posts_per_page' => 6,

'orderby'        => 'rand', // 随机核心

'no_found_rows'  => true,  // 优化性能

]);

3.发布文章的时候自定义关联性,文章关联性强,缺点是变化性不足,例如看到一篇文章推荐的都是同一类型的文章。
function custom_related_posts_meta_box()

{

add_meta_box(

'related_posts_box',

'自定义关联文章',

'related_posts_box_html',

'post',

'normal',

'default'

);

}

add_action('add_meta_boxes', 'custom_related_posts_meta_box');

function related_posts_box_html($post)

{

$related_ids = get_post_meta($post->ID, 'custom_related_posts', true);

wp_nonce_field('related_posts_nonce', 'related_posts_nonce_key');

?>

<p>

<label>填写关联文章ID,多个用英文逗号分隔:</label>

<input type="text" name="custom_related_ids" value="<?php echo esc_attr($related_ids); ?>" style="width:100%;">

<br><small>示例:12,25,36</small>

</p>

<?php

}

七.自定义功能

1.文章页面的广告,博客站的基础,任何网站如果没有盈利,那么就很难长久,所以文章页可以添加广告。一般插件实现即可
2.推荐产品,如果是企业站,这里可以展示几个关联的产品。
3.作者的介绍,如果纯博客站来提高作者的知名度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值