how to display the first line of a text snippet, linking this to the page where it is?

2

I need to display the first line of text that will be between [shortcode] and that this line is a link to your source page.

The code below works, it displays a div, but I want to display the first line of the text that I put [shortcode] between [/shortcode] .

To explain the utility:

I'm using wordpress and I need to write small news / posts in posts, where the first line of each news will go to certain fields within resumes (which are also posts).

I have not found any plugins that can do this with this simplicity.

<?php

add_shortcode('noticiaclaudio', 'func_news');
function func_news($atts, $content='') {
    extract(shortcode_atts(array(
        'height' => '15px',
        'width' => '60%',
        'padding' => '3px',
        'font-color' => '#999999',
        'font-size' => '12px',
        ), $atts));
    return '<div style="height:'.$height.';width:'.$width.';padding:'.$padding.';">' . do_shortcode($content) . '</div>';
}
    
asked by anonymous 29.10.2014 / 12:05

1 answer

1

One way would be to use php to remove the shortcode tags, using the following function:

$a= '[shortcode]Hello World[/shortcode]';
echo str_replace("[shortcode]","",str_replace("[/shortcode]","",$a));

This would return

Hello World

If you had text with more than one line, you could break it into an array using the strtok function, as said here (and also commented here >) to get the first line as:

$a = strtok($a, '\n')

Where $ a would be the first line of your text.

You could also use an array in the method:

<?php
 $lines=explode("\n", $string);
 echo $lines['0'];
?>

That the result would still be the first line of the file.

    
29.10.2014 / 16:58