wordpress摘录根据帖子标题进行更改

时间:2013-06-10 17:34:49

标签: php wordpress

摘录根据帖子标题改变。用于删除空白区域。

我想根据帖子的标题调整(增加或减少)摘录。例如(http://www.webdesignerdepot.com/)此站点中的摘录根据帖子标题进行调整。当帖子标题很大时,摘录会减少,反之亦然。此摘录根据帖子标题中的字母进行调整。

假设没有添加摘录。我想从头开始做每件事。我找到了一个帖子(http://www.rlmseo.com/blog/changing-excerpt-size-dynamically/),这是对我的问题的正确答案,但代码不起作用 - 它是旧的,没有更新,并且是在2008年编写的。

我的index.php片段

<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 个答案:

答案 0 :(得分:0)

<?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
?>