【问题标题】:How to extend sorting order of Team Members如何扩展团队成员的排序顺序
【发布时间】:2019-10-10 17:43:18
【问题描述】:

我正在使用 Business Lounge theme by RT themes 和 Elementor。

Wordpress 版本是最新的 (5.2.1)

在团队页面(演示:https://businesslounge-demo.rtthemes.com/our-team/)上有一个团队成员卡片列表。我想将团队成员的顺序更改为当前无法选择的选项。

团队成员列表使用简码 [staff_box]

在 Elementor 编辑模式下,我看起来像这样:

编辑:

编辑表单定义在
wp-content/plugins/businesslounge-extensions/inc/elementor-addons/staff.php

<?php
namespace Elementor;
// ...
class Widget_RT_Staff extends Widget_Base {
  // ...
  protected function _register_controls() {
    // ...
    $this->add_control(
      'list_orderby',
        [
          'label'     => esc_html_x( 'List Order By', 'Admin Panel','businesslounge' ),
          'description' => esc_html_x('Sorts the posts by this parameter', 'Admin Panel','businesslounge' ),    
          'type'      =>  Controls_Manager::SELECT,
          'default'    =>  "date",
          "options"    => array(
            'date' => esc_html_x('Date',"Admin Panel","businesslounge"),
            'author' => esc_html_x('Author',"Admin Panel","businesslounge"),
            'title' => esc_html_x('Title',"Admin Panel","businesslounge"),
            'modified' => esc_html_x('Modified',"Admin Panel","businesslounge"),
            'ID' => esc_html_x('ID',"Admin Panel","businesslounge"),
            'rand' => esc_html_x('Randomized',"Admin Panel","businesslounge"),                                  
          )
        ]
      );     
      // ...
  }
  // ...
}

Plugin::instance()->widgets_manager->register_widget_type( new Widget_RT_Staff() );
编辑表单在`wp-content/plugins/businesslounge-extensions/inc/editor/staff_box.php`中定义 像这样
<?php
vc_map(
    array(
        'base'        => 'staff_box',
        'name'        => _x( 'Team', 'Admin Panel','businesslounge' ),
        'icon'        => 'rt_theme rt_team',
        'category'    => array(_x( 'Content', 'Admin Panel','businesslounge' ), _x( 'Theme Addons', 'Admin Panel','businesslounge' )),
        'description' => _x( 'Displays team members', 'Admin Panel','businesslounge' ),
        'params'      => array(
// ...    
          array(
            'param_name'  => 'list_orderby',
            'heading'     => _x( 'List Order By', 'Admin Panel','businesslounge' ),
            "description" => _x("Sorts the posts by this parameter",'Admin Panel','businesslounge'),
            'type'        => 'dropdown',
            "value"       => array(
                                _x('Date','Admin Panel','businesslounge') => 'date',
                                _x('Author','Admin Panel','businesslounge') => 'author',
                                _x('Title','Admin Panel','businesslounge') => 'title',
                                _x('Modified','Admin Panel','businesslounge') => 'modified',
                                _x('ID','Admin Panel','businesslounge') => 'ID',
                                _x('Randomized','Admin Panel','businesslounge') => 'rand',
                            ),
            'save_always' => true
        ),
// ...

输出定义在
wp-content/plugins/businesslounge-extensions/inc/shortcodes/staff_box.php

像这样:

<?php
function rt_staff( $atts, $content = null ) { 
// ...

    //defaults
    extract(shortcode_atts(array(  
        "id"           => 'staff-'.rand(100000, 1000000), 
        "class"        => "", 
        "list_layout"  => "1/1", 
        "list_orderby" => "date",
        "list_order"   => "DESC",
        "ids"          => array(),
        "box_style"    => ""        
    ), $atts));

// ...

    //general query
    $args=array( 
        'post_status'    => 'publish',
        'post_type'      => 'staff',
        'orderby'        => $list_orderby,
        'order'          => $list_order,
        'showposts'      => 1000                                                            
    );
// ...
    $theQuery = query_posts($args);
// ...

我想做什么: 将选项 'post_name' 添加到选择框中,以便我可以按不同的字段对团队进行排序。我想添加 'Post name' =&gt; 'post_name',

如何在不更改原始源代码的情况下做到这一点?
我已经激活了 business_lounge 主题的子主题。
我需要为此定制扩展吗?

【问题讨论】:

  • 我对代码进行了更多调查,发现elementor有控件,我发布了错误的控件代码。

标签: wordpress wordpress-theming wordpress-5


【解决方案1】:

您必须修改原始代码。 除非作者将filter 应用于返回的值,或者使用action,否则您无法覆盖该函数。

正如盖尔在他的回答中指出的那样,您可以通过将原始代码复制到您的子主题的 functions.php 然后重命名函数来减轻丢失更新更改的打击 - 例如在添加您的修改之前到my_rt_staff()

但是,您仍然需要在插件中调用 my_rt_staff() 而不是 rt_stuff,并且您必须在插件更新时进行此更改,但您不会丢失代码。

(也许您可以在修改后的my_rt_staff() 方法中将默认短代码属性中的"list_orderby" =&gt; "date" 更改为默认"list_orderby" =&gt; "post_name",,以便它按名称而不是日期作为默认值排序)

但是,这在您的特定情况下没有多大帮助,因为您需要对控件本身进行理想的修改,在 Widget_RT_Staff 类中的 _register_controls() 方法上。您可以通过扩展 Widget_RT_Staff 来覆盖它,但您仍然需要调用您的新类,这会导致您修改插件代码。

没有看到 Widget_RT_Staff 类如何影响短代码,我不能确定这会起作用,但基于 rt_staff() 方法,如果你使用短代码作为 [staff_box orderby="post_name"] 你可能会得到你想要的结果而不必须触摸任何代码。

【讨论】:

  • 我可以通过在浏览器开发工具的控件选择框中添加一个新选项,选择该选项并保存来规避这个问题。该控件似乎没有验证。
  • 我想过直接更改简码,但 elementor 不知何故不适用于简码或不让我看到它们。
【解决方案2】:

您应该能够通过“覆盖”此处描述的功能来修改您的插件而不会失去更新能力:

https://stackoverflow.com/a/40856197/3623080

在您的子主题的functions.php中复制该函数,自定义并重命名它,使用它来代替原始函数。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    相关资源
    最近更新 更多