WordPress中的多个摘录/内容长度

时间:2016-10-17 18:03:14

标签: php wordpress

我已经查看了这篇文章(Multiple excerpt lengths in wordpress),但很明显,WordPress的版本很多时候已从我们现在的位置删除了很多次。我也需要多个不同长度的长度,但标签被剥离。我没有使用"摘录"帖子上的框,所以它需要连接内容本身。我非常喜欢该页面上的建议答案,包括"短","常规"和#34;长",但我似乎无法让它发挥作用。

我在我的主页上的短代码循环中使用它,以与其余4个帖子不同的方式显示组中的第一个帖子。我只在第一篇文章末尾包括代码,因为其他代码不包含摘录。

这就是我现在拥有的。也许有人可以看到我弄乱的地方:

的functions.php     课程摘录{

// Default length (by WordPress)
public static $length = 55;

// So you can call: my_excerpt('short');
public static $types = array(
  'short' => 25,
  'regular' => 55,
  'long' => 100
);

/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length 
* @return void
* @author Baylor Rae'
*/
public static function length($new_length = 55) {
Excerpt::$length = $new_length;

add_filter('excerpt_length', 'Excerpt::new_length', 999);

Excerpt::output();
}

// Tells WP the new length
public static function new_length() {
  if( isset(Excerpt::$types[Excerpt::$length]) )
    return Excerpt::$types[Excerpt::$length];
  else
    return Excerpt::$length;
}

// Echoes out the excerpt
public static function output() {
  the_excerpt();
}

}

// An alias to the class
function my_excerpt($length = 55) {
  Excerpt::length($length);
}

最近的帖子                                                 

                <?php $args2 = array(
                    'post_type' => 'post',
                    'posts_per_page' => 5,
                    'orderby' => 'date',
                    'order' => 'DESC',
                    'post_status' => 'publish',
                );
                $recent_posts = wp_get_recent_posts( $args2 );
                $count2 = 0;

                foreach( $recent_posts as $recent ){
                    $count2++;
                    if ($count2 == 1) { ?>

                <div class="col-xs-12 col-sm-6 col-md-6">
                    <div class="first-news clearfix">
                        <a href="<?php echo get_permalink($recent["ID"]); ?>" title="<?php echo $recent["post_title"]; ?>">
                            <?php $feat_image3 = wp_get_attachment_url( get_post_thumbnail_id($recent["ID"]) ); ?>
                            <div class="post-thumbnail" style="background-image: url(<?php echo $feat_image3; ?>); height: 200px;">

                                <?php $format = get_post_format($recent["ID"]);
                                    if ( false === $format ) { ?>
                                        <span class="standard overlay-icon"><i class="fa fa-search fa-2x"></i></span>                                                   
                                    <?php }

                                    if ( 'aside' === $format ) { ?>
                                        <span class="aside overlay-icon"></span>
                                    <?php } 

                                    if ( 'image' === $format ) { ?>
                                        <span class="image overlay-icon"><i class="fa fa-photo fa-2x"></i></span>
                                    <?php }

                                    if ( 'video' === $format ) { ?>
                                        <span class="video overlay-icon"><i class="fa fa-video-camera fa-2x"></i></span>
                                    <?php }

                                    if ( 'quote' === $format ) { ?>
                                        <span class="quote overlay-icon"><i class="fa fa-quote-left fa-2x"></i></span>
                                    <?php }

                                    if ( 'link' === $format ) { ?>
                                        <span class="link overlay-icon"><i class="fa fa-link fa-2x"></i></span>
                                    <?php }

                                    if ( 'gallery' === $format ) { ?>
                                        <span class="gallery overlay-icon fa-stack">
                                            <i class="fa fa-image fa-stack-1x fa-nudge-right fa-nudge-down fa-outline-inverse" style="font-size:1.5em" ></i>
                                            <i class="fa fa-image fa-stack-1x fa-outline-inverse"  style="font-size:1.5em" ></i>
                                            <div style="background-color:#FFF;width:70%;height:55%;left:4%;top:10%;position:absolute"></div>
                                            <i class="fa fa-image fa-stack-1x fa-nudge-left fa-nudge-up fa-outline-inverse" style="font-size: 1.5em"></i>
                                        </span>
                                    <?php } 

                                    if ( 'status' === $format ) { ?>
                                        <span class="status overlay-icon"><i class="fa fa-sticky-note fa-2x"></i></span>
                                    <?php }

                                    if ( 'audio' === $format ) { ?>
                                        <span class="audio overlay-icon"><i class="fa fa-volume-up fa-2x"></i></span>
                                    <?php }

                                    if ( 'chat' === $format ) { ?>
                                        <span class="chat overlay-icon"><i class="fa fa-commenting fa-2x"></i></span>
                                    <?php }

                                    if ( 'code' === $format ) { ?>
                                        <span class="code overlay-icon"><i class="fa fa-code fa-2x"></i></span>
                                    <?php }
                                ?>
                            </div>
                        </a>

                        <a title="<?php the_title($recent["ID"]) ?>" href="<?php echo get_permalink($recent["ID"]); ?>" rel="bookmark">
                            <h3 class="post-box-title"><?php echo get_the_title($recent["ID"]); ?></h3>
                        </a>

                        <p class="post-meta">
                            <span class="darwin-date">
                                <i class="fa fa-clock-o"></i>
                                <?php echo get_the_time('M d, Y', $recent["ID"]); ?>
                            </span>
                        </p>

                        <div class="entry">
                            <p>
                                <?php echo my_excerpt('short', $recent['ID']); ?>
                            </p>

                            <div class="more-posts">
                                <a class="more-link" title="<?php echo get_the_title($recent["ID"]); ?>" href="<?php the_permalink($recent["ID"]); ?>">Read More &raquo;</a>
                            </div>

1 个答案:

答案 0 :(得分:0)

您可以使用原生Wordpress函数wp_trim_words修剪摘录。

更多wp_trim_words