PHP快捷代码 - 如何接受WP博客文章的类别参数

发布于 2025-02-06 10:41:29 字数 1517 浏览 3 评论 0原文

我有一些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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞琼窗梦回ˉ 2025-02-13 10:41:29

已解决。对于在同一问题上挣扎的任何人,我删除了所有的骆驼,并用下划线代替了

示例


'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'],

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文