Last active
August 29, 2015 14:14
-
-
Save Aziz-Rahman/9ca01878387ba438f81a to your computer and use it in GitHub Desktop.
Sortcodes in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//shortcodes latest post | |
function latest_post( $atts ) { | |
extract( shortcode_atts( | |
array( | |
'limit' => '5', | |
'cat_id' => '', | |
), $atts ) ); | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page'=> $limit, | |
'cat' => $cat_id, | |
'post_status' => 'publish' | |
); | |
$wp_query = new WP_Query(); | |
$wp_query->query( $args ); | |
if ( $wp_query->have_posts() ) : | |
while( $wp_query->have_posts() ) : | |
$wp_query->the_post(); | |
echo '<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">'.the_title().'</a>'; | |
echo '<br>'; | |
endwhile; | |
endif; | |
} | |
add_shortcode( 'latestpost', 'latest_post' ); | |
?> | |
<?php | |
//ket : | |
//Masukan code diatas pada file functions.php atau bisa juga buat file baru kemudian di include di file functions.php | |
//Cara pemangilan, masukan kode [latestpost limit="5"] pada text editor wordpress ! maka akan menampilkan 5 artikel terbaru. | |
//limit diambil dari parameter shortcode_atts 'limit' | |
//Enjoy ! | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function button( $atts ) { | |
$atts = shortcode_atts( array( | |
'type' => 'button', | |
'href' => '', | |
'class' => 'button', | |
'target' => '_blank' | |
), $atts ); | |
if( $atts['href'] == true ) { | |
return '<a href="'.$atts['href'].'" class="'.$atts['class'].'" target="'.$atts['target'].'">Button !</a>'; | |
} else { | |
return '<a href="#" class="button">Button</a>'; | |
} | |
} | |
add_shortcode( 'button', 'button' ); | |
?> | |
<?php | |
//ket : | |
//Masukan code diatas pada file functions.php atau bisa juga buat file baru kemudian di include di file functions.php | |
//Cara pemangilan, masukan kode [button href="Link tujuan"] pada text editor wordpress ! | |
//Enjoy ! | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment