I have 25+ posts that are all structured the same way, the content is like this:

<ul class="foo">
<p>lorem ipsum...</p>

they serve as single portfolio objects.

I want to show some of the content for each posts as a preview in the content-archive.php (like a portfolio grid). It works as far as that I have each post previewed with it's title, header picture etc. (things I can retrieve from WP), but I can either only display an excerpt (which looks bad, since each post starts with a list) or the whole post (which is a bloat and looks bad as well).

The the_expcerpt() function won't do, because I want it displayed like a list, the the_content() won't do because I want to lose the rest.

I only know that each and every posts starts with a <ul> with 3 or 4 <li> lements which are variable in length. Those I wan't to display, nothing more, nothing less

So my guess is to filter the the_content with a regular expression and display only the list elements?

1 Answer 1


There are two ways to achieve it.

$args = array(
                'post_type' => 'post',
   $mj_query = new WP_Query( $args );
  1. By the_expcerpt(), You can use excerpt field to display the content for post listing page.You can enable excerpt field by screen option through current post edit page.Just put html element to that excerpt field and use below code. It will display it as you want.

    while ( $mj_query->have_posts() ) {



  2. By get_the_content(), you can get all content of a specific post by get_the_content() function and alter it using substr() function.Here you can use length according to you content to be displayed.

    while ( $mj_query->have_posts() ) {
       $theExcerpt = get_the_content();
       echo substr($theExcerpt,0,100);


  • The problem with 1. is: I can't html around the expcerpt to display my de facto list content as a list, right? I could wrap a class around it and make it pink, but i can't wrap tags around each line, because i only get a string returned, with no more infomation about the lines. 2. I can't trim the content, because I don't know how long it is. I only know that each posts starts with a <ul> with 3 or 4 <li> elements which are variable in length. Those I wan't to display, nothing more, nothing less.
    – Elias
    Feb 11, 2017 at 9:34
  • As you have mentioned content layout in your question ,the answer ,I have written is appropriate.There should be a generic layout for any content type listing and there should be also a estimated length of content also.I think you don't have clear requirement. Feb 13, 2017 at 20:58
  • okay method 1 works, thanks. But it is a lot of work, I would still appreciate a cleaner solution just out of curiousity.
    – Elias
    Feb 16, 2017 at 19:40

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.