WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Popular Posts
[resolved] How to customize this shortcode which shows the number of views of a single post (5 posts)

  1. Vahid
    Member
    Posted 9 months ago #

    I've defined a shortcode in my functions.php like the following which shows the number of views of the current post by inserting [mwpp] in post editor.

    function mywpp() {
    	return wpp_get_views(get_the_ID(), 'all');
    }
    add_shortcode('mwpp', 'mywpp');

    1- Now I want to have something like [mwpp id="71" range="X"] in post/page editor whick X is one of the daily, weekly, monthly, or all. How can I accomplish this?
    2- How can I put the counter above in a div class like the below?

    .wppcounter{
        color:#fff;
        background:#ca333e;
    }

    http://wordpress.org/plugins/wordpress-popular-posts/

  2. Héctor Cabrera
    Member
    Plugin Author

    Posted 8 months ago #

    Ah, that's clever. You're almost there:

    function mywpp( $atts = NULL ) {
    	extract( shortcode_atts( array(
    		'id' => NULL,
    		'range' => 'all'
    	), $atts ) );
    
    	$pID = NULL;
    
    	if ( $id )
    		$pID = $id;
    	else
    		$pID = get_the_ID();
    
    	return '<div class="wppcounter">' . wpp_get_views($pID, $range) . '</div>';
    }
    add_shortcode('mwpp', 'mywpp');

    I haven't tested this, but it should work.

    Let me know how it goes, alright?

  3. Vahid
    Member
    Posted 8 months ago #

    Héctor, where were you? I missed you:-)
    Thanks so much for your answer, but I have one problem with it:
    When I put something like "This post has been viewd [mwpp id="71" range="X"] times." in post/page editor, I get the following output:

    This post has been viewed
    X
    times.

    In other words, the counter is displayed in a new line. To fix this, I put
    display:inline!important; in the wppcounter class, but it didn't work. could you please help me solve this problem?
    By the way, the default value that you have specified for the range, is all, right?

  4. Héctor Cabrera
    Member
    Plugin Author

    Posted 8 months ago #

    Been too busy with work projects. At my job, we were short of personnel for a bit too much time and many projects got delayed - hence the load of work got too big and you know how that ends. I still have a lot to do (and will get even more work in the upcoming days) but decided to take this weekend to work a little bit on the plugin.

    Anyways, do you really need to use a DIV element in there? How about this:

    function mywpp( $atts = NULL ) {
    	extract( shortcode_atts( array(
    		'id' => NULL,
    		'range' => 'all'
    	), $atts ) );
    
    	$pID = NULL;
    
    	if ( $id )
    		$pID = $id;
    	else
    		$pID = get_the_ID();
    
    	return '<span class="wppcounter">' . wpp_get_views($pID, $range) . '</span>';
    }
    add_shortcode('mwpp', 'mywpp');

    By the way, the default value that you have specified for the range, is all, right?

    Yes, that's correct. Also the post ID will default to the current post/page ID as well if you don't pass it to the shortcode, so you could just use [mwpp].

  5. Vahid
    Member
    Posted 8 months ago #

    Thanks again for your reply. Everything is ok now so I'm marking the topic as resolved.
    By the way, I wish you success in your job and of course, your life.

Reply

You must log in to post.

About this Plugin

About this Topic