0

I'm trying to print just the first line of content on wordpress post:

$lines=the_content();
$lines=explode("\n", $string);
echo $lines['0'];

But it doesn't work. I must say I have looked everywhere, even trying with this and this. But nothing works... what can I do? Thanks in advance.

EDIT: I add this for you to understand better what I want to do. I want to print different content depending of the publishing date of the post. So, I have more than 300 posts that have a link of youtube/vimeo in the first line that converts into an iframe when it is printed. So i need that the first line of the content gets printed, because the rest is given by the different snippets: titulo, duracion, sinopsis, etc...

        $pdate  = strtotime(get_the_date("d/m/y")); 
$mydate =   strtotime('09/10/2019');
if ($pdate < $mydate)
{
           echo ONLY FIRST LINE OF CONTENT;

            echo '<br/>';
            echo '<strong>Título:</strong> ';
            echo ''.the_title().'.<br/>';


    echo '<strong>Duración:</strong> '.get_post_meta(get_the_ID(), 'time_duration', TRUE).'';
    echo '<br/>';

    echo '<strong>Sinopsis:</strong> ';
    echo get_the_excerpt();
    echo '<br/>';

    echo '<strong>Director/@s:</strong> ';
    echo get_the_term_list( $post->ID, 'director', '', ', ', '');
    echo '.<br/>';

    echo '<strong>Género:</strong> ';
    echo get_the_term_list( $post->ID, 'genero', '', ', ', '');
    echo '.<br/>';

    echo '<strong>Año de producción:</strong> ';
    echo get_the_term_list( $post->ID, 'any');
    echo '.<br/>';

    echo '<strong>Recomendación por edades:</strong> ';
    $category = get_the_category();
    $first_category = $category[0];
    echo sprintf( '<a href="%s">%s</a>', get_category_link( $first_category ), $first_category->name );
    echo '.<br/>';

    echo '<strong>País/es:</strong> ';
    echo get_the_term_list( $post->ID, 'pais');
    echo '.<br/>';

    echo '<strong>Idioma:</strong> ';
    echo get_the_term_list( $post->ID, 'idioma');
    echo '.<br/>';

    echo '<strong>Información adicional:</strong> '.get_post_meta(get_the_ID(), 'informacionadicional', TRUE).'';
    echo '<br/>';
    echo '<br/>';

}
else {
        echo '<br/>';
        echo $td_mod_single->get_content();
        echo '<strong>Título:</strong> ';
        echo ''.the_title().'.<br/>';


    echo '<strong>Duración:</strong> '.get_post_meta(get_the_ID(), 'time_duration', TRUE).'';
    echo '<br/>';

    echo '<strong>Sinopsis:</strong> ';
    echo get_the_excerpt();
    echo '<br/>';

    echo '<strong>Director/@s:</strong> ';
    echo get_the_term_list( $post->ID, 'director', '', ', ', '');
    echo '.<br/>';

    echo '<strong>Género:</strong> ';
    echo get_the_term_list( $post->ID, 'genero', '', ', ', '');
    echo '.<br/>';

    echo '<strong>Año de producción:</strong> ';
    echo get_the_term_list( $post->ID, 'any');
    echo '.<br/>';

    echo '<strong>Recomendación por edades:</strong> ';
    $category = get_the_category();
    $first_category = $category[0];
    echo sprintf( '<a href="%s">%s</a>', get_category_link( $first_category ), $first_category->name );
    echo '.<br/>';

    echo '<strong>País/es:</strong> ';
    echo get_the_term_list( $post->ID, 'pais');
    echo '.<br/>';

    echo '<strong>Idioma:</strong> ';
    echo get_the_term_list( $post->ID, 'idioma');
    echo '.<br/>';

    echo '<strong>Información adicional:</strong> '.get_post_meta(get_the_ID(), 'informacionadicional', TRUE).'';
    echo '<br/>';
    echo '<br/>';

}

1 Answer 1

0

What you can do is work with this the_excerpt() change this instead of the_content();

And give the amount of words that you want, add this in functions.php after you add the_excerpt();

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Read more in here: https://developer.wordpress.org/reference/functions/the_excerpt/

1
  • Yes, I know about this... what i need is exactly the other thing... I have edited my question adding the code in which I work.
    – Lucas
    Commented Nov 9, 2019 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.