Wordpress - 创建一个短代码来列出帖子

时间:2014-09-03 23:26:44

标签: php wordpress shortcode

我创建了自定义帖子。我想列出帖子,但我的短代码无效。

这是我的代码

function.php

// register a custom post type called 'Products'

function wptp_create_post_type() {
    $labels = array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'product' ),
        'add_new' => __( 'New product' ),
        'add_new_item' => __( 'Add New product' ),
        'edit_item' => __( 'Edit product' ),
        'new_item' => __( 'New product' ),
        'view_item' => __( 'View product' ),
        'search_items' => __( 'Search products' ),
        'not_found' =>  __( 'No product Found' ),
        'not_found_in_trash' => __( 'No product found in Trash' ),
    );
    $args = array(
        'labels' => $labels,
        'has_archive' => true,
        'public' => true,
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'custom-fields',
            'thumbnail',
            'page-attributes'
        ),
        'taxonomies' => array( 'post_tag', 'category' ),
    );
    register_post_type('product', $args );
}
add_action( 'init', 'wptp_create_post_type' );

产物-page.php文件

add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
    ob_start();
    extract( shortcode_atts( array (
        'type' => 'product',
        'order' => 'date',
        'orderby' => 'title',
        'posts' => -1,

        'category' => '',
    ), $atts ) );
    $options = array(
        'post_type' => $type,
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,

        'category_name' => $category,
    );
    $query = new WP_Query( $options );
    if ( $query->have_posts() ) { ?>

            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </ul>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }   
}

我已从信息中心创建了一个页面,并选择了产品页面作为模板,当我预览或加载页面时,我无法看到我的帖子。

我在我的页面上尝试了以下短代码。没有什么可以帮助我

[list-posts]

[list-posts type="products" category = "movies" orderby="name" order="ASC"]

我关注this tutorial

4 个答案:

答案 0 :(得分:1)

你好,你需要在你的function.php中添加所有代码我正在粘贴我已经完成的工作

<强> function.php

<?php

function wptp_create_post_type() {
    $labels = array(
            'name' => __( 'News' ),
            'singular_name' => __( 'News' ),
            'add_new' => __( 'New News' ),
            'add_new_item' => __( 'Add New News' ),
            'edit_item' => __( 'Edit News' ),
            'new_item' => __( 'New News' ),
            'view_item' => __( 'View News' ),
            'search_items' => __( 'Search News' ),
            'not_found' =>  __( 'No News Found' ),
            'not_found_in_trash' => __( 'No News found in Trash' ),
    );
    $args = array(
            'labels' => $labels,
            'has_archive' => true,
            'public' => true,
            'hierarchical' => false,
            'supports' => array(
                    'title',
                    'editor',
                    'excerpt',
                    'custom-fields',
                    'thumbnail',
                    'page-attributes'
            ),
            'taxonomies' => array( 'post_tag', 'category' ),
    );
    register_post_type('News', $args );
}
add_action( 'init', 'wptp_create_post_type' );



add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
    ob_start();
    extract( shortcode_atts( array (
    'type' => 'News',
    'order' => 'date',
    'orderby' => 'title',
    'posts' => -1,

    'category' => '',
    ), $atts ) );
    $options = array(
            'post_type' => $type,
            'order' => $order,
            'orderby' => $orderby,
            'posts_per_page' => $posts,

            'category_name' => $category,
    );
    $query = new WP_Query( $options );
    if ( $query->have_posts() ) { ?>

            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </ul>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }   
}


?>

<强>产物-page.php文件

 echo do_shortcode('[list-posts]');

我认为这会对你有所帮助。如果你想要其他东西,请告诉我

答案 1 :(得分:1)

将您的短代码的代码从product-page.php移到functions.php中。另外,[list-posts type="products" category = "movies" orderby="name" order="ASC"]应该有type="product",因为您的帖子类型是产品。

答案 2 :(得分:1)

如果没有其他选择,请保留ob_*个功能。

对于常规WP_Query,可以为get_posts()while ( has_posts() )更改

foreach()。唯一的麻烦是post_class(),但核心功能只是一行,因此很容易适应。另外,extract() is outdated

add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );

function rmcc_post_listing_parameters_shortcode( $atts ) {
    $args = shortcode_atts( array(
        'type' => 'News',
        'order' => 'date',
        'orderby' => 'title',
        'posts' => -1,
        'category' => '',
    ), $atts );

    $options = array(
        'post_type' => $args['type'],
        'order'     => $args['order'],
        'orderby'   => $args['orderby'],
        'posts_per_page' => $args['posts'],
        'category_name'  => $args['category'],
    );

    $posts = get_posts( $options );
    $html = 'No posts found.';
    if ( $posts ) { 
        $html = '<ul>';
        foreach( $posts as $post ) {
            $html .= sprintf(
                '<li id="post-%s" class="%s"><a href="%s">%s</a></li>',
                $post->ID,
                join( ' ', get_post_class( '', $post->ID ) ), // simplified version of get_class() 
                get_the_permalink( $post->ID ),
                $post->post_title
            );
        }
        $html .= '</ul>';
    }
    return $html;
}

