Home / Admin / Post Shortcodes
Duplicate Snippet

Embed Snippet on Your Site

Post Shortcodes

estimated read time, breadcrumbs, related posts

Code Preview
php
<?php
// Est. Reading Time
$reading_speed = 200; // 200 words per minute
$content       = get_post_field( 'post_content', get_the_id() );
$word_count    = str_word_count( strip_tags( $content ) );
$reading_time  = ceil( $word_count / $reading_speed );
function post_read_time_shortcode($atts) {
echo '<p>Estimated reading time: ' . absint( $reading_time ) . ' ' . _n( 'minute', 'minutes', $reading_time ) . '</p>';
}
add_shortcode('read_time', 'post_read_time_shortcode');
// Breadcrumbs
global $post;
function breadcrumbs_shortcode {
	if ( ! is_home() ) {
		echo '<div class="breadcrumbs">';
		echo '<a href="' . esc_url( site_url() ) . '">Home</a> / ';
		if ( is_category() || is_single() ) {
			the_category( ' / ' );
			if ( is_single() ) {
				echo ' / ';
				the_title();
			}
		} elseif ( is_page() ) {
			if ( $post->post_parent ) {
				$anc        = get_post_ancestors( $post->ID );
				$page_title = get_the_title();
				foreach ( $anc as $ancestor ) {
					echo '<a href="' . esc_url( get_permalink( $ancestor ) ) . '" title="' . esc_attr( get_the_title( $ancestor ) ) . '">' . esc_html( get_the_title( $ancestor ) ) . '</a> / ';
				}
				echo esc_html( $page_title );
			} else {
				echo '<strong> ' . esc_html( get_the_title() ) . '</strong>';
			}
		}
		echo '</div>';
	}
}
add_shortcode('breadcrumbs', 'breadcrumbs_shortcode');
// Related Posts by Category
function related_posts_shortcode {
if ( ! empty( $post ) ) {
	$categories = get_the_category( $post->ID );
	if ( $categories ) {
		$category_ids = array();
		foreach ( $categories as $category ) {
			$category_ids[] = $category->term_id;
		}
		$query_args = array(
			'category__in'   => $category_ids,
			'post__not_in'   => array( $post->ID ),
			'posts_per_page' => 5
		);
		$related_posts = new WP_Query( $query_args );
		if ( $related_posts->have_posts() ) {
			echo '<div class="related-posts">';
			echo '<h3>Related Posts</h3><ul>';
			while ( $related_posts->have_posts() ) : $related_posts->the_post();
				echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
			endwhile;
			echo '</ul>';
			echo '</div>';
			wp_reset_postdata();
		}
	}
}}
add_shortcode('related_posts', 'related_posts_shortcode');
// Meta boxes 
add_action( 'add_meta_boxes', function () {
	if ( ! current_user_can( 'manage_options' ) ) {
		// Don't display the metabox to users who can't manage options
		return;
	}
	add_meta_box( 'wpcode-view-post-meta', 'Post Meta', function () {
		$custom_fields = get_post_meta( get_the_ID() );
		?>
		<table style="width: 100%; text-align: left;">
			<thead>
			<tr>
				<th style="width: 28%">Meta Key</th>
				<th style="width: 70%">Value</th>
			</tr>
			</thead>
			<tbody>
			<?php foreach ( $custom_fields as $key => $values ) {
				?>
				<?php foreach ( $values as $value ) { ?>
					<tr>
						<td><?php echo esc_html( $key ); ?></td>
						<td><code><?php echo esc_html( $value ); ?></code></td>
					</tr>
				<?php } ?>
			<?php } ?>
			</tbody>
		</table>
		<?php
	}, get_post_type() );
} );

Comments

Add a Comment