diff --git a/src/functions/customizer/tainacan-single-item-page-related-items.php b/src/functions/customizer/tainacan-single-item-page-related-items.php index 5c9c527..7935573 100644 --- a/src/functions/customizer/tainacan-single-item-page-related-items.php +++ b/src/functions/customizer/tainacan-single-item-page-related-items.php @@ -85,6 +85,24 @@ if ( !function_exists('tainacan_interface_customize_register_tainacan_single_ite 'choices' => tainacan_get_single_item_related_items_layout_options() ) ); + /** + * Adds options to select a order for the related items list. + */ + $wp_customize->add_setting( 'tainacan_single_item_related_items_order', array( + 'type' => 'theme_mod', + 'capability' => 'edit_theme_options', + 'default' => 'title_asc', + 'transport' => 'refresh', + 'sanitize_callback' => 'tainacan_sanitize_single_item_related_items_order_options', + ) ); + $wp_customize->add_control( 'tainacan_single_item_related_items_order', array( + 'type' => 'select', + 'priority' => 3, // Within the section. + 'section' => 'tainacan_single_item_page_related_items', + 'label' => __( 'Sorting criteria for the related items query', 'tainacan-interface' ), + 'choices' => tainacan_get_single_item_related_items_order_options() + ) ); + /** * Allows setting max columns count on grid and list layout --------------------------------------------------------- */ @@ -182,3 +200,52 @@ if ( ! function_exists( 'tainacan_sanitize_single_item_related_items_layout_opti } endif; // tainacan_sanitize_single_item_related_items_layout_options + +if ( ! function_exists( 'tainacan_get_single_item_related_items_order_options' ) ) : + /** + * Retrieves an array of options for single item page related items sorting options for Tainacan Interface theme. + * + * Create your own tainacan_get_single_item_related_items_order_options() function to override + * in a child theme. + * + * @since Tainacan Interface theme + * + * @return array $option - a string with sorting options for displaying the related items query + */ + function tainacan_get_single_item_related_items_order_options() { + $related_items_order_options = array( + 'title_asc' => __( 'Title A-Z', 'tainacan-interface'), + 'title_desc' => __( 'Title Z-A', 'tainacan-interface'), + 'date_asc' => __( 'Latest created last', 'tainacan-interface'), + 'date_desc' => __( 'Latest created first', 'tainacan-interface'), + 'modified_asc' => __( 'Latest modified last', 'tainacan-interface'), + 'modified_desc' => __( 'Latest modified first', 'tainacan-interface') + ); + return $related_items_order_options; + } +endif; // tainacan_get_single_item_related_items_order_options + +if ( ! function_exists( 'tainacan_sanitize_single_item_related_items_order_options' ) ) : + /** + * Handles sanitization for Tainacan Interface theme item page related items section sorting options + * + * Create your own tainacan_sanitize_single_item_related_items_order_options() function to override + * in a child theme. + * + * @since Tainacan Interface theme + * + * @param string $option - a string with sorting options for displaying the related items query. + * @return string the selected option. + */ + function tainacan_sanitize_single_item_related_items_order_options( $option ) { + $related_items_order_options = tainacan_get_single_item_related_items_order_options(); + + if ( ! array_key_exists( $option, $related_items_order_options ) ) { + return 'title_asc'; + } + + return $option; + } +endif; // tainacan_sanitize_single_item_related_items_order_options + + diff --git a/src/template-parts/single-items-related-items.php b/src/template-parts/single-items-related-items.php index db7cb2d..7c3f38c 100644 --- a/src/template-parts/single-items-related-items.php +++ b/src/template-parts/single-items-related-items.php @@ -1,5 +1,17 @@
@@ -18,8 +30,8 @@ if ( function_exists('tainacan_the_related_items_carousel') && get_theme_mod('ta // 'collection_heading_class_name' => 'title-content-items', 'items_list_layout' => get_theme_mod( 'tainacan_single_item_related_items_layout', 'carousel' ), 'collection_heading_tag' => 'h3', - 'order' => 'asc', - 'ordeby' => 'title', + 'order' => $order, + 'orderby' => $order_by, 'dynamic_items_args' => [ 'max_columns_count' => get_theme_mod('tainacan_single_item_related_items_max_columns_count', 4) ],