Skip to content

Commit 673c1cc

Browse files
committed
Add option to configure or disable category list on category archive page
1 parent cebf836 commit 673c1cc

File tree

6 files changed

+277
-77
lines changed

6 files changed

+277
-77
lines changed

archive.php

+10-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="row">
1414
<div class="col-12">
1515
<main id="primary" class="site-main archive">
16-
<?php if ( have_posts() ) : ?>
16+
<?php if ( have_posts() ) { ?>
1717

1818
<header class="page-header text-center">
1919
<?php
@@ -22,17 +22,6 @@
2222
?>
2323
</header><!-- .page-header -->
2424

25-
<?php
26-
$sunflower_parsed_blocks = parse_blocks( '<!-- wp:categories /-->' );
27-
if ( $sunflower_parsed_blocks ) {
28-
echo '<div class="filter-button-group mb-5 text-center sunflower-categories">';
29-
foreach ( $sunflower_parsed_blocks as $sunflower_block ) {
30-
echo wp_kses_post( render_block( $sunflower_block ) );
31-
}
32-
echo '</div>';
33-
}
34-
?>
35-
3625
<?php
3726
/* Start the Loop */
3827
$sunflower_list_items = '';
@@ -81,16 +70,17 @@
8170
'next_text' => __( 'next', 'sunflower' ),
8271
);
8372

84-
printf(
85-
'<div class="d-flex justify-content-around mt-3 mb-5"><div class="sunflower-pagination">%s</div></div>',
86-
wp_kses_post( paginate_links( $sunflower_args ) )
87-
);
88-
89-
else :
73+
$sunflower_paginated_links = paginate_links( $sunflower_args );
9074

75+
if ( $sunflower_paginated_links ) {
76+
printf(
77+
'<div class="d-flex justify-content-around mt-3 mb-5"><div class="sunflower-pagination">%s</div></div>',
78+
wp_kses_post( $sunflower_paginated_links )
79+
);
80+
}
81+
} else {
9182
get_template_part( 'template-parts/content', 'none' );
92-
93-
endif;
83+
}
9484
?>
9585

9686
</main><!-- #main -->

category.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* The template for displaying Category pages
4+
*
5+
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
6+
*
7+
* @package sunflower
8+
*/
9+
10+
get_header();
11+
?>
12+
<div id="content" class="container">
13+
<div class="row">
14+
<div class="col-12">
15+
<main id="primary" class="site-main archive">
16+
<?php if ( have_posts() ) { ?>
17+
18+
<header class="page-header text-center">
19+
<?php
20+
the_archive_title( '<h1 class="page-title">', '</h1>' );
21+
the_archive_description( '<div class="archive-description">', '</div>' );
22+
?>
23+
</header><!-- .page-header -->
24+
25+
<?php
26+
/* Show used categories below title and description depending on settings. */
27+
$sunflower_categories_archive_setting = sunflower_get_setting( 'sunflower_categories_archive' ) ? sunflower_get_setting( 'sunflower_categories_archive' ) : 'main-categories';
28+
29+
$sunflower_args = array(
30+
'echo' => false,
31+
'hierarchical' => false,
32+
'parent' => 0,
33+
'orderby' => 'name',
34+
'hide_empty' => true,
35+
'show_option_none' => '',
36+
'title_li' => '',
37+
);
38+
39+
if ( 'only-subcategories' === $sunflower_categories_archive_setting ) {
40+
$sunflower_args['parent'] = $cat;
41+
}
42+
43+
if ( 'no' !== $sunflower_categories_archive_setting ) {
44+
$sunflower_categories_archive = wp_list_categories( $sunflower_args );
45+
46+
if ( $sunflower_categories_archive ) {
47+
echo '<div class="filter-button-group mb-5 text-center sunflower-categories"><ul class="wp-block-categories-list wp-block-categories">';
48+
echo wp_kses_post( $sunflower_categories_archive );
49+
echo '</ul></div>';
50+
}
51+
}
52+
53+
/* Start the Loop */
54+
$sunflower_list_items = '';
55+
while ( have_posts() ) {
56+
57+
the_post();
58+
59+
/*
60+
* Include the Post-Type-specific template for the content.
61+
* If you want to override this in a child theme, then include a file
62+
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
63+
*/
64+
ob_start();
65+
get_template_part( 'template-parts/content', 'archive' );
66+
67+
$sunflower_article = ob_get_clean();
68+
69+
$sunflower_list_items .= sprintf(
70+
'<div class="col-md-6">%s</div>',
71+
$sunflower_article
72+
);
73+
74+
}
75+
?>
76+
77+
<div class="archive-loop row" data-masonry='{"percentPosition": true }'>
78+
<?php
79+
echo wp_kses(
80+
$sunflower_list_items,
81+
array_merge(
82+
wp_kses_allowed_html( 'post' ),
83+
array(
84+
'time' => array(
85+
'class' => true,
86+
'datetime' => true,
87+
),
88+
)
89+
)
90+
);
91+
?>
92+
</div>
93+
<?php
94+
95+
$sunflower_args = array(
96+
'prev_text' => __( 'previous', 'sunflower' ),
97+
'next_text' => __( 'next', 'sunflower' ),
98+
);
99+
100+
$sunflower_paginated_links = paginate_links( $sunflower_args );
101+
102+
if ( $sunflower_paginated_links ) {
103+
printf(
104+
'<div class="d-flex justify-content-around mt-3 mb-5"><div class="sunflower-pagination">%s</div></div>',
105+
wp_kses_post( $sunflower_paginated_links )
106+
);
107+
}
108+
} else {
109+
get_template_part( 'template-parts/content', 'none' );
110+
111+
}
112+
113+
?>
114+
115+
</main><!-- #main -->
116+
</div>
117+
</div>
118+
</div>
119+
<?php
120+
get_sidebar();
121+
get_footer();

