【问题标题】:How to insert php snippet into Parent theme file from child theme functions.php如何从子主题functions.php将php片段插入父主题文件
【发布时间】:2021-08-18 14:03:46
【问题描述】:

我的父主题有一个我无法从子主题覆盖的 php 文件 (/inc/mega-menu.php)(这将是最简单的事情,但我在这里找到的解决方案无效)

在这个文件中有一段代码:

<div class="cs-entry__inner cs-entry__content">

    <?php csco_get_post_meta( array( 'category' ), false, true, 'mega_menu_post_meta' ); ?>

    <?php the_title( '<h6 class="cs-entry__title"><a href="' . esc_url( get_permalink() ) . '">', '</a></h6>' ); ?>

    <?php csco_get_post_meta( array( 'author', 'date', 'views', 'shares', 'comments' ), false, true, 'mega_menu_post_meta' ); ?>

</div>

这会在使用时为超级菜单创建菜单项。我想在标题下方添加_excerpt,以便用户可以在菜单项中添加一些文本......但我不知道如何将其插入那里,因为我无法覆盖父主题文件,并且不要'不想直接修改。

理想情况下应该是这样的:

<div class="cs-entry__inner cs-entry__content">

    <?php csco_get_post_meta( array( 'category' ), false, true, 'mega_menu_post_meta' ); ?>

    <?php the_title( '<h6 class="cs-entry__title"><a href="' . esc_url( get_permalink() ) . '">', '</a></h6>' ); ?>

    <p class="cs-entry__excerpt">
        <?php the_excerpt(); ?>
    </p>

    <?php csco_get_post_meta( array( 'author', 'date', 'views', 'shares', 'comments' ), false, true, 'mega_menu_post_meta' ); ?>

</div>

在 the_title 下方添加了 cs-entry__excerpt div。

这可能吗?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    我不确定这是不是最好的方法。但是您如何看待向the_title 添加挂钩?

    添加mega menu 帖子类型并使用此代码。

    add_filter('the_title', 'add_excerpt_to_mega_menu', 20, 2);
    
    function change_woocommerce_single_title($title, $id) {
       $post_type = get_post_type($id);
    
       if ( $post_type == 'mega_menu') // you should change this line
          $title = $title . '<p class="cs-entry__excerpt">' . get_the_excerpt($id) . '</p>';
          
        return $title;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2018-04-03
      • 2015-12-09
      • 1970-01-01
      • 2016-08-20
      • 2017-01-16
      相关资源
      最近更新 更多