【问题标题】:Multiple excerpt/content lengths in WordPressWordPress中的多个摘录/内容长度
【发布时间】:2017-02-26 19:40:40
【问题描述】:

我已经查看了这篇文章 (Multiple excerpt lengths in wordpress),但很明显,它已经多次从我们现在的位置删除了 WordPress 版本。对于不同的区域,我也需要多个长度,但要去掉标签。我不在帖子上使用“摘录”框,因此它需要连接内容本身。我非常喜欢该页面上包含“短”、“常规”和“长”的建议答案,但我似乎无法让它发挥作用。

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

这是我目前拥有的。也许有人可以看到我在哪里搞砸了:

函数.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>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    更多wp_trim_words

    【讨论】:

    • 我试过了。它不适用于我的 $recent['ID'] 部分,据我所知,它必须存在,因为它在循环之外。换句话说,这是短代码的一部分,而不是在博客页面上(我可能不会在这里解释自己。此刻非常疲劳)。
    • wp_trim_words 你可以使用任何你想要的文本。例如wp_trim_words( $recent['post_content'], 20, '...' )
    • 我以前试过。但它不适用于循环的 $recent["ID"] 组件。至少,我做不到。
    • 我为重复输入道歉。啊哈!我没有正确的垫子。现在就试试吧。
    • 这就像一个魅力。谢谢你。我一直用 $recent['ID'], 20, '...' 尝试它,但没有成功。没想到把 ID 改成 post_content。
    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    相关资源
    最近更新 更多