functions/options/class-sunflowersettingspage.php

+42-5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ public function sunflower_settings_page_init(): void {
171171
'sunflower_layout',
172172
array( 'sunflower_header_social_media', __( 'Show social media icons in header', 'sunflower' ) )
173173
);
174+
175+
add_settings_field(
176+
'sunflower_categories_archive',
177+
__( 'Show list of categories on category archive', 'sunflower' ),
178+
$this->sunflower_categories_archive( ... ),
179+
'sunflower-setting-admin',
180+
'sunflower_layout',
181+
array( 'sunflower_categories_filter', __( 'Show list of categories on category archive', 'sunflower' ) )
182+
);
174183
}
175184

176185
/**
@@ -248,13 +257,41 @@ public function sunflower_contact_form_to(): void {
248257
public function sunflower_header_layout(): void {
249258
echo '<select id="sunflower_header_layout" name="sunflower_options[sunflower_header_layout]">';
250259

251-
$options = array( 'standard', 'personal' );
260+
$options = array(
261+
array( 'standard', __( 'Standard', 'sunflower' ) ),
262+
array( 'personal', __( 'Personal', 'sunflower' ) ),
263+
);
264+
foreach ( $options as $option ) {
265+
$selected = ( isset( $this->options['sunflower_header_layout'] ) && $this->options['sunflower_header_layout'] === $option[0] ) ? 'selected' : '';
266+
printf(
267+
'<option value="%1$s" %2$s>%3$s</option>',
268+
esc_attr( $option[0] ),
269+
esc_attr( $selected ),
270+
esc_attr( $option[1] )
271+
);
272+
}
273+
274+
echo '</select>';
275+
}
276+
277+
/**
278+
* Header layout variant field
279+
*/
280+
public function sunflower_categories_archive(): void {
281+
echo '<select id="sunflower_categories_archive" name="sunflower_options[sunflower_categories_archive]">';
282+
283+
$options = array(
284+
array( 'no', __( 'do not show', 'sunflower' ) ),
285+
array( 'main-categories', __( 'main categories', 'sunflower' ) ),
286+
array( 'only-subcategories', __( 'only sub-categories', 'sunflower' ) ),
287+
);
252288
foreach ( $options as $option ) {
253-
$selected = ( isset( $this->options['sunflower_header_layout'] ) && $this->options['sunflower_header_layout'] === $option ) ? 'selected' : '';
289+
$selected = ( isset( $this->options['sunflower_categories_archive'] ) && $this->options['sunflower_categories_archive'] === $option[0] ) ? 'selected' : '';
254290
printf(
255-
'<option value="%1$s" %2$s>%1$s</option>',
256-
esc_attr( $option ),
257-
esc_attr( $selected )
291+
'<option value="%1$s" %2$s>%3$s</option>',
292+
esc_attr( $option[0] ),
293+
esc_attr( $selected ),
294+
esc_attr( $option[1] )
258295
);
259296
}
260297

languages/de_DE.mo

254 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)