最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Set default sort order to be by 'title' and posts per page to '100'

programmeradmin4浏览0评论

Need to change below code to have the default selection to be 'by title' and '100' per page

function iknow_posts_sorter() {
    if ( $_GET && ! empty( $_GET ) ) {
        iknow_go_filter();
    }

    $sorterby = ! empty( $_GET['select'] ) ? sanitize_text_field( wp_unslash( $_GET['select'] ) ) : '';


    $sorter_arr = array(
        'newest'   => esc_attr__( 'newest', 'iknow' ),
        'title'    => esc_attr__( 'by title', 'iknow' ),
        /*'comments' => esc_attr__( 'by comments', 'iknow' ),*/
    );

    $posts = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : 'default';

    $posts_arr = array(
        'default' => esc_attr__( 'Default', 'iknow' ),
        '20'      => '20 ' . esc_attr__( 'Per Page', 'iknow' ),
        '50'      => '50 ' . esc_attr__( 'Per Page', 'iknow' ),
        '100'     => '100 ' . esc_attr__( 'Per Page', 'iknow' ),

    );
    ?>
    <form method="get" id="order" class="level-right">
        <div class="level-item">
            <div class="field">
                <div class="control">
                    <div class="select is-small is-primary">
                        <select name="select" class="" onchange="this.form.submit();">
                            <?php
                            foreach ( $sorter_arr as $key => $val ) {
                                echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $sorterby, false ) . '>' . esc_attr( $val ) . '</option>';
                            }
                            ?>

                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="level-item">
            <div class="field">
                <div class="control">
                    <div class="select is-small is-primary">
                        <select name="per_page" class="" onchange="this.form.submit();">
                            <?php
                            foreach ( $posts_arr as $key => $val ) {
                                echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $posts, false ) . '>' . esc_attr( $val ) . '</option>';
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php

}

Need to change below code to have the default selection to be 'by title' and '100' per page

function iknow_posts_sorter() {
    if ( $_GET && ! empty( $_GET ) ) {
        iknow_go_filter();
    }

    $sorterby = ! empty( $_GET['select'] ) ? sanitize_text_field( wp_unslash( $_GET['select'] ) ) : '';


    $sorter_arr = array(
        'newest'   => esc_attr__( 'newest', 'iknow' ),
        'title'    => esc_attr__( 'by title', 'iknow' ),
        /*'comments' => esc_attr__( 'by comments', 'iknow' ),*/
    );

    $posts = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : 'default';

    $posts_arr = array(
        'default' => esc_attr__( 'Default', 'iknow' ),
        '20'      => '20 ' . esc_attr__( 'Per Page', 'iknow' ),
        '50'      => '50 ' . esc_attr__( 'Per Page', 'iknow' ),
        '100'     => '100 ' . esc_attr__( 'Per Page', 'iknow' ),

    );
    ?>
    <form method="get" id="order" class="level-right">
        <div class="level-item">
            <div class="field">
                <div class="control">
                    <div class="select is-small is-primary">
                        <select name="select" class="" onchange="this.form.submit();">
                            <?php
                            foreach ( $sorter_arr as $key => $val ) {
                                echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $sorterby, false ) . '>' . esc_attr( $val ) . '</option>';
                            }
                            ?>

                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="level-item">
            <div class="field">
                <div class="control">
                    <div class="select is-small is-primary">
                        <select name="per_page" class="" onchange="this.form.submit();">
                            <?php
                            foreach ( $posts_arr as $key => $val ) {
                                echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $posts, false ) . '>' . esc_attr( $val ) . '</option>';
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php

}
Share Improve this question edited Sep 14, 2020 at 17:58 vancoder 7,92428 silver badges35 bronze badges asked Sep 14, 2020 at 13:28 Jay CaetanoJay Caetano 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The current, or selected, values in your code are stored in $sorterby and $posts variables. The variable values are set by a ternary opertion - a one-line if-statement. The first part is the conditional check, the second part after question mark is the value setting, and after the colon is default value, which is used, if the conditional check fails / is false.

So just match the third part of the ternary operations with any key from the option arrays, $sorter_arr and $posts_arr, and you should be good to go.

$sorterby = ! empty( $_GET['select'] ) ? sanitize_text_field( wp_unslash( $_GET['select'] ) ) : 'title'; // title as default
$posts = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : '100'; // 100 as default

BUT, do note that you shouldn't make any direct changes to plugin or (parent) theme files (if the code above is from eihter one). Any changes you make will get wiped out next time the plugin or theme is updated. If the code is such that you can copy it to your child theme, then it is safe to edit and won't get overwritten when updates are installed.

发布评论

评论列表(0)

  1. 暂无评论