Created
February 11, 2016 07:55
-
-
Save dingo-d/3d5f5e18b699a3cf172e to your computer and use it in GitHub Desktop.
A code to put in your customizer.php and in your functions.php of the wordpress to order your home and category posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
The below code goes in customizer.php of your theme | |
**/ | |
/** | |
------------------------------------------------------------ | |
SECTION: Post order on home page and index pages | |
------------------------------------------------------------ | |
**/ | |
$wp_customize->add_section('section_post_homepage', array( | |
'title' => esc_html__('Post order', 'mytheme'), | |
'priority' => 0, | |
)); | |
/** | |
Post Order | |
**/ | |
$wp_customize->add_setting('post_order', array( | |
'default' => '', | |
)); | |
$wp_customize->add_control( 'post_order', array( | |
'type' => 'select', | |
'label' => esc_html__( 'Post Order', 'mytheme' ), | |
'description' => esc_html__( 'Choose the post order on home and index pages', 'mytheme' ), | |
'choices' => array( | |
'ASC' => esc_html__('Ascending', 'mytheme'), | |
'DESC' => esc_html__('Descending', 'mytheme'), | |
), | |
'section' => 'section_post_homepage', | |
)); | |
/** | |
Post Order By | |
**/ | |
$wp_customize->add_setting('post_order_by', array( | |
'default' => '', | |
)); | |
$wp_customize->add_control( 'post_order_by', array( | |
'type' => 'select', | |
'label' => esc_html__( 'Post Order By', 'mytheme' ), | |
'description' => esc_html__( 'Order posts by on home and index pages', 'mytheme' ), | |
'choices' => array( | |
'none' => esc_html__('None', 'mytheme'), | |
'ID' => esc_html__('ID', 'mytheme'), | |
'author' => esc_html__('Author', 'mytheme'), | |
'title' => esc_html__('Title', 'mytheme'), | |
'name' => esc_html__('Name', 'mytheme'), | |
'date' => esc_html__('Date', 'mytheme'), | |
'rand' => esc_html__('Rand', 'mytheme'), | |
'comment_count' => esc_html__('Comment count', 'mytheme'), | |
), | |
'section' => 'section_post_homepage', | |
)); | |
/** | |
The below code goes in functions.php of your theme | |
**/ | |
/********* Post Order On Home And Index Pages ***********/ | |
add_action( 'pre_get_posts', 'mytheme_order_posts' ); | |
if (!function_exists('mytheme_order_posts')) { | |
function mytheme_order_posts( $query ) { | |
if ( $query->is_home() OR $query->is_category()){ | |
if($query->is_main_query() AND !is_admin() ) { | |
$query->set( 'order', get_theme_mod('post_order', 'DESC') ); | |
} | |
} | |
} | |
} | |
add_action( 'pre_get_posts', 'mytheme_orderby_posts' ); | |
if (!function_exists('mytheme_orderby_posts')) { | |
function mytheme_orderby_posts( $query ) { | |
if ( $query->is_home() OR $query->is_category()){ | |
if($query->is_main_query() AND !is_admin() ) { | |
$query->set( 'orderby', get_theme_mod('post_order_by', 'date') ); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment