Add View count on stories #12
@ -167,6 +167,19 @@ function fictioneer_fcn_story_post_type() {
|
||||
register_post_type( 'fcn_story', $args );
|
||||
}
|
||||
add_action( 'init', 'fictioneer_fcn_story_post_type', 0 );
|
||||
function set_post_views($post_id) {
|
||||
$key = 'post_views';
|
||||
$count = get_post_meta($post_id, $key, true);
|
||||
$count = (empty($count)) ? 1 : $count + 1;
|
||||
update_post_meta($post_id, $key, $count);
|
||||
}
|
||||
|
||||
function track_post_views() {
|
||||
if (is_single() && get_post_type() == 'fcn_story') {
|
||||
set_post_views(get_the_ID());
|
||||
}
|
||||
}
|
||||
add_action('wp', 'track_post_views');
|
||||
|
||||
// =============================================================================
|
||||
// CUSTOM POST TYPE: FCN_CHAPTER
|
||||
|
@ -4051,3 +4051,4 @@ function fictioneer_save_recommendation_metaboxes( $post_id ) {
|
||||
add_action( 'save_post', 'fictioneer_save_recommendation_metaboxes' );
|
||||
|
||||
?>
|
||||
|
||||
|
||||
|
@ -61,12 +61,21 @@ if ( ! $thumbnail_shown ) {
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="story__identity">
|
||||
<h1 class="story__identity-title"><?php echo $story['title']; ?></h1>
|
||||
<h1 class="story__identity-title"><?php echo $story['title'];
|
||||
|
||||
|
||||
?></h1> <?php
|
||||
$views = get_post_views(get_the_ID());
|
||||
echo '<i class="fas fa-eye" style="font-size: 14px;"> ' . $views . '</i>';
|
||||
|
||||
?>
|
||||
|
||||
<div class="story__identity-meta"><?php
|
||||
![]() What happened with the formatting here? What happened with the formatting here?
|
||||
printf(
|
||||
_x( 'by %s', 'Story page: by {Author(s)}', 'fictioneer' ),
|
||||
fictioneer_get_story_author_nodes( $story_id )
|
||||
);
|
||||
|
||||
?></div>
|
||||
</div>
|
||||
|
||||
|
@ -25,6 +25,13 @@ if ( get_post_meta( get_the_ID(), 'fictioneer_story_hidden', true ) ) {
|
||||
|
||||
get_header( null, $header_args );
|
||||
|
||||
// Update the Display of View Count
|
||||
function get_post_views($post_id) {
|
||||
$key = 'post_views';
|
||||
$count = get_post_meta($post_id, $key, true);
|
||||
return (empty($count)) ? 0 : $count;
|
||||
}
|
||||
|
||||
![]() This should really not be in the template or partial. Also, defining the key if it is immediately used and only once is a bit of a waste. The key 'post_views' is also not save, it can be manipulated in the editor under certain circumstances. You should prefix internal meta keys with "fictioneer_" or just "_" if you are confident they will not conflict (which I wouldn't be). This should really not be in the template or partial. Also, defining the key if it is immediately used and only once is a bit of a waste. The key 'post_views' is also not save, it can be manipulated in the editor under certain circumstances. You should prefix internal meta keys with "fictioneer_" or just "_" if you are confident they will not conflict (which I wouldn't be).
|
||||
?>
|
||||
|
||||
<main id="main" class="main story">
|
||||
|
Loading…
x
Reference in New Issue
Block a user
What the... why are my newlines at the end of file not committed? Gotta check this.