Support

Account

Home Forums Pre-purchase Questions Post Views Counter

Solving

Post Views Counter

  • Hi,

    is it possible to create a custom field for page views?
    Everytime when a post is loaded the view counter should increase and will be saved in the custom field.

    best regards,
    Sascha

  • In the template file, get the field, increment it and then save it

    
    $count = get_field('counter_field);
    $count++;
    update_field('counter_field');
    
  • Thanks John, but your code didn’t work for me..

    Managed to find something that did work, thought I’d add it here in case anyone else comes looking..

    
    // Get the current value.
    $count = (int) get_field('counter_field');
    // Increase it.
    $count++;
    // Update with new value.
    update_field('counter_field', $count);
    
  • but in which php file do i insert this code?

  • In the single post template or the template that shows the single post for whatever post type you want to count.

  • I inserted in single.php but the counter is not working, see my prints, am I missing somewhere?

    Print
    contador
    contador

  • My version in Header.php

    <?php		  
    if(is_singular('post')){			  
    $count = get_field('views');			  
    $count++;			  
    update_field('views', number_format($count,0,",","."));
    }	
    ?>
    
  • Thank you for sharing. The website used to be another system. I want to manually modify the fields and increase the original views. Which table in the database, how to manually modify it, and also ask how to count the views on all post.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Post Views Counter’ is closed to new replies.