Site logo

Archived topic

Adding shortcode to php

13 replies · Started by Carlo on February 8, 2020

Viewing posts 1–14 of 14

I'd like to add the following shortcode to the below snippet code but not quite sure how to achieve this:
[editorskit display="wordcount" before="Reading Time: " after=" min"]

Im currently using this php snippet code to display metadata under my headings

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );

add_filter( 'generate_post_date_output', function( $date, $time_string ) {
printf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
esc_html( get_the_author() ),
get_avatar( get_the_author_meta( 'ID' ) )
)
);

if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
echo '<span class="comments-link">';
comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
echo '</span>';
}

printf( '<span class="posted-on">%s</span>',
$time_string
);
}, 10, 2 );

and styled with the following CSS:

.byline img {
    width: 25px;
    height: 25px;
    border-radius: 50%;
    position: relative;
    vertical-align: middle;
    margin: 0 10px 0 0;
}

.byline,
.comments-link,
.posted-on {
	display: inline-block;
}

.comments-link,
.posted-on {
	border-left: 1px solid #ddd;
	padding-left: 10px;
	margin-left: 10px;
}

.comments-link:before {
	display: none;
}

h2.entry-title {
    margin-bottom: 20px;
}

thanks Leo, yeah i did some searching and did see that code mentioned before, my problem is im not sure how to incorporate it into the code im using above. I keep getting errors when i add it so i guess im adding it in the wrong place?

<?php echo do_shortcode("[Shortcode]"); ?>

Just some context in what the layout im trying to achieve:

HEADING
(author image) | author name | publish date | (shortcode = reading time)

anyway to add on the shortcode to the end of the entry meta?

Try this:

echo do_shortcode( '[editorskit display="wordcount" before="Reading Time: " after=" min"]' );

Let me know :)

Got it working, thanks Tom.

just one more thing, ive been playing around with the CSS to get them to stack nicely under each other in mobile view but just doesn't look right.

Im trying to achieve the following layout in mobile. (with the solid border removed on .posted-on class. any quick tips here?

(author image) & author name
publish date
[shortcode = reading time]

Any chance you can link me to the page so I can inspect the CSS/HTML?

Hi Tom, I updated the original post to include a link to the post.
i've added it to a test site until i work it out so i dont mess up my working site.

also, ive added the "reading time shortcode" towards the bottom of this code, is this the right place to add it?

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );

add_filter( 'generate_post_date_output', function( $date, $time_string ) {
printf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
esc_html( get_the_author() ),
get_avatar( get_the_author_meta( 'ID' ) )
)
);

if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
echo '<span class="comments-link">';
comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
echo '</span>';
}

printf( '<span class="posted-on">%s</span>',
$time_string
);
echo do_shortcode( '[editorskit display="wordcount" before="Reading Time: " after=" min"]' );
}, 10, 2 );

Hi there,

try this CSS for your mobile arrangement:

@media (max-width: 768px) {
    .entry-meta .posted-on,
    .editorskit-shortcode {
        display: block;
        border-left: 0;
        padding-left: 0;
        margin-left: 0;
    }
}

And where you added the Shortcode looks fine.

Thanks David, worked like a charm

You're welcome

Hi, sorry to break into this thread but I have the same problem.
I can't get the syntax right.
As of now, the second shortcode gets added into the first list item.

What am I doing wrong?


add_filter( 'generate_post_author_output', 'add_to_end_of_meta' );
function add_to_end_of_meta( $output ) {
  
  echo $output . ' <ul class="reads-shares-meta">
	             <li class="post-reads-info">' . do_shortcode('[post-views]'); '</li>
                     <li class="post-shares-info">' . do_shortcode('[social_warfare buttons="totals"]'); '</li>
                   </ul> '; 
}

And as always.... minutes after posting a question I find a working solution lol


add_filter( 'generate_post_author_output', 'add_to_end_of_meta' );
function add_to_end_of_meta( $output ) {
  
  echo $output . ' <ul class="reads-shares-meta">
	                 <li class="post-reads-info">' . do_shortcode("[post-views]") . '</li>
                     <li class="post-shares-info">' . do_shortcode('[social_warfare buttons="totals"]') . '</li>
                   </ul> '; 
}

Glad you got it working! :)

This archived topic is closed to new replies.