【问题标题】:Wordpress use functions.php to run function only on homepageWordpress 使用 functions.php 仅在主页上运行功能
【发布时间】:2014-09-02 19:14:18
【问题描述】:

这是我的代码:

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);
  }

我想看到这个功能只在主页上工作。但不幸的是,这段代码无处不在(帖子、页面),并且不能显示全部内容。

这是应该反映在主页内容中的代码:

<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>

【问题讨论】:

    标签: php wordpress function show


    【解决方案1】:

    添加查看首页的条件。 is_home() 检查是否为博文索引,is_front_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);
      }
    

    【讨论】:

    • 不幸的是,这不起作用。由“Sahifa Home Builder”制作的主页,不是简单的博客文章或静态页面。
    • 好吧,有人吗?没有任何提示?
    猜你喜欢
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    相关资源
    最近更新 更多