2016-10-01 127 views
1

我需要更改一个主题组件的模板中的一个字符串。可以说这是在header.php中。在我的孩子主题中,我创建了一个header.php文件来覆盖父母的主题header.php。Wordpress儿童主题非常小变化

我的问题是我在header.php中写什么?我是否复制粘贴父母版本中的所有内容并更改字符串?如果'是',那么我如何确保我得到更新,因为现在和将来我都会使用这个版本的header.php,因为它总是会覆盖父版本。如果“不”比我做什么?其中一个文件只是一个模板,但我必须在hook.php文件中做一个小的字符串更改......我该如何处理?

示例:这是来自父母主题的示例文件。我想改变的是“阅读更多”到“更多更好”。

<?php 
if (! class_exists('WPBakeryShortCode')) { 
    return; 
} 

class WPBakeryShortCode_UpsideGridService extends WPBakeryShortCode { 

    protected function content($atts, $content = null) { 
     ob_start(); 
     $atts = vc_map_get_attributes($this->getShortcode(), $atts); 
     extract($atts); 

     $up_tags = array(); 
     if (!empty($tag_slugs)) { 
      $up_tags = explode(',', $tag_slugs); 
     } 

     $query = array(
      'post_type'  => array('service'), 
      'posts_per_page' => (int)$total_item, 
      'post_status' => array('publish') 
     ); 

     if ($up_tags) { 
      $query['tax_query'] = array(
       array(
        'taxonomy' => 'service-tag', 
        'field' => 'slug', 
        'terms' => $up_tags 
       ), 
      ); 
     } 

     $results = new WP_Query($query); 
     if ($results->have_posts()) : 
      switch ($element_layout) { 

       case 'three_items_small_size': 
        $widget_before = '<div class="kopa-ab"><div class="wrapper"><div class="row"><div class="col-md-12"><div class="widget kopa-intro-3-widget">'; 
        $widget_after = '</div></div></div></div></div>'; 
        $widget_head = $this->the_head($element_layout, $title, $content); 
        $widget_before_loop = '<div class="widget-content"><ul class="row ul-mh">'; 
        $widget_after_loop = '</ul></div>'; 
        break; 

       case 'four_items': 
        $widget_before = '<div class="widget kopa-service-1-widget kopa-area-dark">'; 
        $widget_after = '</div>'; 
        $widget_head = $this->the_head($element_layout, $title, $content); 
        $widget_before_loop = '<div class="widget-content"><ul class="ul-mh row">'; 
        $widget_after_loop = '</ul></div>'; 
        break; 

       case 'three_items_big_size': 
        $widget_before = '<div class="widget kopa-service-2-widget">'; 
        $widget_after = '</div>'; 
        $widget_head = $this->the_head($element_layout, $title, $content); 
        $widget_before_loop = '<div class="widget-content"><ul class="ul-mh row">'; 
        $widget_after_loop = '</ul></div>'; 
        break; 

       case 'five_items': 
        $widget_before = '<div class="widget kopa-service-3-widget">'; 
        $widget_after = '</div>'; 
        $widget_head = $this->the_head($element_layout, $title, $content); 
        $widget_before_loop = '<div class="widget-content"><div class="kopa-masonry-2"><ul class="kopa-masonry-wrap">'; 
        $widget_after_loop = '</ul></div></div>'; 
        break; 
      } 

      echo wp_kses_post($widget_before); 
       echo wp_kses_post($widget_head); 
        echo wp_kses_post($widget_before_loop); 
         while ($results->have_posts()) { 
          $results->the_post(); 
          $this->the_loop($element_layout, $element_width, $atts); 
         } 
         wp_reset_postdata(); 
        echo wp_kses_post($widget_after_loop); 
      echo wp_kses_post($widget_after); 
     endif; 

     $output = ob_get_clean(); 
     return $output; 
    } 

