需要更改以下代码,使默认选择为每页“按标题”和“100”
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
}
SO网友:Antti Koskinen
代码中的当前值或选定值存储在$sorterby
和$posts
变量。变量值由三元操作(一行if语句)设置。第一部分是条件检查,问号后的第二部分是值设置,冒号后是默认值,如果条件检查失败/为false,则使用默认值。
所以只需将三元运算的第三部分与选项数组中的任何键进行匹配,$sorter_arr
和$posts_arr
, 你应该准备好了。
$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, 请注意,您不应该对插件或(父)主题文件进行任何直接更改(如果上面的代码来自Either one)。下次更新插件或主题时,您所做的任何更改都将被删除。如果代码可以将其复制到子主题,则可以安全地进行编辑,并且在安装更新时不会被覆盖。