我如何使用wp_get_theme修改singlepost.php
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function hs_set_post_views($postID) {
$count_key = 'hs_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}function hs_get_post_views($postID){
$count_key = 'hs_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}// Then add the following code inside your single post loop:
hs_get_post_views(get_the_ID());
hs_set_post_views(get_the_ID());
?>
我准备制作一个插件, 并且想要如何编辑singlepost.php以添加以下代码:
hs_set_post_views(get_the_ID());
#1【如何使用wp_get_theme修改singlepost.php】这可能有效, 但尚未验证, 只是这个想法:
add_action('loop_start', 'hs_set_post_views_loop');
function hs_set_post_views_loop($post_ID) {
if( is_single() ) {
hs_set_post_views($post_ID);
}
}
推荐阅读
- 我如何在定制器woocommerce部分下添加选项()
- 如何在storefront主题中将SVG用于徽标
- 如何在页面模板(WordPress)中使用获取请求
- 如何在帖子URL中使用自定义元字段代替CPT标题()
- 如何在WordPress 5.0中使用经典编辑器
- 如何使用子主题更新WordPress主题文件
- 如何使用更新查询更新WordPress中的表值()
- 将产品添加到购物车后如何更新微型购物车
- 神策数据(从技术视角看,如何更多更好更快地实施 A/B 试验)