WordPress:在短代码中使用模板标签和函数
我试图找出为什么我的短代码不起作用 - 谁能明白为什么(对蹩脚的格式表示歉意,似乎无法让这个短代码正确显示)?
由于某种原因,该代码在我的短代码之上运行。不确定我完全理解如何在短代码中使用模板标签/WP 函数,因为我相信您需要在函数开头使用类似 get_ 的内容,以便将其返回到短代码中的变量中。有人可以帮忙吗?
谢谢奥苏
/* News from Blog category only - category 3 */
add_shortcode( 'latestblogposts', 'osu_latestblogposts' );
function osu_latestblogposts() {
$start = '<div class="widget widget_osu_blog">';
$start .= '<a title="Subscribe to our RSS feed" href="/feed?cat=3" class="rss"><img alt="RSS" src="' . get_bloginfo('template_directory') . '/images/ico-rss-big.png"></a>';
$start .= '<div>';
$my_query = new WP_Query('category=3&showposts=3');
while ($my_query->have_posts()) : $my_query->the_post();
$inner = '<div class="item"><a href="' . get_permalink() . '" title="';
$inner .= printf( esc_attr__( 'Permalink to %s', 'inspire' ), the_title_attribute( 'echo=0' ) );
$inner .= '" rel="bookmark" class="title">' . the_title() . '</a>';
$inner .= '<p class="post-meta">';
$inner .= '<span class="small">by</span> <span class="post-author"><a title="Posts by ';
$inner .= the_author();
$inner .= '" href="' . the_author_posts_link() . '">' . the_author() . '</a></span>';
$inner .= '<span class="small">on</span> <span class="post-date">';
$inner .= get_the_date('d/m/Y') . '</span></p>';
$inner .= the_excerpt() . '</div> <!-- End div.item -->';
endwhile;
$end = '</div>';
$end .= '</div> <!-- End div.widget_osu_blog -->';
$latestblogposts = $start . $inner . $end;
return $latestblogposts;
}
I'm trying to figure out why my shortcode isn't working - can anyone see why (apologies for crappy formatting, can't seem to get this shortcode to display properly)?
The code is running above my shortcode for some reason. Not sure I completely understand how to use template tags /WP functions in shortcodes as I believe you need to use something like get_ at the beginning of the function in order to return it in a variable within shortcodes. Can anyone help?
Thanks
osu
/* News from Blog category only - category 3 */
add_shortcode( 'latestblogposts', 'osu_latestblogposts' );
function osu_latestblogposts() {
$start = '<div class="widget widget_osu_blog">';
$start .= '<a title="Subscribe to our RSS feed" href="/feed?cat=3" class="rss"><img alt="RSS" src="' . get_bloginfo('template_directory') . '/images/ico-rss-big.png"></a>';
$start .= '<div>';
$my_query = new WP_Query('category=3&showposts=3');
while ($my_query->have_posts()) : $my_query->the_post();
$inner = '<div class="item"><a href="' . get_permalink() . '" title="';
$inner .= printf( esc_attr__( 'Permalink to %s', 'inspire' ), the_title_attribute( 'echo=0' ) );
$inner .= '" rel="bookmark" class="title">' . the_title() . '</a>';
$inner .= '<p class="post-meta">';
$inner .= '<span class="small">by</span> <span class="post-author"><a title="Posts by ';
$inner .= the_author();
$inner .= '" href="' . the_author_posts_link() . '">' . the_author() . '</a></span>';
$inner .= '<span class="small">on</span> <span class="post-date">';
$inner .= get_the_date('d/m/Y') . '</span></p>';
$inner .= the_excerpt() . '</div> <!-- End div.item -->';
endwhile;
$end = '</div>';
$end .= '</div> <!-- End div.widget_osu_blog -->';
$latestblogposts = $start . $inner . $end;
return $latestblogposts;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您需要使用可选参数调用函数才能获取返回值,而不是直接回显它。例如,使用 the_title(),您有 3 个可选参数,第三个参数设置输出(默认为 true)。
the_title()。
对于其他值,您需要更改您调用的函数。 the_author() 始终显示(回显)该值,您需要调用 get_the_author() 来代替。
If I understand you correctly, you need to call the functions with an optional argument in order to get the returned value instead of echo it directly. For instance, with the_title(), you have 3 optional arguments, the third sets the output (defaults to true).
the_title().
For other values you will need to change the function you call. the_author() always displays (echo) the value, you need to call get_the_author() instead.