Skip to content

Commit

Permalink
Add option to show latest posts with 2 or 3 columns
Browse files Browse the repository at this point in the history
  • Loading branch information
albig committed Sep 2, 2024
1 parent 29c0ddb commit 1418261
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"type": "string",
"default": "grid"
},
"columns": {
"type": "number",
"default": 2
},
"excerptLength": {
"type": "number",
"default": 55
Expand Down
15 changes: 15 additions & 0 deletions src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default function Edit( { attributes, setAttributes } ) {
count,
archiveText,
blockLayout,
columns,
} = attributes;

const [ categoriesFormSuggestions, setCategoriesFormSuggestions ] =
Expand Down Expand Up @@ -250,6 +251,20 @@ export default function Edit( { attributes, setAttributes } ) {
value={ archiveText }
onChange={ onChangeArchiveText }
/>
{ blockLayout === 'grid' && (
<RangeControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Columns' ) }
value={ columns }
onChange={ ( value ) =>
setAttributes( { columns: value } )
}
min={ 2 }
max={ 3 }
required
/>
) }
</PanelBody>
</InspectorControls>
}
Expand Down
18 changes: 15 additions & 3 deletions src/latest-posts/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
$sunflower_classnames[] = 'has-background';
$sunflower_classnames[] = 'latest-posts';

$sunflower_is_grid = false;
$sunflower_columns = 1;
if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
$sunflower_is_grid = true;
if ( isset( $attributes['columns'] ) ) {
$sunflower_columns = $attributes['columns'];
}
}

$sunflower_count = isset( $attributes['count'] ) ? (int) $attributes['count'] : 6;
Expand Down Expand Up @@ -60,7 +62,17 @@
$sunflower_title
);

$sunflower_css_col = true === $sunflower_is_grid ? 'col-md-6' : 'col-12';
switch ( $sunflower_columns ) {
case 1:
$sunflower_css_col = 'col-12';
break;
case 2:
$sunflower_css_col = 'col-md-6';
break;
case 3:
$sunflower_css_col = 'col-md-4';
break;
}

while ( $sunflower_posts->have_posts() ) {
$sunflower_posts->the_post();
Expand Down

0 comments on commit 1418261

Please sign in to comment.