Wordpress 显示来自类别短代码的帖子

Wordpress 显示来自类别短代码的帖子,wordpress,function,shortcode,Wordpress,Function,Shortcode,我试图在Wordpress中创建一个短代码,以显示某个类别(ID=1361…我们的播客类别)的帖子列表。当我使用下面的代码时,它只显示短代码文本([pages_posts]),而不是帖子列表。有什么想法吗 // Podcast Page Listing shortcode function podcast_pages_posts() { $args = array( 'post_type' => 'post', 'posts_per_page'=> -1, '

我试图在Wordpress中创建一个短代码,以显示某个类别(ID=1361…我们的播客类别)的帖子列表。当我使用下面的代码时,它只显示短代码文本([pages_posts]),而不是帖子列表。有什么想法吗

// Podcast Page Listing shortcode
function podcast_pages_posts() {
$args = array(
    'post_type' => 'post',
    'posts_per_page'=> -1,
    'cat'=> 1361,
);

$podcast_pages_posts = new WP_Query( $args );

if( $podcast_pages_posts->have_posts() ):
    $ppp_output = '<ul>';
    while ( $podcast_pages_posts->have_posts() ) : $podcast_pages_posts->the_post();
        $ppp_output .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
    endwhile; 
    $ppp_output .= '</ul>';
endif;

return $ppp_output;
wp_reset_postdata();

}
add_shortcode( 'pages_posts', 'podcast_pages_posts' );
//列出短代码的播客页面
功能播客\页面\帖子(){
$args=数组(
“post_type”=>“post”,
“每页帖子数”=>-1,
“猫”=>1361,
);
$podcast\u pages\u posts=新的WP\u查询($args);
如果($podcast\u pages\u posts->have\u posts()):
$ppp_输出=“
    ”; 而($podcast_pages_posts->have_posts()):$podcast_pages_posts->the_post(); $ppp_输出。='
  • '; 结束时; $ppp_输出。='
'; endif; 返回$ppp_输出; wp_reset_postdata(); } 添加快捷码('pages\u posts','podcast\u pages\u posts');
这是一个不起作用的页面:
这是同一个站点上的一个页面,另一个短代码正在运行(它是底部的列表),因此我不认为该站点完全拒绝使用短代码:

我一直在修补同一个问题,并找到了有效的解决方案。放入functions.php文件中。然后在页面中放置快捷码,如[homepage\u info id=“234”]

//特定类别的快捷码。在主页(或任何地方)上使用以显示帖子类别列表
功能快捷信息快捷($atts){
提取(短码)附件(数组)(
'id'=>17//添加*默认类别id
)美元(附件);;
$posts=获取_posts(数组(
“每页帖子数”=>-1,
“发布状态”=>“发布”,
'cat'=>$id,
) );
$return='';
$return.='';
foreach($posts作为$post){
$permalink=get\u permalink($post->ID);
$return.='';
} 
$return.='';
return$return;
}
添加快捷代码(“主页信息”、“快捷信息”);
//放置在页面:[主页信息]简单整洁,适合客户
//或
//[homepage_info id=“x”](x=类别id/除*默认类别id之外的类别id。

需要澄清的是,此代码位于当前活动主题的
functions.php
文件中?此外,在
返回后,您不能有任何内容statement@celeriko是的,您应该在返回之前运行
wp\u reset\u postdata();
(尽管大多数人使用效率低下的wp\u reset\u query(),但这是值得称赞的)。您是否可以通过返回一个静态字符串来验证此短代码是否正常工作,代码看起来很好。@celeriko-这是关键!我像个白痴一样,在我的移动主题中的functions.php中工作,而不是在我的主主题中工作。#失败我还移动了wp_reset_查询()以上是建议的回复。现在一切正常!谢谢各位!我如何才能正确地将此标记为已回答?我是否标记原始帖子?@user2805240我们中最好的,很高兴我能提供帮助
// shortcode for specific category.  Use on homepage (or wherever) to display a list of categories of posts
function quick_info_shorty( $atts ) {
    extract( shortcode_atts( array(
        'id' => 17      // Add the *default category id
    ), $atts ) );

    $posts = get_posts( array(
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'cat'       => $id,
    ) );

    $return = '';
    $return .= '<div class="homepage_info_box">';

    foreach ( $posts as $post ) {
        $permalink = get_permalink($post->ID);
        $return .= '<a class="item" href="' . $permalink . '">' . apply_filters( 'the_title', $post->post_title ) . '</a>';
    } 

$return .= '</div>';
return $return;
}
add_shortcode( 'homepage_info', 'quick_info_shorty' );  
// place in page: [homepage_info]   Simple & clean for clients
// OR
// [homepage_info id="x"]           (x = id of category/categories other than *default category id.