doulangpeng3933 2014-09-03 23:26
浏览 32
已采纳

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

I have created the custom post. I want to list down posts, but my shortcodes are not working.

Here is my code

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' );

product-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;
    }   
}

I have created a page from dashboard and select product-page as template and when I preview or load the page, but I couldn't see my posts.

I have try these below shortcodes on my page. Nothing works out for me

[list-posts]

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

I followed this tutorial

  • 写回答

4条回答 默认 最新

  • dswg47377 2014-09-04 04:50
    关注

    Hello you need to add all code in your function.php i am pasting my what i have done

    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;
        }   
    }
    
    
    ?>
    

    product-page.php

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

    I think that would help you. Let me know if you want something else

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值