How to show post excerpts


Hello, First of all i want to thank you beans developers for providing this amazing framework. I was building a child theme using beans, This is the first time i'm working with beans, That's why i'm facing some dificulties.

I wonder how can i show the post excerpt instead of full post and how to i show the meta description like this "Posted By {Author} | In: {category} | Last Updated: {date} | {3 Comments}" and remove the tag cloud and the category for bottom of the post..??

Could You help me with this?

Regards, Anupam Saikia


I have question about expcerpt too. How can i read excerpt to use them on my static homepage?

EDIT: I found solution for my problem, and I will share it, but it isn't answer to your question. I thought that someone can use it :)

Here's my snippet, which get post title, excerpt and link by ID and echoes them as a HTML markup for my purpose:

  function get_post_meta_by_id( $post_id ) {
    global $post;
    $post = get_post( $post_id );
    setup_postdata( $post );
    $title = get_the_title();
    $excerpt = get_the_excerpt();
    $link = get_permalink();
    wp_reset_postdata();
    echo beans_open_markup( 'tm_post_meta', 'div', array( 'class' => 'tm-post-meta uk-text-large uk-text-left uk-width-medium-4-5 uk-container-center') );
      echo beans_open_markup( 'tm_post_title', 'h4');
        echo $title;
      echo beans_close_markup( 'tm_post_title', 'h4' );
      echo beans_open_markup( 'tm_post_excerpt', 'p');
        echo $excerpt;
        echo ' '; // adds space between excerpt and link
          echo beans_open_markup( 'tm_post_link', 'a', array( 'href' => $link  ));
            echo 'Read more...';
          echo beans_close_markup( 'tm_post_link', 'a' );
      echo beans_close_markup( 'tm_post_meta', 'p' );
    echo beans_close_markup( 'tm_post_meta', 'div' );
    return;
  }

then i just use function inside html markup line this:

<?php get_post_meta_by_id( 45 );

Second function, if you want to use post_name instead of id (it depends on function above, so keep in mind that in project should be two functions):

function get_post_meta_by_name ( $post_name ) {
  global $wpdb;
  $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");
  return get_post_meta_by_id( $post_id );
}

Now, if you want to call function, you do this:

<?php get_post_meta_by_name( 'your_post_name' );

Mariusz

Edited 1 year, 5 months ago by Mariusz Wachowski


Thanks Mariusz for the reply, But actually i want to show something like this website . Can you please help me with that..??

Edited 1 year, 5 months ago by Anupam Saikia


Hey guys,

Regarding the post excerpt, you can apply the following snippet which will show the excerpt and read more link on the blog home and front page:


add_filter( 'the_content', 'example_post_excerpt' );

function example_post_excerpt( $content ) {

    // Return the excerpt() if it exists and if it is the home/fron page.
    if ( has_excerpt() && ( is_home() || is_front_page() ) ) {
        return '<p>' . get_the_excerpt() . '</p><p>' . beans_post_more_link() . '</p>';
    }

    return $content;
}

Here is the snippet to remove the post categories and tags below the content:

// Remove the post meta categories.
beans_remove_action( 'beans_post_meta_categories' );

// Remove the post meta tags.
beans_remove_action( 'beans_post_meta_tags' );

Regarding the post meta under the post title, Beans as a filter called beans_post_meta_items which filters the meta action called and their priority. If we look at the filter default value, it looks as such:

array(
    'date' => 10,
    'author' => 20,
    'comments' => 30
);

From there Beans will sort it by priority and loop through each using their key to do an action called beans_post_meta_{$key}. You may change the order or add your own action and callback if needed. This is a bit complex and I can't really explain it deeper so you will have to digg in the code if it doesn't make sense to you.

Alternatively, you could completely replace the post meta with your own, an example would be as follow:

beans_modify_action_callback( 'beans_post_meta', 'example_post_meta' );

function example_post_meta() {

    ?>
    <ul class="uk-article-meta uk-subnav uk-subnav-line">
        <li>Example meta 1</li>
        <li>Example meta 2</li>
        <li>Example meta 3</li>
    </ul>
    <?php

}

Hope that helps,


Hello Thierry,

Thank you for the snippet, I appreciate this. But i've already tried this code, This code shows excerpt only if it is availabvle I want to show except even when the excerpt is not specified or available. on all of the pages except the post page.

Could You help me with this? :)

And the snippet to remove the post categories and tags below the content worked like a charm, thank you so much for that snippet too.

and to show the categories i've done this and it worked.

