Create Shortcode in WordPress

Create shortcode that can be used in posts or pages like this:

[newshortcode]

Add to functions.php

<?php 
function newshortcode_shortcode() { 
	if(is_admin()) return;
	$post_title = get_the_title();
	ob_start();
	?>
	<?php echo $post_title; ?>
	<div>Some text</div>
	<?php
	$output = ob_get_contents();
	ob_end_clean();
	return $output;
} 
add_shortcode('newshortcode', 'newshortcode_shortcode');
?>

Get attachment image from media library

wp_get_attachment_image function can accept four values as you can see:

wp_get_attachment_image( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

Examples

<?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), false, array( "class" => "img-fluid" ) ); ?>
<?php echo wp_get_attachment_image( 5151, 'medium', false, array( "class" => "img-fluid" ) ); ?>

Get Featured Image

Display the featured image of a post in a tag.

echo get_the_post_thumbnail();

Additionally if you want to grab a specific size for the featured image you can fill out the second argument with an image size. Also can add class onto image.

echo get_the_post_thumbnail( get_the_ID(), 'medium', array('class' => 'img-fluid') );

Return the URL of the featured post image.

echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );