简体   繁体   English

从插件修剪推文

[英]Trim tweets from plugin

I am using this Wordpress plugin 我正在使用这个Wordpress插件

https://smashballoon.com/custom-twitter-feeds/demo https://smashballoon.com/custom-twitter-feeds/demo

I would like to trim the Tweets to 120 characters. 我想将推文调整为120个字符。

I have no JS skills and very little php - I tried this which I found but I don't know if it's relevant at all. 我没有JS技能,却很少有php-我尝试了发现的这个,但是我根本不知道它是否相关。

$twitter_feed = (strlen($twitter_feed) > 120) ? substr($twitter_feed,0,10).'...' : $twitter_feed;
enter code here

I also attempted to edit my working code for trimming my post titles to suit which did not work. 我还尝试编辑我的工作代码,以修整我的帖子标题以适应不起作用的情况。

    function custom_trim_my_tweet( $twitter_feed ) {
        global $uncode_vc_index; 
        if ( is_singular() && !$uncode_vc_index )
            return $twitter_feed;

$twitter_feed = wp_trim_words( $twitter_feed, 9 );
    return $twitter_feed; 
}
add_filter( 'ctf-tweet-text', 'custom_trim_my_tweet' );

To trim the text using JavaScript: 要使用JavaScript修剪文本,请执行以下操作:

First you have to select the element using a css selector. 首先,您必须使用CSS选择器选择元素。 It's easiest if you can select the elements using an element id, like so: 如果可以使用元素ID选择元素,这是最简单的,例如:

var tweet = document.getElementById("my_tweet_id");

Then you can grab the text content of the element with element.innerText 然后,您可以使用element.innerText获取元素的文本内容

var tweetText = tweet.innerText;

Then you can simple cut the length of the text inside the element using string.substring() 然后,您可以使用string.substring()简单削减元素内文本的长度。

tweet.innerText = tweetText.substring(0, 120);

I honestly think you should try to fix this at the plugin/wordpress/php level rather than cutting text in JavaScript. 老实说,我认为您应该尝试在插件/ wordpress / php级别修复此问题,而不是在JavaScript中剪切文本。 But if that's what you want to do, then the above methos would work. 但是,如果您要这样做,那么上面的方法将起作用。

EDIT: In case the tweet elements do not have unique ID's you will have to select all of them and then loop through each and perform the text-cutting. 编辑:如果tweet元素没有唯一的ID,则您必须选择所有ID,然后遍历每个元素并执行文本剪切。 I'll give you an example of how to do that: 我会给你一个例子来做到这一点:

You might have to be a bit creative with your css selectors depending on how the tweets are displayed. 您可能需要对CSS选择器有所创意,具体取决于推文的显示方式。

In my example I will assume that you have a div with the ID 'tweets' that then holds five separate div elements, one for each tweet, that all have the same class of 'single-tweet' . 在我的例子,我会假设你有一个div ID为'tweets'那则拥有五个独立div元素,每个鸣叫,所有具有相同类的'single-tweet'

First we get all the 'single-tweet' elements: 首先,我们获得所有'single-tweet'元素:

var tweets = document.querySelectorAll('#tweets .single-tweet');

tweets now contain a node list of the five 'single-tweet' divs. tweets现在包含五个'single-tweet' div的节点列表。

Then we loop through them with .forEach() and do the text-cutting on each element. 然后,我们使用.forEach()遍历它们, .forEach()每个元素进行文本剪切。

tweets.forEach( (tweet) => {
    tweet.innerText = tweet.innerText.substring(0, 120);
})

Now all of the 'single-tweet' elements will contain what's left after you cut the text down to 120 characters. 现在,所有'single-tweet'元素都将包含将文本缩减为120个字符后剩下的内容。

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

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