个性化阅读
专注于IT技术分析

如何使用wp_get_theme修改singlepost.php

我如何使用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


这可能有效, 但尚未验证, 只是这个想法:

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);
   }
}
赞(0)
未经允许不得转载:srcmini » 如何使用wp_get_theme修改singlepost.php

评论 抢沙发

评论前必须登录!