QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Bitcoin BankingBuy, Sell, Store and Earn Interest
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Get Excerpt in WordPress

Get Excerpt in WordPress

Here is the code to display the excerpt of the current requested page in WordPress. This code works outside loop also. Excerpts are usually displayed in description meta tags.

<?php

    if(is_single() || is_page())
    {
        global $wp_query;
        $id = $wp_query->post->ID;
        $the_post = get_post($id);
        $the_excerpt = $the_post->post_content;
        $shortcode_pattern = get_shortcode_regex();
        $the_excerpt = preg_replace('/' . $shortcode_pattern . '/', '', $the_excerpt);
        $the_excerpt = strip_tags($the_excerpt);   
        echo esc_attr(substr( $the_excerpt, 0, 200));
    }
    else if(is_home() || is_front_page())
    {
        echo "QNimate provides tutorials on WordPress, Hybrid Mobile Development, Web Development and Much More";
    }
    else if(is_category())
    {
        $name = single_cat_title('', false);
        echo "Learn about " . $name;
    }
    else if(is_tag())
    {
        $name = single_tag_title('', false);
        echo "Learn about " . $name;   
    }
    else if(is_date())
    {
        $date = single_month_title(' ', false);
        echo "Displaying posts of ". $date;
    }

For home, category, tag and date pages you need to define the expert explicitly. For posts and pages the excerpt is retrieved from the content.

Jan 5, 2015Narayan Prusty
Push Notification in Intel XDK using Push PluginLocal Database Storage using Intel XDK
Comments: 0

    Leave a Reply Cancel reply

    To create code blocks or other preformatted text, indent by four spaces:

        This will be displayed in a monospaced font. The first four
        spaces will be stripped off, but all other whitespace
        will be preserved.
        
        Markdown is turned off in code blocks:
         [This is not a link](http://example.com)
    

    To create not a block, but an inline code span, use backticks:

    Here is some inline `code`.

    Narayan Prusty

    I am a full-stack web developer. I specialize in Blockchain and JavaScript. This is my personal blog where I write about programming and technologies that I learn and feel interesting to share.

    6 years ago 1 Comment WordPress
    Share this
    0
    GooglePlus
    0
    Facebook
    0
    Twitter
    0
    Linkedin
    Related Articles
    • WordPress Custom Meta Boxes Tutorial
    • HTML5 Mobile App Development Using Intel XDK
    • Create a Drag and Drop Page Builder for WordPress
    • Get WordPress Post Date as Current Time Difference
    • Intel XDK App using WordPress Backend
    Our Sponsor
    Freelance: I am available
    @narayanprusty
    Hi, I am a full-stack developer - focusing on JavaScript, WordPress, Blockchain, DevOps, Serverless and Cordova. You can outsource your app development to me.





    Will get back to you soon!!!
    WordPress
    ReactJS
    Meteor
    Blockchain
    Serverless
    Kubernetes
    DevOps
    DB & Storage
    Ejabberd
    Let's get started
    My Books

    2014 - 2015 © QNimate
    All tutorials MIT license