WP作业管理器类别下拉列表;通过筛选器更改占位符文本

时间:2016-10-19 作者:Craig Watson

我已经在谷歌上搜索了几个小时,但没有真正有用的结果,我有一个使用WP Job Manager的网站。此插件附带的搜索字段之一是类别下拉列表。

我想更改此文本的占位符,从“选择类别…”“选择您的扇区…”

我看到了这篇帖子:https://wordpress.org/support/topic/changing-placeholder-text-for-category-dropdown/

这就解释了这是可以做到的。

下面的代码取自WP Job Manager插件,供上面的链接参考。

function job_manager_dropdown_categories( $args = \'\' ) {
    $defaults = array(
        \'orderby\'         => \'id\',
        \'order\'           => \'ASC\',
        \'show_count\'      => 0,
        \'hide_empty\'      => 1,
        \'child_of\'        => 0,
        \'exclude\'         => \'\',
        \'echo\'            => 1,
        \'selected\'        => 0,
        \'hierarchical\'    => 0,
        \'name\'            => \'cat\',
        \'id\'              => \'\',
        \'class\'           => \'job-manager-category-dropdown \' . ( is_rtl() ? \'chosen-rtl\' : \'\' ),
        \'depth\'           => 0,
        \'taxonomy\'        => \'job_listing_category\',
        \'value\'           => \'id\',
        \'multiple\'        => true,
        \'show_option_all\' => false,
        \'placeholder\'     => __( \'Choose a category…\', \'wp-job-manager\' ),
        \'no_results_text\' => __( \'No results match\', \'wp-job-manager\' ),
        \'multiple_text\'   => __( \'Select Some Options\', \'wp-job-manager\' )
    );

    $r = wp_parse_args( $args, $defaults );

    if ( ! isset( $r[\'pad_counts\'] ) && $r[\'show_count\'] && $r[\'hierarchical\'] ){
        $r[\'pad_counts\'] = true;
    }

    // WPML & Polylang caching per language
    if ( defined( \'ICL_SITEPRESS_VERSION\' ) ) {
        $r[\'lang\'] = apply_filters( \'wpml_current_language\', NULL );
    } elseif ( function_exists( \'pll_current_language\' ) ) {
        $r[\'lang\'] = pll_current_language();
    }

    extract( $r );

// Store in a transient to help sites with many cats
    $categories_hash = \'jm_cats_\' . md5( json_encode( $r ) . WP_Job_Manager_Cache_Helper::get_transient_version( \'jm_get_\' . $r[\'taxonomy\'] ) );
    $categories      = get_transient( $categories_hash );

    if ( empty( $categories ) ) {
        $categories = get_terms( $taxonomy, array(
            \'orderby\'         => $r[\'orderby\'],
            \'order\'           => $r[\'order\'],
            \'hide_empty\'      => $r[\'hide_empty\'],
            \'child_of\'        => $r[\'child_of\'],
            \'exclude\'         => $r[\'exclude\'],
            \'hierarchical\'    => $r[\'hierarchical\']
        ) );
        set_transient( $categories_hash, $categories, DAY_IN_SECONDS * 30 );
    }

    $name       = esc_attr( $name );
    $class      = esc_attr( $class );
    $id         = $id ? esc_attr( $id ) : $name;

    $output = "<select name=\'" . esc_attr( $name ) . "[]\' id=\'" . esc_attr( $id ) . "\' class=\'" . esc_attr( $class ) . "\' " . ( $multiple ? "multiple=\'multiple\'" : \'\' ) . " data-placeholder=\'" . esc_attr( $placeholder ) . "\' data-no_results_text=\'" . esc_attr( $no_results_text ) . "\' data-multiple_text=\'" . esc_attr( $multiple_text ) . "\'>\\n";

    if ( $show_option_all ) {
        $output .= \'<option value="">\' . esc_html( $show_option_all ) . \'</option>\';
    }

    if ( ! empty( $categories ) ) {
        include_once( JOB_MANAGER_PLUGIN_DIR . \'/includes/class-wp-job-manager-category-walker.php\' );

        $walker = new WP_Job_Manager_Category_Walker;

        if ( $hierarchical ) {
            $depth = $r[\'depth\'];  // Walk the full depth.
        } else {
            $depth = -1; // Flat.
        }

        $output .= $walker->walk( $categories, $depth, $r );
    }

    $output .= "</select>\\n";

    if ( $echo ) {
        echo $output;
    }

    return $output;
}
如果您能提供有关如何创建过滤器以更改占位符文本的建议,我将不胜感激。

克雷格

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

根据我所做的测试,占位符字符串选择一个类别…不能用submit_job_form_fields 过滤器,但仍有方法更改该字符串。

更改文本的一种方法是覆盖插件的默认值job-filters.php 使用自己的模板。

复制默认模板,/wp-job-manager/templates/job-filters.php 转到您的主题:/your-theme/job_manager/job-filters.php

编辑主题的副本job-filters.php 通过添加placeholder 两个调用的参数job_manager_dropdown_categories():

<?php wp_enqueue_script( \'wp-job-manager-ajax-filters\' ); ?>

<?php do_action( \'job_manager_job_filters_before\', $atts ); ?>

<form class="job_filters">
    <?php do_action( \'job_manager_job_filters_start\', $atts ); ?>

    <div class="search_jobs">
        <?php do_action( \'job_manager_job_filters_search_jobs_start\', $atts ); ?>

        <div class="search_keywords">
            <label for="search_keywords"><?php _e( \'Keywords\', \'wp-job-manager\' ); ?></label>
            <input type="text" name="search_keywords" id="search_keywords" placeholder="<?php esc_attr_e( \'Keywords\', \'wp-job-manager\' ); ?>" value="<?php echo esc_attr( $keywords ); ?>" />
        </div>

        <div class="search_location">
            <label for="search_location"><?php _e( \'Location\', \'wp-job-manager\' ); ?></label>
            <input type="text" name="search_location" id="search_location" placeholder="<?php esc_attr_e( \'Location\', \'wp-job-manager\' ); ?>" value="<?php echo esc_attr( $location ); ?>" />
        </div>

        <?php if ( $categories ) : ?>
            <?php foreach ( $categories as $category ) : ?>
                <input type="hidden" name="search_categories[]" value="<?php echo sanitize_title( $category ); ?>" />
            <?php endforeach; ?>
        <?php elseif ( $show_categories && ! is_tax( \'job_listing_category\' ) && get_terms( \'job_listing_category\' ) ) : ?>
            <div class="search_categories">
                <label for="search_categories"><?php _e( \'Category\', \'wp-job-manager\' ); ?></label>
                <?php if ( $show_category_multiselect ) : ?>
                    <?php job_manager_dropdown_categories( array( \'placeholder\' => __( \'Choose a sector&hellip;\', \'your-text-domain\' ), \'taxonomy\' => \'job_listing_category\', \'hierarchical\' => 1, \'name\' => \'search_categories\', \'orderby\' => \'name\', \'selected\' => $selected_category, \'hide_empty\' => false ) ); ?>
                <?php else : ?>
                    <?php job_manager_dropdown_categories( array( \'placeholder\' => __( \'Choose a sector&hellip;\', \'your-text-domain\' ), \'taxonomy\' => \'job_listing_category\', \'hierarchical\' => 1, \'show_option_all\' => __( \'Any category\', \'wp-job-manager\' ), \'name\' => \'search_categories\', \'orderby\' => \'name\', \'selected\' => $selected_category, \'multiple\' => false ) ); ?>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <?php do_action( \'job_manager_job_filters_search_jobs_end\', $atts ); ?>
    </div>

    <?php do_action( \'job_manager_job_filters_end\', $atts ); ?>
</form>

<?php do_action( \'job_manager_job_filters_after\', $atts ); ?>

<noscript><?php _e( \'Your browser does not support JavaScript, or it is disabled. JavaScript must be enabled in order to view listings.\', \'wp-job-manager\' ); ?></noscript>
另一种改变类别的方法是选择一个类别&hellip文本将使用gettext 过滤器:

add_filter(\'gettext\', \'wpse243242_change_wp_job_manager_text\', 20, 3 );
function wpse243242_change_wp_job_manager_text( $translated_text, $untranslated_text, $domain ) {
    if ( \'wp-job-manager\' !== $domain ) {
        return $translated_text;        
    }

    // make the changes to the text
    switch( $untranslated_text ) {
            // Multi category select
            case \'Choose a category&hellip;\':
                $translated_text = __( \'Choose a sector&hellip;\', \'text_domain\' );
            break;

            // Single category select
            case \'Any category\':
                $translated_text = __( \'Any sector\', \'text_domain\' );
            break;

            // add more items
     }

    return $translated_text;        
}

相关推荐

如果WP_QUERY中有数据,如何签入Functions.php?

我在主页上有两个不同的WP查询。我需要检查函数中的$home\\u查询条件。php然后,如果$home\\u query返回null,我想做些什么。我尝试了pre\\u get\\u posts操作,但我无法捕获$home\\u查询数据,而且它运行了两次,因为页面上有两个WP\\u查询。使用If$home\\u query返回null的分页方式,我想将页面重定向到404页面并获取404响应的代码。例如https://example.com/page/200/ 页码是200,但在页面中没有200页码,我想重