修改快捷代码以使用自定义帖子类型

时间:2015-02-13 作者:JoaMika

我有这个短代码,它可以很好地处理帖子,但我需要修改它,以获取自定义帖子类型和自定义分类的参数。

<?php
// [blog_posts]
function shortcode_latest_from_blog($atts, $content = null) {
    $sliderrandomid = rand();
    extract(shortcode_atts(array(
        "posts" => \'8\',
        "columns" => \'4\',
        "category" => \'\',
        "style" => \'text-normal\',
        "image_height" => \'auto\',
        "show_date" => \'true\',
        "excerpt" => \'true\',
    ), $atts));
    ob_start();
    ?>

        <div class="row column-slider">
            <div id="slider_<?php echo $sliderrandomid ?>" class="iosSlider blog-posts <?php if($style  == \'text-overlay\') { ?>slider-center-arrows<?php } ?>" style="min-height:<?php echo $image_height; ?>;height:<?php echo $image_height; ?>;">
                <ul class="slider large-block-grid-<?php echo $columns ?> small-block-grid-2">

                    <?php
                    $args = array(
                        \'post_status\' => \'publish\',
                        \'post_type\' => \'post\',
                        \'category_name\' => $category,
                        \'posts_per_page\' => $posts
                    );

                    $recentPosts = new WP_Query( $args );

                    if ( $recentPosts->have_posts() ) : ?>

                        <?php while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>

                        <li class="ux-box text-center post-item ux-<?php echo $style; ?>">
                            <div class="inner">
                              <div class="inner-wrap">
                                <a href="<?php the_permalink() ?>">
                                  <div class="ux-box-image">
                                        <div class="entry-image-attachment" style="max-height:<?php echo  $image_height; ?>;overflow:hidden;">
                                            <?php the_post_thumbnail(\'medium\'); ?>
                                        </div>
                                  </div><!-- .ux-box-image -->
                                  <div class="ux-box-text text-vertical-center">
                                        <h3 class="from_the_blog_title"><?php the_title(); ?></h3>
                                        <div class="tx-div small"></div>
                                        <?php if($excerpt != \'false\') { ?>
                                            <p class="from_the_blog_excerpt small-font show-next"><?php
                                                $excerpt = get_the_excerpt();
                                                echo string_limit_words($excerpt,15) . \'[...]\';
                                            ?>
                                           </p>
                                         <?php } ?>

                                     </div><!-- .post_shortcode_text -->
                                </a>

                                   <?php if($show_date != \'false\') {?>
                                                        <div class="post-date">
                                                                <span class="post-date-day"><?php echo get_the_time(\'d\', get_the_ID()); ?></span>
                                                                <span class="post-date-month"><?php echo get_the_time(\'M\', get_the_ID()); ?></span>
                                                         </div>
                                    <?php } ?>
                                </div><!-- .inner-wrap -->
                            </div><!-- .inner -->
                        </li><!-- .blog-item -->

                        <?php endwhile; // end of the loop. ?>

                    <?php

                    endif;
                    wp_reset_query();

                    ?>
                </ul>   <!-- .slider -->

                <div class="sliderControlls dark">
                    <div class="sliderNav small hide-for-small">
                    <a href="javascript:void(0)" class="nextSlide prev_<?php echo $sliderrandomid ?>"><span class="icon-angle-left"></span></a>
                    <a href="javascript:void(0)" class="prevSlide next_<?php echo $sliderrandomid ?>"><span class="icon-angle-right"></span></a>
                    </div>
                </div><!-- .sliderControlls -->
        </div> <!-- .iOsslider -->
    </div><!-- .row .column-slider -->


    <?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

function string_limit_words($string, $word_limit) {
    $words = explode(\' \', $string, ($word_limit + 1));
    if(count($words) > $word_limit)
    array_pop($words);
    return implode(\' \', $words);
}

add_shortcode("blog_posts", "shortcode_latest_from_blog");

1 个回复
最合适的回答,由SO网友:gdaniel 整理而成

代码的这一部分定义了可以通过短代码传递的参数:

extract(shortcode_atts(array(
        "posts" => \'8\',
        "columns" => \'4\',
        "category" => \'\',
        "style" => \'text-normal\',
        "image_height" => \'auto\',
        "show_date" => \'true\',
        "excerpt" => \'true\',
    ), $atts));
这允许您稍后将此值用作$posts、$columns等等。如果在短代码中未传递任何值,则将使用默认值。在这种情况下,$posts=8(要显示的帖子数量)

这是根据传递的值获取帖子的查询:

$args = array(
     \'post_status\' => \'publish\',
     \'post_type\' => \'post\',
     \'category_name\' => $category,
     \'posts_per_page\' => $posts
);
因此,如果您希望能够通过快捷码传递帖子类型,可以执行以下操作:

extract(shortcode_atts(array(
            "post_type" => \'post\',
            "posts" => \'8\',
            "columns" => \'4\',
            "category" => \'\',
            "style" => \'text-normal\',
            "image_height" => \'auto\',
            "show_date" => \'true\',
            "excerpt" => \'true\',
        ), $atts));
您可以修改查询以使用该值:

$args = array(
     \'post_status\' => \'publish\',
     \'post_type\' => $post_type,
     \'category_name\' => $category,
     \'posts_per_page\' => $posts
);
编写快捷码时,您可以执行以下操作[blog_posts post_type="my_custom_post_type"]

同样的想法也可以通过自定义分类法实现。如果您只使用一种自定义帖子类型,而且这种类型永远不会更改,那么您可以跳过第一步,直接更改查询。还可以查看codex以了解wp\\U查询的工作原理,以及它期望的值:

http://codex.wordpress.org/Class_Reference/WP_Query

结束

相关推荐

Child theme functions.php

我不知道如何改变我的孩子主题的功能。php。在woothemes文档中,它说“子主题中的functions.php应该是空的,并且不包括父主题functions.php中的任何内容。”我需要使用此功能来不显示产品类别,但我不确定如何在我的子主题中执行此操作。也许有人能给我提供一些指示?add_action( \'pre_get_posts\', \'custom_pre_get_posts_query\' ); function custom_pre_get_posts_query( $