Wordpress-Создайте шорткод для списка постов

Я создал пользовательский пост. Я хочу перечислить посты, но мои шорткоды не работают.

Вот мой код

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

Я создал страницу из панели инструментов и выбрал страницу продукта в качестве шаблона, и когда я просматривал или загружал страницу, я не мог видеть свои сообщения.

Я попробовал эти шорткоды ниже на своей странице. У меня ничего не получается

[list-posts]

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

Я следил за этим руководством.


person hellosheikh    schedule 03.09.2014    source источник
comment
Вам нужно создать тип сообщения о продукте, а не о странице. type по умолчанию равно product, и вы также указываете его в своем примере.   -  person doublesharp    schedule 04.09.2014


Ответы (4)


Здравствуйте, вам нужно добавить весь код в ваш 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;
    }   
}


?>

product-page.php

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

Я думаю, это поможет вам. Дайте мне знать, если вы хотите что-то еще

person Sandeep Kumar    schedule 04.09.2014

Переместите код вашего шорткода из product-page.php в functions.php. Кроме того, [list-posts type="products" category = "movies" orderby="name" order="ASC"] должно иметь type="product", так как тип вашего сообщения — продукт.

person WordPress Mike    schedule 04.09.2014

Оставьте ob_* функции на тот случай, когда альтернативы не будет.

WP_Query можно заменить на get_posts() и while ( has_posts() ) на обычный foreach(). Единственная проблема — post_class(), но основная функция — всего одна строка, поэтому ее легко адаптировать. Кроме того, extract() устарело.

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;
}
person brasofilo    schedule 04.09.2014

Не используйте 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 ‹ 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();
}
person Pieter Goosen    schedule 04.09.2014