1
<div class="class1">
    <ul>
        <li>
            <div class="class2">
                <?php the_post_thumbnail( 'large') ?>
            </div>
            <div class="class3">
                <a href="">category</a>
                <h2><?php the_title(); ?></h2>
                <p><?php echo wp_trim_words( get_the_content(), 70 ); ?> </p>
            </div>
        </li>
    </ul>
</div>

The above is an excerpt that is kept on content-home.php

This is the correct version or correct coding practice →

<p><?php echo wp_trim_words( get_the_content(), 70 ); ?> 

or this one w/o the <p></p> tag →

<?php echo wp_trim_words( get_the_content(), 70 ); ?> 
3
  • Sorry but I don't see the question here?
    – Johansson
    Commented Jul 26, 2017 at 11:06
  • My question is <p></p> tag is automaticall fetched when we run this → <?php echo wp_trim_words( get_the_content(), 70 ); ?> or It is advised that we should incorporate the dynamic code within <p></p> tag.
    – WordCent
    Commented Jul 26, 2017 at 11:09
  • 1
    you can also just use "get_the_excerpt()" this way, you can write a precise short text explaining it better than just a cut off version of the content
    – Stender
    Commented Jul 26, 2017 at 12:25

1 Answer 1

4

To output your content, It is advised that you pass your content through a filter. get_the_content() does not do this, so it will not for example execute shortcodes or create <p> tags.

So, it would be better to use it this way:

<?php echo apply_filters( 'the_content', wp_trim_words( get_the_content() , 70 ) ); ?> 

You can also pass the $before and $after arguments to your title. This is a simple example of how to do so:

<?php the_title('<h1>', '</h1>'); ?>

Which will output this:

<h1>Your Title Here</h1>

It's possible to pass variables or functions too. Take a look at this example:

<?php the_title('<h1><a href="'.get_the_permalink().'">','</a></h1>'); ?>

This will create a link of your title and wrap it in a header tag:

<h1><a href="some path here">Your Title Here</a></h1>

You might want to take a look at this codex page for additional information about the_title_attribute() too.

0

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.