How To create shortcode in Wordpress
The shortcode API in wordpress allows you to create your own shortcodes by adding functions to your theme functions.php template (this is located at www.example.com/wp-content/themes/yourtheme/).
You can also create shortcode for plugin . it can be use in your wordpress page.
Basic Example Of Shortcode
}
add_shortcode( 'my_example', 'example_shortcode');
Creating a Shortcode With Attributes in wordpress
<? php $args = array( 'post_type' => post, 'posts_per_page' => 10 ) ;
$loop = new WP_Query( $args ) ;
$short_data = shortcode_atts( array(
'show_post_title' => 'true',
'show_post_image' => 'true'
), $atts);
ob_start( ); ?>
<? php
$loop->the_post( );
?>
<? php if( $show_image == "true") { ? >
<a href="<? php echo get_the_permalink( );?>">
<? php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img src="<? php echo $url;?>" title="<? php echo get_the_title( );?>" alt="<? php echo get_the_title( );?>">
</a>
<? php } ? >
<? php if( $show_post_title == "true") { ? >
<h4 class="news-widget-title"><a href="<? php the_permalink( );?>"><?php echo get_the_title( );?></a ></h4>
<? php } ? >
<? php }
add_shortcode( 'my_news', 'latest_news' ) ; ?>
0 komentar:
Post a Comment