WHY: Shortcodes enable you to use PHP in your posts and pages.
Shortcodes are very easy to learn, but you will not find such an easy to learn tutorial on the web such as this. (Sometimes I think they get paid by the word!)
Add this to your functions.php file
1 2 3 4 5 6 7 |
function manyposts($atts) { $postsperpage = get_option('posts_per_page'); return $postsperpage; } add_shortcode('showhowmany', 'manyposts'); |
And this in any page or post with the square brackets.
1 2 3 |
[showhowmany] |
DONE!
Explanation: –
There is a function called howmanyposts ($atts is not used just a default variable. Leave it!)
$postsperpage variable finds how many pages wordpress has and saves as a number.
Then it returns it when called.
The ‘addshortcode’ part says when you type [showhowmany], wordpress will run the function called manyposts.
Then all you need to do is type [showhowmany] and it will replace the shortcode with whatever result comes back from the function.
Was that so hard, after 30 other tutorials trying to explain it in depth…WHY….
Here’s a handy utility for generating shortcodes
To move a shortcode around with CSS: –
If you add this code to the bottom of your functions file
1 2 3 4 5 6 7 |
function manyposts($atts) { $postsperpage = get_option('posts_per_page'); return $postsperpage; } add_shortcode('showhowmany', 'manyposts'); |
Call it by using [showhowmany] in a wordpress page, it will show a number. to add CSS to this you will need to change the functions file. (as below)
1 2 3 4 5 6 7 8 |
function manyposts($atts) { $postsperpage = get_option('posts_per_page'); $withcss = "<div style='color:red;'>$postsperpage</div>"; return $withcss; } add_shortcode('showhowmany', 'manyposts'); |
You can add a class name instead of inline styling.
To make sure it appears where you want it to and not at the top, dont use echo (use return instead)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function makepod($atts) { $cat = "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>"; $args = array( 'numberposts' => '6' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ $cat.= '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>'; } $cat .= "</div>"; return $cat; } add_shortcode('showpod', 'makepod'); |
With added javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function manyposts($atts) { ?> <script> alert('test') </script> <?php $postsperpage = get_option('posts_per_page'); return $postsperpage; } add_shortcode('showhowmany', 'manyposts'); |
With added Jquery! (thank me later!)
Notice the dollar has been replaced with jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function manyposts($atts) { ?> <script> jQuery(document).on('click', '#mybutton', function() { alert('Hello'); }); </script> <?php $postsperpage = get_option('posts_per_page'); return $postsperpage; } add_shortcode('showhowmany', 'manyposts'); |