    public function the_loop($element_layout='four_items', $element_width='3', $atts = array()) { 
     extract($atts); 

     if ('5' == $element_width) { 
      $item_class = 'ms-item-2 col-custom-5'; 
     } else { 
      $item_class = sprintf('col-md-%s col-sm-%s col-xs-12', esc_attr($element_width), esc_attr($element_width)); 
     } 
     $service_linkto = get_post_meta(get_the_ID(), 'upside-kidhouse-linkto', true); 
     if (empty($service_linkto)) { 
      $service_linkto = get_the_permalink(); 
     } 
     $service_icon = get_post_meta(get_the_ID(), 'upside-kidhouse-icon', true); 
     if (false === strpos($service_icon, 'fa ')) { 
      $service_icon = 'fa ' . $service_icon; 
     } 
     $service_title = get_the_title(); 
     if (! isset($excerpt_length) || empty($excerpt_length)) { 
      $excerpt_length = 55; 
     } 
     $GLOBALS['upside_excerpt_length'] = (int) $excerpt_length; 
     add_filter('excerpt_length', 'upside_set_excerpt_length'); 
     switch ($element_layout) { 
      case 'three_items_small_size': ?> 
       <li class="<?php echo esc_attr($item_class); ?>"> 
        <article class="entry-item"> 
         <?php if (! empty($service_icon)) : ?> 
          <span class="kopa-icon-1 <?php echo esc_attr($service_icon); ?>"></span> 
         <?php endif; ?> 
         <div class="entry-content"> 
          <?php if (! empty($service_title)) : ?> 
           <h4 class="entry-title"><a href="<?php echo esc_url($service_linkto); ?>" title="<?php echo wp_kses_post($service_title); ?>"><?php echo wp_kses_post($service_title); ?></a></h4> 
          <?php endif; ?> 
          <?php the_excerpt(); ?> 
         </div> 
         <a href="<?php echo esc_url($service_linkto); ?>" class="more-link-1"><i class="fa fa-angle-right"></i></a> 
        </article> 
       </li> 
       <?php break; 
      case 'four_items': ?> 
        <li class="<?php echo esc_attr($item_class); ?>"> 
         <div class="sv-item-1"> 
          <?php if (! empty($service_title)) : ?> 
           <h4><a href="<?php echo esc_url($service_linkto); ?>"><?php echo wp_kses_post($service_title);?></a></h4> 
          <?php endif; 
          if (! empty($service_icon)) : 
          ?> 
           <div class="sv-icon-1"> 
            <a href="<?php echo esc_url($service_linkto); ?>"> 
             <span class="<?php echo esc_attr($service_icon); ?>"></span> 
            </a> 
           </div> 
          <?php endif; ?> 
          <?php the_excerpt(); ?> 
         </div> 
        </li> 
       <?php 
       break; 
      case 'three_items_big_size': 
      ?> 
       <li class="<?php echo esc_attr($item_class); ?>"> 
        <div class="kopa-item-wrap"> 
         <div class="kopa-item sv-item-2"> 
          <div class="sv-item-content"> 
           <?php if (! empty($service_icon)) : ?> 
            <span class="<?php echo esc_attr($service_icon); ?>"></span> 
           <?php endif; ?> 
           <?php if (! empty($service_title)) : ?> 
            <h5><a href="<?php echo esc_url($service_linkto); ?>" title="<?php echo wp_kses_post($service_title); ?>"><?php echo wp_kses_post($service_title); ?></a></h5> 
           <?php endif; 
            the_excerpt(); 
           ?> 

           <a class="kopa-btn-3" href="<?php echo esc_url($service_linkto); ?>"><?php esc_html_e('+ Read More', 'upside'); ?></a> 
          </div> 
         </div> 
        </div> 
       </li> 

      <?php 
       break; 

      case 'five_items': 
       ?> 
        <li class="<?php echo esc_attr($item_class); ?>"> 
         <article class="entry-item"> 
          <?php if (! empty($service_icon)) : ?> 
           <div class="entry-icon-1"> 
            <span class="<?php echo esc_attr($service_icon); ?>"></span> 
           </div> 
          <?php endif;?> 
          <h4 class="entry-title"><a href="<?php echo esc_url($service_linkto); ?>"><?php echo wp_kses_post($service_title); ?></a></h4> 
         </article> 
        </li> 
       <?php 
       break; 
     } 

     remove_filter('excerpt_length', 'upside_set_excerpt_length'); 
    } 

