1

On my Wordpress blog I use excerpts a lot. On my homepage I want a "read more..." on the same line as the text(without CSS), however I do not want it on my news page like that. I found this code which does exactly what I want but it applies it to all the excerpts. How do I have it just do this on the Homepage?

function new_excerpt_more( $more ) {
    return ' <a class="read-more" href="'
        . get_permalink( get_the_ID() )
        . '">Read More...</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
BDGapps
  • 3,318
  • 10
  • 56
  • 75
  • I believe this is what you're looking for: http://stackoverflow.com/questions/4082662/multiple-excerpt-lengths-in-wordpress – mikowl Aug 28 '13 at 03:39
  • @MichaelElias I am not looking for different lengths, rather I want the read more on my home page but not on my news page – BDGapps Aug 28 '13 at 03:41

1 Answers1

2

This should work:

function new_excerpt_more($more) {
  if(is_home())
    $more = ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More...</a>';

  return $more;
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

EDIT: In your theme, before the loop, you should add a variable (a flag) to see if it's the page that you want:

$my_flag = is_page( array( id, id, id, id, 'page-name', 'page-slug' );

if( have_posts() ) :
  while( have_posts() ) :
    // Your code
  endwhile;
endif;

And in your function (new_excerpt_more), you must check it like this:

function new_excerpt_more($more) {
      global $my_flag;

      if(is_home() || $my_flag)
        $more = ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More...</a>';

      return $more;
    }
    add_filter( 'excerpt_more', 'new_excerpt_more' );

I haven't tested it, but it should work. However, isn't supposed that pages display single content? It would never display only the excerpt (of course, it will depend on your theme :P)

Documentation: is_home()

Skaparate
  • 489
  • 3
  • 14
  • what if it was the news page and the about page. Does this only work for home – BDGapps Aug 28 '13 at 06:15
  • @BDGapps If you want it on other pages, then you could use is_page('page_slug'). For specific categories, you could use is_category('category_slug'). See is_page (http://codex.wordpress.org/Function_Reference/is_page) and is_category (http://codex.wordpress.org/Function_Reference/is_category) docs. – Skaparate Aug 28 '13 at 16:49
  • how do i use is_page if i just have the id – BDGapps Aug 28 '13 at 17:15
  • Use the id, not a string: is_page(4); – Skaparate Aug 28 '13 at 18:07
  • Make sure you are using it before the loop, with a flag: $flag = is_page(4);, then within the loop use if($flag) – Skaparate Aug 28 '13 at 19:38