Display Excerpts Of Password Protected Posts For WordPress.jpg

Display Excerpts Of Password Protected Posts For WordPress

Outline

    Original Post

    Display Excerpts Of Password Protected Posts For WordPress


    Original Posts

    〈Display Excerpt Of Password Protected Posts For WordPress〉

     

    As a default, posts with password protected cannot display excerpts. However, it is helpful to hack it to achieve marketing goals. For example, you can write some information to require users to subscribe to the newsletter, then send them the password with e-mail. wordpress_display_excerpt_for_password_protected_posts.

    Not Using Built-In Function

    There are two built-in functions in WordPress to display excerpt, the_excerpt() and get_the_excerpt(). However, both of them will hide excerpt for posts with passwords. There are alternatives as below, and you can add it to the archive page or single page.

    $post = get_post();
    echo $post->post_excerpt

    Extend WooCommerce Templates

    As the default WooCommerce template content-single-product.php, there is a piece of code to handle with passwords.

    if ( post_password_required() ) {
    echo get_the_password_form(); // WPCS: XSS ok.
    return;
    }

    And we can extend it like this:

    if ( post_password_required() ) { ?>
    echo get_the_password_form(); // WPCS: XSS ok.
    echo "<p>".$post->post_excerpt."</p>";
    return;
    }

    By the way, if the post isn’t locked down or the password was validated, post_password_required() will return false for both scenarios.

    Related Posts

    〈Learn Python And R On DataCamp. Start Your Data Science Career.〉



    Related Posts

    Aron

    A data scientist working in a retail company, with experience in web design and marketing. Recently dives into cryptocurrency and quantitative investing.

    facebook telegram

    2 comments

    1. hoary

      Τhat is a very good tip especially to those fresh
      to the blogospһere. Brief but very accurate informatіon… Many tһanks for sharing this one.
      A must гead post!

    2. fibrously

      Keep this going pleasе, great job!

    Leave a Reply

    • Required fields are market * .
    • Your email address will not be published.
    • Please ensure your email address is correct, then you will get a notification once a new comment reply.

    Your email address will not be published.