显示类别短代码的帖子

时间:2014-05-02 18:18:52

标签: 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,
    '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' );

以下是无法使用的页面:http://www.churchmarketingsucks.com/developer-test/ 这是另一个短代码在同一网站上的页面(它是底部的列表),所以我认为该网站完全拒绝短代码:http://www.churchmarketingsucks.com/cmp/

1 个答案:

答案 0 :(得分:0)

我一直在修补同样的问题,并找到了解决方案。放在functions.php文件中。然后在您的页面中放置短代码,如[homepage_info id =“234”]

// 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.