diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 9f25ad0a594b89..82a93ab0c35b15 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -639,7 +639,7 @@ Displays the next or previous post link that is adjacent to the current post. ([ - **Name:** core/post-navigation-link - **Category:** theme - **Supports:** color (background, link, text), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~ -- **Attributes:** arrow, label, linkLabel, showTitle, textAlign, type +- **Attributes:** arrow, inSameTerm, label, linkLabel, showTitle, taxonomy, textAlign, type ## Post Template diff --git a/packages/block-library/src/post-navigation-link/block.json b/packages/block-library/src/post-navigation-link/block.json index e1b6d4fa90a40c..61d42f0e55f201 100644 --- a/packages/block-library/src/post-navigation-link/block.json +++ b/packages/block-library/src/post-navigation-link/block.json @@ -28,8 +28,16 @@ "arrow": { "type": "string", "default": "none" + }, + "inSameTerm": { + "type": "boolean" + }, + "taxonomy": { + "type": "string", + "default": "" } }, + "usesContext": [ "postType" ], "supports": { "reusable": false, "html": false, diff --git a/packages/block-library/src/post-navigation-link/edit.js b/packages/block-library/src/post-navigation-link/edit.js index f899788428bcdb..464531f9d45461 100644 --- a/packages/block-library/src/post-navigation-link/edit.js +++ b/packages/block-library/src/post-navigation-link/edit.js @@ -10,6 +10,7 @@ import { __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOption as ToggleGroupControlOption, ToggleControl, + SelectControl, PanelBody, } from '@wordpress/components'; import { @@ -20,9 +21,20 @@ import { useBlockProps, } from '@wordpress/block-editor'; import { __, _x } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; export default function PostNavigationLinkEdit( { - attributes: { type, label, showTitle, textAlign, linkLabel, arrow }, + context: { postType }, + attributes: { + type, + label, + showTitle, + textAlign, + linkLabel, + arrow, + taxonomy, + }, setAttributes, } ) { const isNext = type === 'next'; @@ -47,6 +59,40 @@ export default function PostNavigationLinkEdit( { [ `has-text-align-${ textAlign }` ]: textAlign, } ), } ); + + const taxonomies = useSelect( + ( select ) => { + const { getTaxonomies } = select( coreStore ); + const filteredTaxonomies = getTaxonomies( { + type: postType, + per_page: -1, + context: 'view', + } ); + return filteredTaxonomies; + }, + [ postType ] + ); + const getTaxonomyOptions = () => { + const selectOption = { + label: __( 'Unfiltered' ), + value: '', + }; + const taxonomyOptions = ( taxonomies ?? [] ) + .filter( + ( tax ) => + tax.slug !== 'nav_menu' && + tax.slug !== 'wp_pattern_category' + ) + .map( ( item ) => { + return { + value: item.slug, + label: item.name, + }; + } ); + + return [ selectOption, ...taxonomyOptions ]; + }; + return ( <> @@ -114,6 +160,22 @@ export default function PostNavigationLinkEdit( { + + + setAttributes( { + taxonomy: value, + inSameTerm: value === '' ? false : true, + } ) + } + help={ __( + 'Only link to posts that have the same taxonomy terms as the current post. For example the same tags or categories.' + ) } + /> + %2$s', $wrapper_attributes, diff --git a/test/integration/fixtures/blocks/core__post-navigation-link.json b/test/integration/fixtures/blocks/core__post-navigation-link.json index 9689f956f11509..b226c04855e359 100644 --- a/test/integration/fixtures/blocks/core__post-navigation-link.json +++ b/test/integration/fixtures/blocks/core__post-navigation-link.json @@ -6,7 +6,8 @@ "type": "next", "showTitle": false, "linkLabel": false, - "arrow": "none" + "arrow": "none", + "taxonomy": "" }, "innerBlocks": [] }