Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the Query title block to display a "Blog" title on any template #64574

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ Display the query title. ([Source](https://github.com/WordPress/gutenberg/tree/t
- **Name:** core/query-title
- **Category:** theme
- **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** level, levelOptions, showPrefix, showSearchTerm, textAlign, type
- **Attributes:** blogTitle, level, levelOptions, showPrefix, showSearchTerm, textAlign, type

## Quote

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/query-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"showSearchTerm": {
"type": "boolean",
"default": true
},
"blogTitle": {
"type": "string",
"default": "Blog"
}
},
"example": {
Expand Down
47 changes: 41 additions & 6 deletions packages/block-library/src/query-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { __, _x, sprintf } from '@wordpress/i18n';
*/
import { useArchiveLabel } from './use-archive-label';

const SUPPORTED_TYPES = [ 'archive', 'search' ];
const SUPPORTED_TYPES = [ 'archive', 'search', 'blog' ];

export default function QueryTitleEdit( {
attributes: {
Expand All @@ -32,10 +32,14 @@ export default function QueryTitleEdit( {
textAlign,
showPrefix,
showSearchTerm,
blogTitle,
},
setAttributes,
} ) {
const { archiveTypeLabel, archiveNameLabel } = useArchiveLabel();
const currentTemplate = useSelect( ( select ) => {
return select( coreStore )?.getEditedPostType();
} );

const TagName = `h${ level }`;
const blockProps = useBlockProps( {
Expand All @@ -44,7 +48,10 @@ export default function QueryTitleEdit( {
} ),
} );

if ( ! SUPPORTED_TYPES.includes( type ) ) {
if (
! SUPPORTED_TYPES.includes( type ) &&
! [ 'index', 'home' ].includes( currentTemplate )
) {
return (
<div { ...blockProps }>
<Warning>{ __( 'Provided type is not supported.' ) }</Warning>
Expand Down Expand Up @@ -103,9 +110,7 @@ export default function QueryTitleEdit( {
<TagName { ...blockProps }>{ title }</TagName>
</>
);
}

if ( type === 'search' ) {
} else if ( type === 'search' ) {
titleElement = (
<>
<InspectorControls>
Expand All @@ -130,6 +135,30 @@ export default function QueryTitleEdit( {
</TagName>
</>
);
} else if (
[ 'index', 'home' ].includes( currentTemplate ) ||
type === 'blog'
) {
titleElement = (
<>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<TextControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Blog Title' ) }
value={ blogTitle || __( 'Blog' ) }
onChange={ ( value ) =>
setAttributes( { blogTitle: value } )
}
/>
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>
{ blogTitle || __( 'Blog' ) }
</TagName>
</>
);
}

return (
Expand All @@ -149,7 +178,13 @@ export default function QueryTitleEdit( {
} }
/>
</BlockControls>
{ titleElement }
{ titleElement || (
<div { ...blockProps }>
<Warning>
{ __( 'Unsupported template for Query Title block.' ) }
</Warning>
</div>
) }
</>
);
}
5 changes: 5 additions & 0 deletions packages/block-library/src/query-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ function render_block_core_query_title( $attributes ) {
return '';
}
$title = '';

if ( is_home() || is_front_page() ) {
$title = isset( $attributes['blogTitle'] ) ? $attributes['blogTitle'] : __( 'Blog' );
}

if ( $is_archive ) {
$show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
if ( ! $show_prefix ) {
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__query-title.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"attributes": {
"level": 1,
"showPrefix": true,
"showSearchTerm": true
"showSearchTerm": true,
"blogTitle": "Blog"
},
"innerBlocks": []
}
Expand Down
Loading