How to display only an excerpt of the content with custom post types?

<?php
/*
Template Name: second
*/
?>


<?php get_header(); ?>


<?php query_posts(array('post_type'=>'event')); ?>
    <?php if(have_posts()) while(have_posts()) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="entry">
        <div class="thumbnail"><?php the_post_thumbnail('full'); ?></div>
        <h1 class="title"><?php the_title(); ?></h1>
        <div class="content page">
            <?php the_content(); ?>
            <?php wp_link_pages(array('before' => '<div class="page-link">'.__('Pages', 'cpotheme').':', 'after' => '</div>')); ?>
        </div>
    </div>
    <?php endwhile; ?>


<?php get_sidebar(); ?>
<?php get_footer(); ?>

The page acts like an index.php What should I amend in order to make the title as the link to the full page and make display only an excerpt of the content?

Related posts

Leave a Reply

3 comments

  1. You only need two small changes (1st and 3rd lines), though I also took the liberty of tweaking the classes on the div to what seemed more appropriate:

    <h1 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <div class="excerpt event">
       <?php the_excerpt(); ?>
       <?php wp_link_pages(array('before' => '<div class="page-link">'.__('Pages', 'cpotheme').':', 'after' => '</div>')); ?>
    </div>
    
  2. You’ll need to form a html anchor tag using WordPress functions, here’s the example.

    Example –

    <h1 class="title">
        <a href="<?php the_permalink(); ?>" title="<?php printf( 'Permalink to %s', the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    </h1>
    

    And to display only excerpt there’s functions called the_excerpt()

  3. First off: Interesting Question!

    Second: you need to add the <!--nextpage--> tag in your post one or multiple times. Else it won’t work.

    The plugin:

    What this plugin does is intercepting the output of get_permalink(), which is called internally by wp_link_pages(). Inside get_permalink(), we got the get_post_permalink() function that returns for custom post types. There we find the 'post_type_link'-filter and this is where we hook in.

    Then we take the global $pages, which holds an array of the post content splitted by the <!--nextpage--> HTML comment. Our static variable is a counter that helps us determining which part we should return – based on the times the function was called (starting at 0).

    The last thing is that we append this part from $pages to our post link. As the esc_url() function intercepts our permalink and would mess up all our Uppercase letters, spaces, etc., we attach a single firing filter there too and just return the original string.

    <?php 
    /** Plugin Name: (#67750) »kaiser« Append excerpt to post pagination */
    
    /**
     * Callback for get_post_permalink() that is called by get_permalink() for
     * custom post types. We append the $GLOBALS['pages'][ $current ] excerpt here.
     * @param  string $post_link The original posts page link
     * @param  object $post The post object
     * @param  mixed boolean/string $leavename
     * @param  mixed boolean/string $sample A sample link
     * @return string $post_link
     */
    function wpse67750_pagination_excerpt( $post_link, $post, $leavename, $sample )
    {
        if (
            'YOUR_POST_TYPE' !== get_post_type()
            OR ! in_the_loop()
            OR ! is_singular()
        )
            return;
    
        static $n = 0;
        // The global pages array: Contains the current post type pages
        global $pages;
    
        // We need a callback to reserve the original appended string
        // not messed up by the esc_url() function.
        add_filter( 'clean_url', 'wpse67750_pagination_url_cb', 10, 3 );
    
        // The current page content
        $curr = $pages[ $n++ ];
    
        // Build the output
        // Wrap it up for easy targeting via CSS and JS
        // Also append a non visible single HTML tag to allow closing the still open anchor
        $output = sprintf(
             '"> %s <span class="excerpt" id="page-%s">%s</span><a style="display:none" rel="nofollow" href="#'
            ,$n
            ,$n
            ,trim( $curr )
        );
        /*$output = apply_filters(
             'the_excerpt'
            ,$output
        );*/
    
        // Append to the link
        return $post_link.$output;
    }
    add_filter( 'post_type_link', 'wpse67750_pagination_excerpt', 10, 4 );
    
    /**
     * The callback to preserve the string appended to the permalink
     * @param  string $url Escaped
     * @param  string $orig_url Not escaped
     * @param  string $context 'display'
     */
    function wpse67750_pagination_url_cb( $url, $orig_url, $context )
    {
        // Only run once
        remove_filter( current_filter(), __FUNCTION__ );
    
        // Return the non escaped original
        return $orig_url;
    }
    

    The solution is not completely perfect. I hadn’t got around getting rid of the first number and there seems to be a slight problem with retrieving the last link in some cases. It also had the problem, that core doesn’t offer a possibility to get rid of the appended </a> closing tag, so I had to add fake links there. Those don’t get displayed, won’t be followed by search engines and don’t harm. They’re just not beautiful, but that’s the only possibility.