$meta_items = apply_filters( 'beans_post_meta_items', array(
    'date' => 10,
    'author' => 20,
    'comments' => 30,
    'categories' => 40
) );

Edited 1 year, 5 months ago by Anupam Saikia


Hey Anupam, I use the following function. I think it's exactely what you need:

// Add expert and more link

add_filter( 'the_content', 'beans_child_modify_post_content' );

function beans_child_modify_post_content( $content ) {

    // Stop here if we are on a single view.
    if ( is_singular() )
        return $content;

    // Return the excerpt() if it exists other truncate.
    if ( has_excerpt() )
        $content = '<p>' . get_the_excerpt() . '</p>';
    else
        $content = '<p>' . wp_trim_words( get_the_content(), 40, '...' ) . '</p>';

    // Return content and readmore.
    return $content . '<p>' . beans_post_more_link() . '</p>';

}

Hey Jochen,

Thank you so much for the snippet it worked like a charm. Thank you so much. That is exactly what I needed. :)

Edited 1 year, 5 months ago by Anupam Saikia


Cool, I got so much help here from Thierry, this is the first time that I could help :)


I'm so lucky that I got in contact with you otherwise my problem would never be solved. Thank you again, And thank you Thierry for your solution. :)


Hi Thierry, is there a way, to get excerpt for a specific post by id or/and post name?

I wrote code (few post above), but when i saw your code i thought that maybe is an easier way :)

Mariusz


@anupam the code snippet you provided in this reply seem to be as if you where modifying it straight in Beans Core Theme. If it is the case, you cannot do that as it will be overwritten next time you update Beans. You should always make modifications in your child theme, here is the code to add to your child theme functions.php

add_filter( 'beans_post_meta_items', 'example_post_meta_items' );

function example_post_meta_items() {

    return array(
        'date' => 10,
        'author' => 20,
        'comments' => 30,
        'categories' => 40
    );

}

Marius you can use the following snippet to get a post excerpt by ID:

$post_excerpt = apply_filters( 'the_excerpt', get_post_field( 'post_excerpt', $post_id ) )

You would set $post_id with the post ID you are looking for.

Happy coding,


Hello Thierry,

Thank you for the response and the code, Actually i didn't modified the core theme, I did that in my child theme. And i was wondering how to show the fontawesome fonts in the meta section..??

Thanks.


You can use the icons very easely.

<!-- This is an icon -->
<i class="uk-icon-cog"></i>

<!-- This is an icon in a link -->
<a href="" class="uk-icon-hover uk-icon-github"></a>

See here: http://getuikit.com/docs/icon.html


Hey Jochen,

Yes i know that but i was wondering if that can be done in the function.php as i'm using

add_filter( 'beans_post_meta_items', 'HI_post_meta_items' );

function HI_post_meta_items() {

    return array(
        'author' => 10,
        'date' => 20,
        'categories' => 30,
        'comments' => 40
    );

}

Edited 1 year, 5 months ago by Anupam Saikia


As for every markup in Beans, you can add HTML using the hooks available (refer to this reply for more info).

For example if you wanted to add the clock icon before the date meta, you would use the following code snippet:

add_action( 'beans_post_meta_item[_date]_prepend_markup', 'example_post_meta_date_icon' );

function example_post_meta_date_icon() {

    ?><i class="uk-icon-clock-o uk-text-muted uk-margin-small-right"></i><?php

}

Hey Thierry,

That's what i was looking for, thank you so much.


Can one just comment out author like I did in the following code to remove/hide it?

add_filter( 'beans_post_meta_items', 'post_meta_items' );
function post_meta_items() {
    return array(
        'date' => 10,
        //'author' => 20,
        'comments' => 30,
        'categories' => 40,
        'tags' => 50
        );  
}

Or should I do like so (trying to simplify it..):

add_filter( 'beans_post_meta_items', 'post_meta_items' );
function post_meta_items($items) {
    return array(
        'date' => 10,
        //'author' => 20,
        'comments' => 30,
        'categories' => 40,
        'tags' => 50
        );

        unset( $items['author'] );
        return $items;

}

One more thing. Can one also add a code for last updated? Having older posts it would be great to also show when they were updated last. Something similar to this: http://www.wpbeginner.com/wp-tutorials/display-the-last-updated-date-of-your-posts-in-wordpress/ that updates the posted on post meta date.

Thanks! I am working on additional tutorials.

I will also add in a link to another forum post: https://community.getbeans.io/discussion/remove-author-from-meta-info/

Edited 3 months ago by Paal Joachim Romdahl

Write a reply

Login or register to write a reply, it's free!