Support » Plugin: Post Views for Jetpack » How to use this code in post byline?

  • Resolved Appu Srva

    (@appu-srva)


    Hi Jeremy,

    The plugin is working like a charm, when I added the code in posts. However, I tried to use the code <?php echo do_shortcode(β€˜[jp_post_view]’); ?> in the entry meta section to get the views displayed in post byline. But it’s not working.

    Can you please tell me whether this is the right way or not?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    This should indeed work, you did the right thing by using echo do_shortcode('[jp_post_view]');!

    Could you paste the whole code used to display the byline in your theme, so I can take a closer look?

    Thanks!

    Hi Jeremy, Thank you for your quick response.

    Here is the code of education-hub/template-parts/content-single.php

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<header class="entry-header">
    		<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    
    		<div class="entry-meta">
    			<?php education_hub_posted_on(); ?>
                      
    		</div><!-- .entry-meta -->
    	</header><!-- .entry-header -->
    
    	<div class="entry-content">
        <?php
    	  do_action( 'education_hub_single_image' );
    	?>
    		<?php the_content(); ?>

    `

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Could you let me know where you had added the do_shortcode call?

    At first I added the code like this

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<header class="entry-header">
    		<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    
    		<div class="entry-meta">
    			<?php education_hub_posted_on(); ?>
    
    <?php echo do_shortcode(β€˜[jp_post_view]’); ?>
                      
    		</div><!-- .entry-meta -->
    	</header><!-- .entry-header -->
    
    	<div class="entry-content">
        <?php
    	  do_action( 'education_hub_single_image' );
    	?>
    		<?php the_content(); ?>

    After I added this code, the pages were loaded only till breadcrumb navigation. So, I tried like this

     <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<header class="entry-header">
    		<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    
    		<div class="entry-meta">
    			<?php education_hub_posted_on(); ?>
    
    echo do_shortcode('[jp_post_view]');
                      
    		</div><!-- .entry-meta -->
    	</header><!-- .entry-header -->
    
    	<div class="entry-content">
        <?php
    	  do_action( 'education_hub_single_image' );
    	?>
    		<?php the_content(); ?>

    This time, pages loaded well, but the output was displayed as just ‘echo do_shortcode(‘[jp_post_view]’);’

    I thought to add the code in template-tags.php, where the function for <?php education_hub_posted_on(); ?> is defined.

    The code there is like this

    if ( ! function_exists( 'education_hub_posted_on' ) ) :
    	/**
    	 * Prints HTML with meta information for the current post-date/time and author.
    	 */
    	function education_hub_posted_on() {
    		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    		if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    			$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    		}
    
    		$time_string = sprintf( $time_string,
    			esc_attr( get_the_date( 'c' ) ),
    			esc_html( get_the_date() ),
    			esc_attr( get_the_modified_date( 'c' ) ),
    			esc_html( get_the_modified_date() )
    		);
    
    		$posted_on = sprintf(
    			'%s',
    			'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
    		);
    
    		$byline = sprintf(
    			'%s',
    			'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
    		);
    
    		echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
    
    	}
    endif;

    But I have no idea how to add the code here.

    Thank you!

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    At first I added the code like this

    The problem in that first snippet is that you used apostrophes (β€˜) instead of single quotes ('). The correct snippet is:

    <?php echo do_shortcode('[jp_post_view]'); ?>

    This time, pages loaded well, but the output was displayed as just β€˜echo do_shortcode(β€˜[jp_post_view]’);’

    That’s because this is PHP code. You’ll consequently want to wrap it in between <?php ?> tags.

    Could you try again, with the snippet I posted above?

    <?php echo do_shortcode('[jp_post_view]'); ?>

    Oh my God! It’s working. Thank you so much. I’m new to coding, so I just copied the code from here – https://wordpress.org/support/topic/displaying-of-post-views-2/
    and pasted there.

    Thanks a ton Jeremy. And thank you for the awesome plugin. Good Day. πŸ™‚

    • This reply was modified 2 years, 11 months ago by Appu Srva.
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    I’m new to coding, so I just copied the code from here – https://wordpress.org/support/topic/displaying-of-post-views-2/
    and pasted there.

    Oh, that makes sense! Unfortunately the forums sometimes mess up code formatting a bit. Sorry about that!

    Oh, I will keep that in mind in further modifications. Thank you. πŸ™‚

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to use this code in post byline?’ is closed to new replies.