PHP快捷代码 - 如何接受WP博客文章的类别参数
我有一些PHP可以按WordPress中的类别显示显示博客文章片段,但是我无法接受该类别的参数。
无论我在短码的“ PageCategory”部分中放置什么,短代码都会显示所有博客文章。
但是,如果我进行了硬码“ PageCategory” =&GT,则确实显示了特定类别的帖子; ''特定类别。
如何获得此功能接受变量类别?
function wpb_postsbycategory($atts) {
$a = shortcode_atts( array(
'pageCategory' => ''
), $atts );
// the query
$the_query = new WP_Query( array(
'category_name' => $a['pageCategory'],
'posts_per_page' => 15
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
} else {
// if no featured image is found
$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
}
}
} else {
// no posts found
$string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
快捷代码
[categoryposts pageCategory='aCategory']
I have some PHP to show display Blog Post Snippets by category in Wordpress, but I can't get it to accept an argument for the category.
The shortcode displays all Blog Posts no matter what I put in the 'pageCategory' portion of the shortcode.
However, it does show posts for specific categories if I hardcode 'pageCategory' => ' ' to a specific category.
How do I get this function to accept variable categories?
function wpb_postsbycategory($atts) {
$a = shortcode_atts( array(
'pageCategory' => ''
), $atts );
// the query
$the_query = new WP_Query( array(
'category_name' => $a['pageCategory'],
'posts_per_page' => 15
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
} else {
// if no featured image is found
$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
}
}
} else {
// no posts found
$string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
Shortcode
[categoryposts pageCategory='aCategory']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决。对于在同一问题上挣扎的任何人,我删除了所有的骆驼,并用下划线代替了
示例
:
'category_name'=&gt; $ a ['pageCategory'],
到
'category_name'=&gt; $ a ['page_category'],
Solved. For anyone struggling with the same issue, I removed all of my camelCase and replaced it with underscores
Example:
From
'category_name' => $a['pageCategory'],
To
'category_name' => $a['page_category'],