dongsheng4126335
dongsheng4126335
2013-06-10 17:34

wordpress摘录基于帖子标题的变化[关闭]

已采纳

Excerpt changing according to post title. For removing the blank space.

I want to adjust (increase or decrease) the excerpt depending on the title of the post. For example (http://www.webdesignerdepot.com/) here in this site the excerpt is adjusted according to the post title. When the post title is large, the excerpt is decreased and vice versa. This excerpt is adjusted according to the letters in the post title.

Suppose there is no excerpt added. I want every thing from start. I found a post (http://www.rlmseo.com/blog/changing-excerpt-size-dynamically/) which is a correct answer to my question, but the code is not working - it is old, not updated, and was written in 2008.

My index.php snippet

<div class="mcr-post-area mcr-handheld">  
    <div class="mcr-item-warp">
        <a class="mcr-title2-link" href="<?php the_permalink() ?>"><h2 class="mcr-title2-header"><?php the_title(); ?></h2></a>
        <div class="mcr-below-title">
            <span class="mcr-author">
                <i class="icon-user"></i><span>&nbsp; <?php the_author(); ?>  </span>
            </span>             

            <span class="mcr-date">
                <i class="icon-calendar-empty"></i>&nbsp; <?php the_time('M j, Y ') ?>      
            </span>

            <span class="mcr-comment">
                <i class="icon-comments"></i> &nbsp;
                <?php comments_number( 'no responses', 'one response', '% responses' ); ?>
            </span>

            <div style="clear:both;"></div>
        </div>   
    </div><!--mcr-item-warp-->

    <div class="mcr-post-detial" style="height: auto;">
        <div style="margin: 0px; padding: 0px; border: 0px;">
            <div class="entry-home">
                <?php the_excerpt(strlen(the_title())); ?>
                <?php the_excerpt(); ?>
            </div>
        </div>
    </div>

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dounuo9921 dounuo9921 8年前
    <?php
    // Dynamically resize excerpt according to title length
    $rem_len = ""; //clear variable
    $title_len = strlen(get_the_title()); //get length of title
    if($title_len <= 35){
        $rem_len=188; //calc space remaining for excerpt
    }elseif($title_len <= 70){
        $rem_len=146;
    }elseif($title_len <= 105){
        $rem_len=104;
    }elseif($title_len <= 140){
        $rem_len=62;
    }
    $trunc_ex = substr(get_the_excerpt(), 0, $rem_len); //truncate excerpt to fit remaining space
    if(strlen($trunc_ex) < strlen($post->post_excerpt)) $trunc_ex = $trunc_ex . " [...]";
    echo "<p>" . $trunc_ex . "</p>"; //display excerpt
    ?>
    
    点赞 评论 复制链接分享

相关推荐