Forums

post excerpt (7 posts)

  1. kikko088
    Member
    Posted 1 year ago #

    i try to follow this wordpress wiki http://codex.wordpress.org/Function_Reference/get_page
    for put on my home page the excerpt of some post, with this code i put on my home page the content

    <?php
    $page_id = 424;
    $page_data = get_page( $page_id );
    $content = $page_data->post_content;
    $title = $page_data->post_title;
    echo $page_data->post_content;
    ?>

    then with (theorically) I put the excerpt on the home page, but nothing,

    <?php
    $page_id = 424;
    $page_data = get_page( $page_id );
    $content = $page_data->post_content;
    $title = $page_data->post_title;
    $excerpt = $page_data->post_excerpt;
    echo $page_data->post_excerpt;
    ?>

    where is the mistake?the function is in the codex so it should go :(

    kikko088

  2. Mark / t31os
    Moderator
    Posted 1 year ago #

    Does the page have an excerpt entered into the excerpt field?

    If there's nothing in the excerpt field for that page, you'll see just that, nothing!.. :)

    You could use something like this instead..

    <?php global $post; $myposts = get_posts('post_type=page&include=424'); ?>
    <?php foreach( $myposts as $post ) : setup_postdata( $post ); ?>
        <?php the_excerpt(); ?>
    <?php endforeach; wp_reset_query(); ?>

    http://codex.wordpress.org/Template_Tags/get_posts

    Using get_posts() and setup_postdata() should allow the use of the_excerpt() which will return content even if you've not used the excerpt field.. ;)

    NOTE: Despite the name, get_posts() can return any post_type, not just posts.. (and it supports more parameters then the get_page eqivalent).

  3. kikko088
    Member
    Posted 1 year ago #

    yes i create an excerpt field with some word but nothing, your code is perfect, now I have to add the possibility to have random subpage of a page.
    I don't know that get_post return also page, i think that return only posts, thank you very much!

    excuse me for my bad english :)
    kikko088

  4. kikko088
    Member
    Posted 1 year ago #

    now i put on the code the parent and random,but i have only a question, to have the link to complete post instead the "[...]" what I have to do?
    thank you very much.

    kikko088

  5. Kapil Chugh
    Member
    Posted 1 year ago #

    add this code in your functions.php

    add_filter('wp_trim_excerpt', custom_excerpt);
    function custom_excerpt($text) {
      $text = str_replace('[...]', '<a href="' . get_permalink() .'">..complete post</a>', $text);
      return $text;
    }
  6. kikko088
    Member
    Posted 1 year ago #

    thank you!

  7. raskull
    Member
    Posted 1 year ago #

    In WP 2.9+ you can use excerpt_more instead of wp_trim_excerpt:

    add_filter('excerpt_more', 'custom_excerpt');
    function custom_excerpt($custom) {
    	return '<a href="' . get_permalink() .'">..complete post</a>';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags