Wordpress儿童主题变化非常小

时间:2016-10-01 19:12:48

标签: php wordpress

我需要在一个主题组件的模板中更改一个字符串。让我们说这是在header.php中。在我的子主题中,我创建了一个header.php文件来覆盖父主题header.php。

我的问题是我在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')
                )
            ),
        )
    ) );

}

2 个答案:

答案 0 :(得分:1)

如果您正在使用子主题,则需要将父主题中原始header.php文件中的所有内容复制到同名子主题文件夹中的文件。在您的子主题文件夹中有header.php文件后,您可以打开并编辑内容。

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

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

答案 1 :(得分:0)

  1. 在wp themes文件夹中创建与父主题相同的目录中的子文件夹,例如: MyTheme的胎
  2. 将完整的header.php复制到此目录中,但使用此更改行
  3. <a class="kopa-btn-3" href="<?php echo esc_url($service_linkto); ?>"><?php esc_html_e('+ Прочети Още', 'upside'); ?></a>
    
    1. Wp使用myTheme-child中的.php文件覆盖父主题中的所有.php

      更新
    2. 使用带有add_filter函数的Wp Hook的替代解决方案:

      1. 在myTheme-child中创建functions.php
      2. 将以下代码复制到其中:
      3.  <?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' );
          ?>
        

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

        myTheme-child 中的header.php将从父主题替换文件,因此您将从父主题中删除更新。

        希望这有帮助!