答案 3 :(得分:0)

请勿在短代码中使用extract()。事实上,永远不应该使用extract()。这是我最近在WPSE上的另一个答案中所做的短代码示例。根据需要使用和修改

add_shortcode( 'news_box', 'newsbox_new_loading_shortcode' );
function newsbox_new_loading_shortcode($atts){
    ob_start();
    $a = shortcode_atts( 
        [
            'posts_per_page'    => '-1',
            'news_box_title'    => 'Latest News',
            'news_box_more'     => '',
            'post_type'         => 'post',
            'taxonomy'          => '',
            'terms'             => '',
            'category'          => '',
        ], 
        $atts 
    );

    if( '' == $a['taxonomy'] || '' == $a['terms'] ) {
      if( '' == $a['category'] ) {

        $args = [
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
        ];


      }else{
        $args = [
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'category_name'     => $a['category'],

        ];
     }

    }else{
        $args = [
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'tax_query'         => [
                [
                    'taxonomy'  => $a['taxonomy'],
                    'field'     => 'slug',
                    'terms'     => $a['terms'],
                ]
            ]
        ];
    }

    //The following lines is for the excerpt more text NEW!!
    if( 'post' != $a['post_type'] && '' != $a['news_box_more'] ){
        $read_more_text = $a['news_box_more'];
    }else {
        $read_more_text = "Read More &raquo;";
    }
    // end of excerpt more text code

    $q = new WP_Query($args);

    if ( $q->have_posts() ) : 

        while($q->have_posts()) : $q->the_post();   
            $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, '' ); 

            // wp_trim_words function NEW!!
            $content = get_the_content();
            $trimmed_content = wp_trim_words( $content, 55, '<a href="'. get_permalink() .'"> ...' . $read_more_text . '</a>' ); 
            // wp_trim_words function
            ?>


            <li class="news-item">
                <table cellpadding="4">
                    <tr>
                        <td>
                            <?php if( !empty($newsbox_post_img_src)) { ?>
                                <img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
                            <?php } ?>      
                        </td>
                        <td>
                            <?php echo $trimmed_content; // Replaced the_excerpt() ?>
                        </td>
                    </tr>
                </table>
            </li>

        <?php endwhile;
        $list = ob_get_clean();

        return $list;
    endif;

    wp_reset_postdata();
}

支持PHP&lt; 5.4,您可以为短代码功能执行以下操作。

add_shortcode( 'news_box', 'newsbox_new_loading_shortcode' );
function newsbox_new_loading_shortcode($atts){
    ob_start();
    $a = shortcode_atts( 
        array(
            'posts_per_page'    => '-1',
            'news_box_title'    => 'Latest News',
            'news_box_more'     => '',
            'post_type'         => 'post',
            'taxonomy'          => '',
            'terms'             => '',
            'category'          => '',
        ), 
        $atts 
    );

    if( '' == $a['taxonomy'] || '' == $a['terms'] ) {
      if( '' == $a['category'] ) {

        $args = array(
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
        );


      }else{
        $args = array(
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'category_name'     => $a['category'],

        );
     }

    }else{
        $args = array(
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'tax_query'         => array(
                array(
                    'taxonomy'  => $a['taxonomy'],
                    'field'     => 'slug',
                    'terms'     => $a['terms'],
                ),
            ),
        );
    }

    //The following lines is for the excerpt more text NEW!!
    if( 'post' != $a['post_type'] && '' != $a['news_box_more'] ){
        $read_more_text = $a['news_box_more'];
    }else {
        $read_more_text = "Read More &raquo;";
    }
    // end of excerpt more text code

    $q = new WP_Query($args);

    if ( $q->have_posts() ) : 

        while($q->have_posts()) : $q->the_post();   
            $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, '' ); 

            // wp_trim_words function NEW!!
            $content = get_the_content();
            $trimmed_content = wp_trim_words( $content, 55, '<a href="'. get_permalink() .'"> ...' . $read_more_text . '</a>' ); 
            // wp_trim_words function
            ?>


            <li class="news-item">
                <table cellpadding="4">
                    <tr>
                        <td>
                            <?php if( !empty($newsbox_post_img_src)) { ?>
                                <img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
                            <?php } ?>      
                        </td>
                        <td>
                            <?php echo $trimmed_content; // Replaced the_excerpt() ?>
                        </td>
                    </tr>
                </table>
            </li>

        <?php endwhile;
        $list = ob_get_clean();

        return $list;
    endif;

    wp_reset_postdata();
}