    public function the_head($element_layout='four_items', $title, $content) { 
     $output = ''; 
     switch ($element_layout) { 
      case 'three_items_small_size': 
      case 'four_items': 
      case 'three_items_big_size': 
       if (! empty($title) || ! empty($content)) { 
        $output .= '<header class="widget-hd-1">'; 
         if (! empty($title)) { 
          $output .= sprintf('<h3 class="widget-title s1">%s</h3>', esc_html($title)); 
         } 
         if (! empty($content)) { 
          $output .= sprintf('<p>%s</p>', wp_kses_post($content)); 
         } 
        $output .= '</header>'; 
       } 
       break; 
      case 'five_items': 
       if (! empty($title) || ! empty($content)) { 
        $output .= '<header class="widget-hd-1">'; 
        if (! empty($title)) { 
         $output .= sprintf('<h3 class="widget-title s4">%s</h3>', esc_html($title)); 
        } 
        if (! empty($content)) { 
         $output .= sprintf('<p>%s</p>', wp_kses_post($content)); 
        } 
        $output .= '</header>'; 
       } 
       break; 
     } 
     return $output; 
    } 
} 

add_action('vc_before_init', 'upside_map_grid_service'); 
function upside_map_grid_service() { 
    $grid_cols_list = array(
     array('label' => esc_attr__("6 items", 'upside'), 'value' => 2), 
     array('label' => esc_attr__("5 items", 'upside'), 'value' => 5), 
     array('label' => esc_attr__("4 items", 'upside'), 'value' => 3), 
     array('label' => esc_attr__("3 items", 'upside'), 'value' => 4), 
     array('label' => esc_attr__("2 items", 'upside'), 'value' => 6) 
    ); 

    vc_map(array(
     "name" => esc_attr__("Service Grid",'upside'), 
     "base" => "UpsideGridService", 
     'category' => esc_attr__('Upside', 'upside'), 
     'description' => esc_attr__('Display some services.', 'upside'), 
     "icon" => apply_filters('upside_k_get_shortcode_icon', 'upside-icon', 'UpsideGridService'), 
     "params" => array(
      array(
       "type" => "textfield", 
       "admin_label" => true, 
       "group" => esc_attr__("General", 'upside'), 
       "heading" => esc_attr__("Title", 'upside'), 
       "param_name" => "title", 
       "value" =>"", 
       "description" => '', 
      ), 
      array(
       "type" => "textarea", 
       "group" => esc_attr__("General", 'upside'), 
       "heading" => esc_attr__("Description", 'upside'), 
       "param_name" => "content", 
       "value" => "", 
       "description" => '', 
      ), 
      array(
       'type' => 'exploded_textarea', 
       "group" => esc_attr__("General", 'upside'), 
       'heading' => esc_attr__('Tag slugs', 'upside'), 
       'param_name' => 'tag_slugs', 
       'description' => esc_attr__('Enter tag slugs (Note: divide tag slugs with linebreaks (Enter)).', 'upside'), 
      ), 
      array(
       'type' => 'dropdown', 
       "group" => esc_attr__("General", 'upside'), 
       'heading' => esc_attr__('Display type', 'upside'), 
       'param_name' => 'element_layout', 
       'value' => array(
        array('label' => "Default four items per row", 'value' => 'four_items'), 
        array('label' => "Default three items per row with big size", 'value' => 'three_items_big_size'), 
        array('label' => "Default three items per row with small size", 'value' => 'three_items_small_size'), 
        array('label' => "Default five items per row", 'value' => 'five_items'), 
       ), 
       'std' => 'four_items', 
       'edit_field_class' => 'vc_col-sm-12 vc_column', 
       'description' => esc_attr__('Select layout to display.', 'upside'), 
      ), 
      array(
       "type" => "textfield", 
       "group" => esc_attr__("General", 'upside'), 
       "heading" => esc_attr__("Total items", 'upside'), 
       "param_name" => "total_item", 
       "value" =>"12", 
       "description" => '', 
      ), 
      array(
       'type' => 'dropdown', 
       "group" => esc_attr__("General", 'upside'), 
       'heading' => esc_attr__('Number items per row', 'upside'), 
       'param_name' => 'element_width', 
       'value' => $grid_cols_list, 
       'std' => '3', 
       'edit_field_class' => 'vc_col-sm-12 vc_column', 
       'description' => esc_attr__('Select number of services per row.', 'upside'), 
      ), 
      array(
       "type" => "textfield", 
       "group" => esc_attr__("General", 'upside'), 
       "heading" => esc_attr__("Number words of excerpt to show", 'upside'), 
       "param_name" => "excerpt_length", 
       "value" =>"55", 
       "description" => '', 
       'dependency' => array(
        'element' => 'element_layout', 
        'value' => array('four_items', 'three_items_big_size', 'three_items_small_size') 
       ) 
      ), 
     ) 
    )); 

} 

