简体   繁体   English

修剪标题和摘录组合的总字符值

[英]Trim the total character value of title and excerpt combined

I would like to trim the combined added character value of a post title and excerpt.我想修剪帖子标题和摘录的组合附加字符值。

Currently I can do both independently - the excerpt through the theme settings and the Post title though JS.目前我可以独立完成这两项工作 - 通过主题设置的摘录和通过 JS 的帖子标题。

However, the title lengths vary so much that the height of each post preview varies greatly and looks messy.但是,标题长度变化很大,以至于每个帖子预览的高度变化很大,看起来很凌乱。

Is there a way to add the total number of characters in the title AND the excerpt and then trim - the end result being that posts with a longer title have a shorter portion of the excerpt displaying, with a max of say 100 characters in total as a starting point.有没有办法添加标题和摘录中的字符总数,然后修剪 - 最终结果是标题较长的帖子显示的摘录部分较短,总共最多 100 个字符一个起点。

The title is targeted with:标题的目标是:

.t-entry-title a

And the excerpt with摘录与

.t-entry-excerpt

in the past I have managed to get this to trim my titles在过去,我设法让这个来修剪我的头衔

$(".t-entry-title a").each (function () {
  if ($(this).text().length > 50)
    $(this).text($(this).text().substring(0,50) + '...');
});

But I can't work out how to leave the full title length and then calculate the rest being say 100 (total characters) minus the title and then allocate the remaining characters to the post excerpt.但我不知道如何留下完整的标题长度,然后计算 rest 等于 100(总字符)减去标题,然后将剩余的字符分配给帖子摘录。

Thanks in advance.提前致谢。

Edit -编辑 -

I would usually trim long titles but in this case the client specifically does not want the titles trimmed and unfortunately they vary greatly in length.我通常会修剪长标题,但在这种情况下,客户特别不希望修剪标题,不幸的是它们的长度差异很大。

They are less worried about the trimming of the excerpt and thus would like to show it only when there are characters available.他们不太担心摘录的修剪,因此只想在有可用字符时才显示它。

Some titles are very short - for example 2 words and so need the excerpt to balance out the height - both when used as a carousel and as a grid有些标题很短 - 例如 2 个单词,因此需要摘录来平衡高度 - 无论是用作旋转木马还是用作网格

If your HTML is structured such as below:如果您的 HTML 结构如下:

<div class="t-entry">
    <div class="t-entry-title">
        <a>This is a short title</a>
    </div>
    <div class="t-entry-excerpt">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
</div>
<div class="t-entry">
    <div class="t-entry-title">
        <a>This is a very very very very very very very very long title</a>
    </div>
    <div class="t-entry-excerpt">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
</div>

Then in javascript you can do:然后在 javascript 你可以这样做:

//Max number of characters to be printed for each entry
const maxchar = 100;

//Iterates each entry
document.querySelectorAll(".t-entry").forEach(function(entry){
    var title = entry.querySelector(".t-entry-title>a");
    var excerpt = entry.querySelector(".t-entry-excerpt>p");
    var length = Math.min(title.innerText.length,maxchar);
    //Truncates title and excerpt text up to maxchar
    title.innerText = title.innerText.substring(0,maxchar);
    excerpt.innerText = excerpt.innerText.substring(0,maxchar-length);
});

Instead of trying to manipulate the strings, is there a reason to NOT use CSS styling to trim long strings to fit their containers?与其尝试操纵字符串,是否有理由不使用 CSS 样式来修剪长字符串以适合其容器?

For instance, if the "title" <a> elements had this class/style applied...例如,如果“title” <a>元素应用了此类/样式...

.t-entry-title a {
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

...I'm thinking the contents would adjust to whatever spacing your page elements allow ( of course, it's hard to know without seeing the rest of the page ). ...我认为内容会调整为您的页面元素允许的任何间距(当然,如果没有看到页面的 rest 就很难知道)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM