admin管理员组文章数量:1323023
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
1 Answer
Reset to default 0The 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.
本文标签: categoriesSet default sort order to be by 39title39 and posts per page to 3910039
版权声明:本文标题:categories - Set default sort order to be by 'title' and posts per page to '100' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742112797a2421328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论