how to get specific page content

bellow is my code working nice but problem is don’t coming with html tag.. like etc… no idea why…

  <?php $recent = new WP_Query("page_id=2"); while($recent->have_posts()) : $recent->the_post();?>
    <?php 
    echo substr(get_the_excerpt(), 0,450);

     ?>
     <a href="<?php the_permalink() ?>" rel="bookmark">
             More About Us
              </a>

bellow is another code which is html tag and everything ok.. but i can’t figure out how to permalink do there.. the permalink i put there i not work.

Read More
<?php
$my_id = 2;
$page_id = get_post($my_id);
$content = $page_id->post_content;
echo substr($content, 0, 450);

?>
  <a href="<?php the_permalink() ?>" >More About Us</a>

also what is best way to get specific page content like bellow way

<h2>title</h2>
<div>featured image </div>
<div>content</div>
<a href="<?php the_permalink() ?>" rel="bookmark">

Related posts

2 comments

  1. Rather than call WP_Query() you can use get_post() and “set up” the global $post. This is probably a little more efficient than @tf’s answer, though the ideas are broadly the same.

    Please note, in both cases you should reset the post data afterwards.

    /**
     * Display the post content. Optionally allows post ID to be passed
     * @uses the_content()
     *
     * @param int $id Optional. Post ID.
     * @param string $more_link_text Optional. Content for when there is more text.
     * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
     */
    function sh_the_content_by_id( $post_id=0, $more_link_text = null, $stripteaser = false ){
        global $post;
        $post = &get_post($post_id);
        setup_postdata( $post, $more_link_text, $stripteaser );
        the_content();
        wp_reset_postdata();
    }
    

    Source: http://stephenharris.info/get-post-content-by-id/

  2. (get_)the_excerpt() strips HTML tags – that’s why you don’t have HTML tags. 😉

    Just use the_content() instead of the_excerpt() if you want to show the full content.

    $recent = new WP_Query("page_id=2");
    while ($recent->have_posts()) : $recent->the_post();
        echo '<a href="'.get_the_permalink().'" rel="bookmark">Permalink</a>';
        the_content();
    endwhile;
    wp_reset_postdata();
    

    As for the best way of outputting post data, you should have a look at this.

Comments are closed.