diff --git a/src/block/posts/edit.js b/src/block/posts/edit.js index 22fb77492d..5cd2a086d7 100644 --- a/src/block/posts/edit.js +++ b/src/block/posts/edit.js @@ -107,6 +107,7 @@ const Edit = props => { taxonomyType = 'category', taxonomy = '', taxonomyFilterType = '__in', + taxonomyTypeToDisplay = 'category', contentOrder = DEFAULT_ORDER, uniqueId, } = attributes @@ -189,6 +190,7 @@ const Edit = props => { taxonomyType={ taxonomyType } taxonomy={ taxonomy } taxonomyFilterType={ taxonomyFilterType } + taxonomyTypeToDisplay={ taxonomyTypeToDisplay } blockStyle={ blockStyle } focalPointPlaceholder={ focalPointPlaceholder } /> @@ -319,6 +321,8 @@ const InspectorControls = memo( props => { onChangeTaxonomy={ taxonomy => props.setAttributes( { taxonomy } ) } taxonomyFilterType={ props.taxonomyFilterType } onChangeTaxonomyFilterType={ taxonomyFilterType => props.setAttributes( { taxonomyFilterType } ) } + taxonomyTypeToDisplay={ props.taxonomyTypeToDisplay } + onChangeTaxonomyTypeToDisplay={ taxonomyTypeToDisplay => props.setAttributes( { taxonomyTypeToDisplay } ) } stkVersion="3" /> { showProNotice && } diff --git a/src/block/posts/index.php b/src/block/posts/index.php index abccf61329..b4e7a6b9b1 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -67,7 +67,10 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t // Category. if ( strpos( $new_template, '!#category!#' ) !== false ) { - $category = Stackable_Posts_Block::get_category_list_by_id( $post_id ); + // If taxonomyTypeToDisplay is set, display that taxonomy instead of category. + $taxonomy_type = isset( $attributes['taxonomyTypeToDisplay'] ) ? $attributes['taxonomyTypeToDisplay'] : 'category'; + $category = Stackable_Posts_Block::get_taxonomy_term_list_by_id( $post_id, $taxonomy_type ); + if ( $category_highlighted ) { preg_match_all( '/]*>([^<]*)<\/a>/', $category, $matches ); foreach ( $matches[0] as $i=>$match ) { @@ -442,14 +445,35 @@ public static function get_excerpt_by_post_id( $post_id, $post = null, $max_exce } /** - * Get the category list by post id + * Get the taxonomy term list by post id * - * @param string post id + * @param string $post_id + * @param string $taxonomy * - * @return string the category list + * @return string the taxonomy term list */ - public static function get_category_list_by_id( $id ) { - return get_the_category_list( esc_html__( ', ', STACKABLE_I18N ), '', $id ); + public static function get_taxonomy_term_list_by_id( $post_id, $taxonomy ) { + if ( $taxonomy === 'category' ) { + return get_the_category_list( esc_html__( ', ', STACKABLE_I18N ), '', $post_id ); + } + + $terms = get_the_terms( $post_id, $taxonomy ); + + if ( is_wp_error( $terms ) || empty( $terms ) ) { + return ''; + } + + $separator = esc_html__( ', ', STACKABLE_I18N ); + $term_links = array(); + + foreach ( $terms as $term ) { + $term_link = get_term_link( $term, $taxonomy ); + if ( ! is_wp_error( $term_link ) ) { + $term_links[] = '' . esc_html( $term->name ) . ''; + } + } + + return implode( $separator, $term_links ); } /** @@ -559,7 +583,11 @@ public function get_posts( $request ) { $query->posts[$key]->comments_num = $this->get_comments_number( $post_array ); $query->posts[$key]->author_info = $this->get_author_info( $post_array ); - $query->posts[$key]->category_list = $this->get_category_list_by_id( $post->ID ); + + // If taxonomy_type_to_display is set, get terms from that taxonomy instead of category. + $taxonomy_type = isset( $args['taxonomy_type_to_display'] ) ? $args['taxonomy_type_to_display'] : 'category'; + $query->posts[$key]->category_list = $this->get_taxonomy_term_list_by_id( $post->ID, $taxonomy_type ); + $query->posts[$key]->featured_image_urls = $this->get_featured_image_urls_from_attachment_id( get_post_thumbnail_id($post->ID) ); } diff --git a/src/block/posts/schema.js b/src/block/posts/schema.js index 93248015f6..0b18533316 100644 --- a/src/block/posts/schema.js +++ b/src/block/posts/schema.js @@ -312,6 +312,17 @@ export const attributes = ( version = VERSION ) => { versionDeprecated: '', } ) + attrObject.add( { + attributes: { + taxonomyTypeToDisplay: { + type: 'string', + default: 'category', + }, + }, + versionAdded: '3.19.8', + versionDeprecated: '', + } ) + return attrObject.getMerged( version ) } diff --git a/src/components/taxonomy-control/index.js b/src/components/taxonomy-control/index.js index 16063c177c..087bcec6a9 100644 --- a/src/components/taxonomy-control/index.js +++ b/src/components/taxonomy-control/index.js @@ -149,6 +149,7 @@ class TaxonomyControl extends Component { this.props.onChangePostType( value ) this.props.onChangeTaxonomyType( taxonomyTypesAvailable.length ? taxonomyTypesAvailable[ 0 ] : '' ) this.props.onChangeTaxonomy( '' ) + this.props.onChangeTaxonomyTypeToDisplay( taxonomyTypesAvailable.length ? taxonomyTypesAvailable[ 0 ] : '' ) } } default="post" /> @@ -194,6 +195,19 @@ class TaxonomyControl extends Component { /> } + { taxonomyTypeOptions.length > 0 && + { + this.props.onChangeTaxonomyTypeToDisplay( value ) + } } + default={ taxonomyTypeOptions[ 0 ]?.value || 'category' } + help={ __( 'Taxonomy to display instead of Category.', i18n ) } + /> + } ) } diff --git a/src/hooks/use-posts-query.js b/src/hooks/use-posts-query.js index 60a2860c23..efdba9bd2d 100644 --- a/src/hooks/use-posts-query.js +++ b/src/hooks/use-posts-query.js @@ -28,6 +28,7 @@ export const usePostsQuery = attributes => { taxonomyType, taxonomy, taxonomyFilterType, + taxonomyTypeToDisplay, postOffset, postExclude, postInclude, @@ -47,6 +48,7 @@ export const usePostsQuery = attributes => { orderby: [ orderBy, 'ID' ].join( ' ' ), posts_per_page: numberOfItems, // eslint-disable-line camelcase max_excerpt: excerptLength, // eslint-disable-line camelcase + taxonomy_type_to_display: taxonomyTypeToDisplay, // eslint-disable-line camelcase }, attributes ), }, value => { // Exludes and includes can be empty. @@ -82,6 +84,7 @@ export const usePostsQuery = attributes => { taxonomyType, taxonomy, taxonomyFilterType, + taxonomyTypeToDisplay, postOffset, postExclude, postInclude,