回答

1

如果您使用子主题的工作,你会希望所有的内容从原来的header.php文件父主题复制到文件同名的孩子的主题文件夹。在子主题文件夹中有header.php文件后,可以打开并编辑内容。

当您使用子主题时,您必须从父主题复制已更新的header.php文件,如果您不想松散自定义,则应该在您使用的标题之间进行比较子主题和父代的新更新标题。

一些可用的软件提供两个文件之间的比较选项来检测您所做的修改。

+0

所以孩子的主题工作流程是:我创建一个header.php文件,并把它放在相同的相对路径。我复制原始header.php的内容,更改文本并保存。当主题得到更新时,我比较我的header.php和新的。如果有我再次手动复制粘贴应用修改? – mp3por

0
  1. 在与您的父主题相同的目录中的wp主题文件夹中创建子文件夹,例如, MyTheme的胎
  2. 复制此目录中,但与该线的变化的完整的header.php
<a class="kopa-btn-3" href="<?php echo esc_url($service_linkto); ?>"><?php esc_html_e('+ Прочети Още', 'upside'); ?></a> 
  • Wp的覆盖所有.php's从与.php文件父主题,你必须在MyTheme的孩子

    更新:
  • 使用Wp的钩与功能的add_filter

    替代解决方案:

    1. 在MyTheme的孩子创建的functions.php
    2. 复制下面的代码到其中:
    <?php 
    /* Child theme functions file 
    */ 
    
    function custom_readMore($more) 
    { return '<a class="kopa-btn-3" href="<?php echo > > esc_url($service_linkto); ?>"><?php esc_html_e('+ Прочети Още', 'upside'); ?></a>'; } 
    
    add_filter('excerpt_more', 'custom_readMore'); 
        ?> 
    

    的功能。 PHP是一个例外,Wp总是首先查看父主题的functions.php 然后执行myTheme-child中的功能。

    myTheme-child 中的header.php将替换父级主题中的文件,因此您将失去父主题的更新。

    希望这会有所帮助!

    +0

    对。这是我迄今为止所做的。但是,如果我这样做,主题得到更新时会发生什么?如果主题创建者修改header.php来修复bug,我将不得不复制粘贴他们的新版本,这听起来非常不合算。 – mp3por

    +0

    好吧,我明白,那么你需要hook到functions.php。我更新了我的答案! –