Chun
Chun

Reputation: 2270

Display a code in every posts/pages on wordpress

I've wrote this code to display a box with pageviews which is inside of the arrow button on the right side of every title, but the thing is, the counter is not counting pageviews on pages and I don't know why. I added this code in index.php

I tried to put my code for pageviews inside of <?php is_singular( $post_types ); ?> like this:

<?php is_singular(
    <div class="square">
    <div class="pageviews-icon"></div>
    <div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
</div>
); ?>

but is not working, the site crashed. Can anybody help me with this?

Here's the code above this as well, maybe it could help and I could wrap all together

<div class="frame">
                        <!-- uses the post format -->
                        <?php {
                            if(!get_post_format()) {
                               get_template_part('format', 'standard');
                            } else {
                               get_template_part('format', get_post_format());
                            };
                        ?>
                            <div class="square">
                                <div class="pageviews-icon"></div><div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
                            </div>
                    </div>

The website: The link for the website

Here's the pageview code too:

    function getPostViews($postID){
    $count_key = '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 Unique Views";
    }
    return $count.' Unique Views';
}
function setPostViews($postID) {
    $count_key = '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);
    }
}

Upvotes: 0

Views: 637

Answers (4)

Chun
Chun

Reputation: 2270

I've figured it out were the code were messing up thanks to @RobSchmuecker, I was looking for bugs on the wrong place until he tells me about other pages so then I looked into all this code to see his statement, and I discovered where the code were messing up which was on a very bottom of a code page where I set it up the php views counter which the code was:

<?php if(is_single ()) { ?>
    <?php setPostViews(get_the_ID()); ?>
<?php } ?>

instead of:

<?php if(is_singular ()) { ?>
    <?php setPostViews(get_the_ID()); ?>
<?php } ?>

With many many hours of digging in code and trying stuff, I completly forgot about this part of the code. Thanks to all for helping me on this

Upvotes: 1

Rob Schmuecker
Rob Schmuecker

Reputation: 8954

You're going to want to do this in the file where you are looping through all your posts. This can be in different locations depending on your theme etc .

For the twentyfourteen theme this is in index.php and looks like this

PHP

<div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

        <?php
            if ( have_posts() ) :
                // Start the Loop.
                while ( have_posts() ) : the_post();

                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme, then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */
                    get_template_part( 'content', get_post_format() );
..... code carries on

The last line here: get_template_part tells us that it's pulling in a template called content if we go and open content.php we can see that this is where it actually outputs things specific for each page/post. This is where you will need to do your logic. So for example, instead of this in twentyfourteen content.php

PHP:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php twentyfourteen_post_thumbnail(); ?>

    <header class="entry-header">
        <?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && twentyfourteen_categorized_blog() ) : ?>
        <div class="entry-meta">
            <span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfourteen' ) ); ?></span>
        </div>
        <?php
            endif;

            if ( is_single() ) :
                the_title( '<h1 class="entry-title">', '</h1>' );
            else :
                the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
            endif;
        ?>

        <div class="entry-meta">
            <?php
                if ( 'post' == get_post_type() )
                    twentyfourteen_posted_on();

                if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
            ?>
            <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
            <?php
                endif;

                edit_post_link( __( 'Edit', 'twentyfourteen' ), '<span class="edit-link">', '</span>' );
            ?>
        </div><!-- .entry-meta -->
    </header><!-- .entry-header -->

You would do something like this

PHP:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php twentyfourteen_post_thumbnail(); ?>

<header class="entry-header">
    <?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && twentyfourteen_categorized_blog() ) : ?>
    <div class="entry-meta">
        <span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfourteen' ) ); ?></span>
    </div>
    <?php
        endif;

        if ( is_single() ) :
            the_title( '<h1 class="entry-title">', '</h1>' );
        else :
            the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
        endif;
    ?>
        <div class="square">
        <div class="pageviews-icon"></div>
        <div class="pageviews"><?php echo getPostViews(the_ID()); ?></div>

    <div class="entry-meta">
        <?php
            if ( 'post' == get_post_type() )
                twentyfourteen_posted_on();

            if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
        ?>
        <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
        <?php
            endif;

            edit_post_link( __( 'Edit', 'twentyfourteen' ), '<span class="edit-link">', '</span>' );
        ?>
    </div><!-- .entry-meta -->
</header><!-- .entry-header -->

and then make sure that your getPostViews is accessible in your functions.php file.

This will make sure that your postViews are being displayed. But how to count them in the first place right?!

Go back to the content.php and have a look at this part of the code

PHP:

<?php if ( is_search() ) : ?>
    <div class="entry-summary">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
        <?php
            the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
            wp_link_pages( array(
                'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfourteen' ) . '</span>',
                'after'       => '</div>',
                'link_before' => '<span>',
                'link_after'  => '</span>',
            ) );
        ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

The is_search() function says whether to display Excerpts for Search, home, category and tag pages. However we don't want to set a pageview each time the post shows up in the search or home etc, so we must only put the setPostViews call in the other part of that if i.e. when the post/page is being viewed on its own.

That'll give you code like this

PHP:

<?php if ( is_search() ) : ?>
    <div class="entry-summary">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <!-- Now we can add a pageview -->
    <?php setPostViews(the_ID()); ?>
    <div class="entry-content">
        <?php
            the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
            wp_link_pages( array(
                'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfourteen' ) . '</span>',
                'after'       => '</div>',
                'link_before' => '<span>',
                'link_after'  => '</span>',
            ) );
        ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

Again making sure that setPostViews is accessible in your functions.php file.

This should get you what you want.

Upvotes: 1

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17954

As the documentation says:

This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.

You should do it like this:

<?php if is_singular() { ?>
    <div class="square">
        <div class="pageviews-icon"></div>
        <div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
    </div>
<?php }; ?>

Edit
You have to do some changes to your css:

div.frame{position:relative;}
div.square{display:block;}

Your view counter is functional. too see it live, please correct your php code as I mentioned above

live on your site :
enter image description here

Upvotes: 1

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

You cannot put HTML in PHP. You can put STRINGS in PHP, but they need to be put in quotes.

if(is_single() || is_page() || is_attachment()) {
    ?>
    <div class="square">
        <div class="pageviews-icon"></div>
        <div class="pageviews"><?php echo getPostViews(get_the_ID()); ?></div>
    </div>
    <?php
}

Upvotes: 1

Related Questions