WordPress use functions.php to run function only on homepage

Here is my code:

add_filter("the_content", "plugin_myContentFilter", "tie_excerpt_home_length");
  function plugin_myContentFilter($content)
  {
      $content = preg_replace("/<img[^>]+>/i", "(kép)", $content); 
    // Take the existing content and return a subset of it
    return substr($content, 0, 300);
  }

I would like to see this function work only the homepage. But unfortunatly this code is working everywhere (posts, pages), and do not let to display the whole content.

Read More

Here is the code which should be reflected in the content on the homepage:

<li <?php tie_post_class('first-news'); ?>>
                        <div class="inner-content">
                        <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>         
                            <div class="post-thumbnail">
                                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                                    <?php tie_thumb( 'tie-medium' ); ?>
                                    <span class="overlay-icon"></span>
                                </a>
                            </div><!-- post-thumbnail /-->
                        <?php endif; ?>
                        <h2 class="post-box-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                            <?php get_template_part( 'includes/boxes-meta' ); ?>

                            <div class="entry">
                                <?php if($_eventcat) the_content(); else tie_excerpt_home() ?>
                                <a class="more-link" href="<?php the_permalink() ?>"><?php _e( 'Tovább >', 'tie' ) ?></a>
                            </div>
                        </div>
                    </li>

Related posts

Leave a Reply

1 comment

  1. Add the conditional for checking the home page. is_home() checks if it’s the blog post index, is_front_page() checks if it’s the frontpage (blog post or static page);

    if ( is_home() || is_front_page() ) {
        add_filter("the_content", "plugin_myContentFilter", "tie_excerpt_home_length");
    }
    function plugin_myContentFilter($content)
      {
          $content = preg_replace("/<img[^>]+>/i", "(kép)", $content); 
        // Take the existing content and return a subset of it
        return substr($content, 0, 300);
      }