Adds option to set default sorting of items related to this.

This commit is contained in:
mateuswetah 2023-05-24 11:30:54 -03:00
parent f2bcb3342c
commit 0dd6424f43
2 changed files with 81 additions and 2 deletions

View File

@ -85,6 +85,24 @@ if ( !function_exists('tainacan_interface_customize_register_tainacan_single_ite
'choices' => tainacan_get_single_item_related_items_layout_options() '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 --------------------------------------------------------- * 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 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

View File

@ -1,5 +1,17 @@
<?php <?php
$order_option = get_theme_mod( 'tainacan_single_item_related_items_order', 'title_asc' );
$order_option_split = explode( '_', $order_option );
$order_by = $order_option_split[0] ? $order_option_split[0] : 'title';
$order = $order_option_split[1] ? $order_option_split[1] : 'asc';
if ( !in_array($order_by, [ 'title', 'date', 'modified' ]) )
$order_by = 'title';
if ( !in_array($order, [ 'asc', 'desc' ]) )
$order = 'asc';
if ( function_exists('tainacan_the_related_items_carousel') && get_theme_mod('tainacan_single_item_enable_related_items_section', true) && tainacan_has_related_items() ) : ?> if ( function_exists('tainacan_the_related_items_carousel') && get_theme_mod('tainacan_single_item_enable_related_items_section', true) && tainacan_has_related_items() ) : ?>
<div class="mt-3 tainacan-single-post"> <div class="mt-3 tainacan-single-post">
@ -18,8 +30,8 @@ if ( function_exists('tainacan_the_related_items_carousel') && get_theme_mod('ta
// 'collection_heading_class_name' => 'title-content-items', // 'collection_heading_class_name' => 'title-content-items',
'items_list_layout' => get_theme_mod( 'tainacan_single_item_related_items_layout', 'carousel' ), 'items_list_layout' => get_theme_mod( 'tainacan_single_item_related_items_layout', 'carousel' ),
'collection_heading_tag' => 'h3', 'collection_heading_tag' => 'h3',
'order' => 'asc', 'order' => $order,
'ordeby' => 'title', 'orderby' => $order_by,
'dynamic_items_args' => [ 'dynamic_items_args' => [
'max_columns_count' => get_theme_mod('tainacan_single_item_related_items_max_columns_count', 4) 'max_columns_count' => get_theme_mod('tainacan_single_item_related_items_max_columns_count', 4)
], ],