admin管理员组文章数量:1316008
I really need some help, I dont know where else to turn with this request. I'd like to add a shortcode to one of my posts. The template tags get_next_post() and get_previous will not work for me since I dont want post navigation on each post page and I want to control where it displays. In a DIV in an HTML structure I've created in a post.
So what I need is to generate shortcodes from get_next_post() and get_previous_post()
I really need some help, I dont know where else to turn with this request. I'd like to add a shortcode to one of my posts. The template tags get_next_post() and get_previous will not work for me since I dont want post navigation on each post page and I want to control where it displays. In a DIV in an HTML structure I've created in a post.
So what I need is to generate shortcodes from get_next_post() and get_previous_post()
Share Improve this question asked Mar 24, 2011 at 8:28 brettbrett 971 gold badge4 silver badges8 bronze badges2 Answers
Reset to default 4this is very simple to do...
// next
function next_shortcode($atts) {
// global $post; -unnecessary
return '<div class="nav-next">'.next_post_link( '%link', '%title <span class="meta-nav">' . _x( '', 'Next post link', ' ' ) . '</span>',true ).'</div>';
}
add_shortcode( 'next', 'next_shortcode' );
//prev
function prev_shortcode($atts) {
//global $post; -unnecessary
return '<div class="nav-previous">'.next_post_link( '%link', '%title <span class="meta-nav">' . _x( '', 'Previous post link', ' ' ) . '</span>',true ).'</div>';
}
add_shortcode( 'prev', 'prev_shortcode' );
Goodluck! Here for any question....
There were a couple issues with the above code. You need to wrap shortcode output with ob_start() in order to get it to echo to the screen in the right place. Also, as one of the commenters above mentioned, those functions already echo their result so putting them in a return statement won't work correctly.
Here is my fix:
add_shortcode( 'prev', 'prev_shortcode' );
add_shortcode( 'next', 'next_shortcode' );
function next_shortcode($atts) {
global $post;
ob_start();
next_post_link( '<div class="nav-next">%link</div>', 'Next post link' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
function prev_shortcode($atts) {
global $post;
ob_start();
previous_post_link( '<div class="nav-previous">%link</div>', 'Previous post link' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
本文标签: template tagsMaking a Shortcode NEXT and PREVIOUS to place into specific posts for post navigation
版权声明:本文标题:template tags - Making a Shortcode [NEXT] and [PREVIOUS] to place into specific posts for post navigation 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741984748a2408607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论