user3237190
user3237190

Reputation: 61

getting excerpt as substring in a wordpress post

I work on .NET applications but currently have to work on php/wordpress .I have a code in WordPress which displays title of a post on image mouse hover .

<h5><?php echo the_title(false, false); ?></h5>

I want to replace title with first line/sub string of Description of that post. OR I can use Excerpts here i.e to write some excerpts with each post and get them in above mentioned h5 tag. I require some hints in implementing any of two tasks. Note while performing my effort/testing purpose I wrote this code but its not working

<h5><?php echo substr(the_title(false, false),0,4); ?></h5>

Thanks ,

Upvotes: 0

Views: 1939

Answers (2)

Rajendra Yadav
Rajendra Yadav

Reputation: 635

use function

<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
   }
echo $my_excerpt; 
 ?>

Upvotes: 0

Shazzad
Shazzad

Reputation: 970

To use excerpt, try -

<h5><?php the_excerpt(); ?></h5>

Or a better approach will be to use Custom fields. You can easily add a Custom field with any post or page from the post/page edit screen. Use a specific key, ex: subtitle, the display that with get_post_meta function -

<h5><?php echo get_post_meta( get_the_ID(), 'subtitle', true ); ?></h5>

Reference:
1. Function the_excerpt
2. Function get_post_meta
3. Function get_the_ID
4. How to add custom field Youtube Video

Upvotes: 2

Related Questions