display some portion of the wordpress post outside of wordpress blog

i have wordpress blog in my custom built website, i am displaying some posts from wordpress blog into my website based on tags like below

require('../news/wp-blog-header.php');
                            $query = new WP_Query('tag=Dalaman');

                            if ($query->have_posts()):
                                while ($query->have_posts()) : $query->the_post();
                                    ?>
                                    <h3> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                    <p><?php the_content();?></p>
                                    <?php
                                endwhile;
                            endif;

the_content displays the 10 posts from wordpress database based on WP_Query

Read More

Problem: i want to display some part of the post, let’s say 55 chracters
posts, in my db dont have excerpt by default and i dont want to use the_exerpt() as it strips html tags and my post contains <img> on start of every post

i tried many things but all went in vain,i also used php’s substr() function, but it didn’t work in this case.

so how can i display somepart of the post along with image ?

very much thanks.

Kind Regards !

Related posts

Leave a Reply

2 comments

  1. you can do it like below,

    $limit = 55;
                                $content = explode(' ', get_the_content(), $limit);
    
                                if (count($content) >= $limit) {
                                    array_pop($content);
                                    $content = implode(" ", $content) . '...';
                                } else {
                                    $content = implode(" ", $content);
                                }
                                $content = preg_replace('/[.+]/', '', $content);
                                $content = apply_filters('the_content', $content);
                                $content = str_replace(']]>', ']]&gt;', $content);
                                echo $content;
    
  2. http://codex.wordpress.org/Function_Reference/the_content

    I suggest you do what the article says and insert a <!--more--> at the breakpoints whenever – this is more safer than stripping an arbitrary amount of characters because you may break your html tags that way.

    If you’re not concerned about that, then instead of

    <?php the_content(); ?>
    

    do

    <?php
    $content = get_the_content(); //get the content as a string
    $content = substr($content, 0, 55); //cut the first 55 characters
    echo $content; //display it as usual
    ?>