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

Asked

Viewed 144 times

2

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

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

To explain the usefulness:

I’m using wordpress and need to write small news/texts 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 plugin that can accomplish 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>';
}
  • First line, how so? Can give a real example?

  • http://easfutebol.com.br/? p=180 The page above is one of the resumes where I need to insert the first line of news about the professional, I will have several curricunluns, and I need to edit a post with various news, but that when putting them within determined [shortcode], this insert the first line there on that field. Thank you

  • Patricia, personally, I can’t see your problem. The link to your website is cool as a reference, but the description of the problem (and relevant code) should be all within the question. You are free to edit it whenever you need to clarify/add details.

  • If Khaosdr’s answer does not solve the problem, it is because he could not interpret/understand the question. Your job is to make the text as clear as possible (using images, code, explanations, links). This allows exact and time-saving responses.

  • Thank you so much for the attention, as I am working with wordpress, I found a plugin that made my life easier, if anyone needs, follow the plugin: https://wordpress.org/plugins/shortcoder/

1 answer

1

An output would be you using php to remove 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 catch 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.