Skip to content

Commit fc4fcce

Browse files
author
Paul Bearne
committed
Enable custom post types on the homepage selection, add 'Books' CPT.
1 parent 713b4bb commit fc4fcce

File tree

4 files changed

+88
-56
lines changed

4 files changed

+88
-56
lines changed

src/wp-admin/includes/options.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ function options_reading_add_js() {
115115
<script>
116116
jQuery( function($) {
117117
var section = $('#front-static-pages'),
118-
staticPage = section.find('input:radio[value="page"]'),
119118
selects = section.find('.staticPages select'),
120119
homepage_types = section.find('select#homepage_types')
121120
check_disabled = function(){
122-
selects.prop( 'disabled', ! staticPage.prop('checked') );
123-
homepage_types.prop( 'disabled', staticPage.prop('checked') );
121+
selects.prop( 'disabled', homepage_types.val() !== "page" );
124122
};
125123
check_disabled();
126-
section.find( 'input:radio' ).on( 'change', check_disabled );
124+
homepage_types.on( 'change', check_disabled );
127125
} );
128126
</script>
129127
<?php

src/wp-admin/options-reading.php

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@
6767
if ( ! is_utf8_charset() ) {
6868
add_settings_field( 'blog_charset', __( 'Encoding for pages and feeds' ), 'options_reading_blog_charset', 'reading', 'default', array( 'label_for' => 'blog_charset' ) );
6969
}
70-
?>
71-
72-
<?php if ( ! get_pages() ) : ?>
73-
<input name="show_on_front" type="hidden" value="posts" />
74-
<table class="form-table" role="presentation">
75-
<?php
76-
if ( 'posts' != get_option( 'show_on_front' ) ) :
77-
update_option( 'show_on_front', 'posts' );
78-
endif;
79-
8070
/**
8171
* this fillter overrides which post type are show in the dropdown
8272
*
@@ -88,36 +78,23 @@
8878
*/
8979
$post_types_allowed_on_home_page = apply_filters( 'post_types_allowed_on_home_page', array_keys( get_post_types( array( 'show_in_home_page_list' => true ) ) ) );
9080
$args = array(
91-
'post_type' => $post_types_allowed_on_home_page,
92-
'post_status' => 'publish',
93-
'fields' => 'ids'
81+
'post_type' => $post_types_allowed_on_home_page,
82+
'post_status' => 'publish',
83+
'fields' => 'ids'
9484
);
9585
$allowed_pages = new WP_Query( $args );
86+
?>
87+
88+
<?php if ( ! get_pages() ) : ?>
89+
<input name="show_on_front" type="hidden" value="posts" />
90+
<table class="form-table" role="presentation">
91+
<?php
92+
if ( 'posts' != get_option( 'show_on_front' ) ) :
93+
update_option( 'show_on_front', 'posts' );
94+
endif;
95+
endif;
96+
9697

97-
//if ( empty( $allowed_pages->posts ) ) : ?>
98-
<!--<input name="show_on_front" type="hidden" value="posts" />-->
99-
<!--<table class="form-table" role="presentation">-->
100-
<!-- <tr>-->
101-
<!-- <th scope="row">--><?php //_e( 'Your homepage displays' ); ?><!--</th>-->
102-
<!-- <td>--><?php
103-
// $content_type_links = array();
104-
// foreach ($post_types_allowed_on_home_page as $type ){
105-
// $content_type_links[] = sprintf('<a href="%s">%s</a>',
106-
// 'edit.php?post_type=' . $type, ucfirst( $type ) );
107-
// }
108-
// printf(
109-
// /* translators: %s: URL to Pages screen. */
110-
// __( 'No selectable pages found! Add at least one of these content types %s' ),
111-
// implode( ', ', $content_type_links )
112-
// );
113-
// ?>
114-
<!-- </td>-->
115-
<!-- --><?php
116-
// if ( 'posts' !== get_option( 'show_on_front' ) ) :
117-
// update_option( 'show_on_front', 'posts' );
118-
// endif;
119-
//
120-
//else :
12198
if ( 'page' === get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) ) {
12299
update_option( 'show_on_front', 'posts' );
123100
}
@@ -130,22 +107,26 @@
130107
<td id="front-static-pages"><fieldset>
131108
<legend class="screen-reader-text"><span><?php echo $your_homepage_displays_title; ?></span></legend>
132109
<p><label>
133-
<input name="show_on_front" type="radio" value="posts" class="tog" <?php
110+
<?php
134111
$allowed_archives_on_home_page = array( 'posts') + $post_types_allowed_on_home_page;
135112
unset( $allowed_archives_on_home_page['page'] );
136-
checked( in_array( get_option( 'show_on_front' ), $allowed_archives_on_home_page, true ) ); ?> />
137-
<?php
138113
if( 1 === count( $allowed_archives_on_home_page ) ) {
139-
_e( 'Your latest posts' );
114+
_e( sprintf( 'Your latest %s', get_post_type_object( $allowed_archives_on_home_page[0] )->labels->name ) );
115+
?><input name="show_on_front" type="hidden" value="<?php echo esc_attr( $allowed_archives_on_home_page[0] ); ?>" /> <?php
140116
} else {
141-
$output = "<select name='show_archive_on_front' id='homepage_types'>\n";
117+
$output = "<select name='show_on_front' id='homepage_types'>\n";
118+
$output .= sprintf( '<option value="page" %s>A static page (select below)</option>', selected( -1, strtolower( get_option( 'show_on_front' ) ), false ) ) ;
142119
foreach ( $allowed_archives_on_home_page as $post_type ){
143120
$post_type = ( 'posts' === $post_type ) ? 'post' : $post_type;
144-
$output .= sprintf( '<option %s>%s</option>', $post_type, get_post_type_object( $post_type )->labels->name ) ;
121+
$output .= sprintf( '<option value="%s" %s>%s</option>',
122+
$post_type,
123+
selected( $post_type, strtolower( get_option( 'show_on_front' ) ),false ),
124+
sprintf( __( 'Your latest %s' ), get_post_type_object( $post_type )->labels->name )
125+
) ;
145126
}
146127
$output .= "</select>\n";
147128

148-
printf( __( 'Your latest %s' ), $output );
129+
echo $output;
149130
}
150131
?>
151132
</label>
@@ -166,13 +147,13 @@
166147
</p>
167148
<?php else : ?>
168149
<p><label>
169-
<input name="show_on_front" type="radio" value="page" class="tog" <?php checked( 'page', get_option( 'show_on_front' ) ); ?> />
150+
<!-- <input name="show_on_front" type="radio" value="page" class="tog" --><?php //checked( 'page', get_option( 'show_on_front' ) ); ?><!-- />-->
170151
<?php
171-
printf(
172-
/* translators: %s: URL to Pages screen. */
173-
__( 'A <a href="%s">static page</a> (select below)' ),
174-
'edit.php?post_type=page'
175-
);
152+
// printf(
153+
// /* translators: %s: URL to Pages screen. */
154+
// __( 'A <a href="%s">static page</a> (select below)' ),
155+
// 'edit.php?post_type=page'
156+
// );
176157
?>
177158
</label>
178159
</p>
@@ -190,7 +171,10 @@
190171
);
191172
$cpt_posts = new WP_Query( $args );
192173
foreach ( $cpt_posts->posts as $cpt_post ) {
193-
$output .= sprintf( '<option %s>%s</option>', $cpt_post->ID, $cpt_post->post_name ) ;
174+
$output .= sprintf( '<option value="%s" %s>%s</option>',
175+
$cpt_post->ID,
176+
selected( $cpt_post->ID, get_option( 'page_on_front' ),false ),
177+
$cpt_post->post_name ) ;
194178
}
195179
$output .= '</optgroup>';
196180
}

src/wp-content/themes/twentytwentyfive/functions.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,52 @@ function twentytwentyfive_register_block_bindings() {
141141
endif;
142142
add_action( 'init', 'twentytwentyfive_register_block_bindings' );
143143

144+
/**
145+
* Registers the 'books' custom post type.
146+
*
147+
* @since Twenty Twenty-Five 1.0
148+
*
149+
* @return void
150+
*/
151+
function twentytwentyfive_register_books_post_type() {
152+
$labels = array(
153+
'name' => _x( 'Books', 'post type general name', 'twentytwentyfive' ),
154+
'singular_name' => _x( 'Book', 'post type singular name', 'twentytwentyfive' ),
155+
'menu_name' => _x( 'Books', 'admin menu', 'twentytwentyfive' ),
156+
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'twentytwentyfive' ),
157+
'add_new' => _x( 'Add New', 'book', 'twentytwentyfive' ),
158+
'add_new_item' => __( 'Add New Book', 'twentytwentyfive' ),
159+
'new_item' => __( 'New Book', 'twentytwentyfive' ),
160+
'edit_item' => __( 'Edit Book', 'twentytwentyfive' ),
161+
'view_item' => __( 'View Book', 'twentytwentyfive' ),
162+
'all_items' => __( 'All Books', 'twentytwentyfive' ),
163+
'search_items' => __( 'Search Books', 'twentytwentyfive' ),
164+
'parent_item_colon' => __( 'Parent Books:', 'twentytwentyfive' ),
165+
'not_found' => __( 'No books found.', 'twentytwentyfive' ),
166+
'not_found_in_trash' => __( 'No books found in Trash.', 'twentytwentyfive' ),
167+
);
168+
169+
$args = array(
170+
'labels' => $labels,
171+
'public' => true,
172+
'publicly_queryable' => true,
173+
'show_ui' => true,
174+
'show_in_menu' => true,
175+
'query_var' => true,
176+
'rewrite' => array( 'slug' => 'book' ),
177+
'capability_type' => 'post',
178+
'has_archive' => true,
179+
'hierarchical' => false,
180+
'menu_position' => null,
181+
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
182+
'show_in_rest' => true,
183+
'show_in_home_page_list' => true,
184+
);
185+
186+
register_post_type( 'book', $args );
187+
}
188+
add_action( 'init', 'twentytwentyfive_register_books_post_type' );
189+
144190
if ( ! function_exists( 'twentytwentyfive_format_binding' ) ) :
145191
/**
146192
* Callback function for the post format name block binding source.
@@ -157,3 +203,5 @@ function twentytwentyfive_format_binding() {
157203
}
158204
}
159205
endif;
206+
207+

src/wp-includes/class-wp-query.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4470,8 +4470,10 @@ public function is_comment_feed() {
44704470
* @return bool Whether the query is for the front page of the site.
44714471
*/
44724472
public function is_front_page() {
4473+
$post_types_allowed_on_home_page = apply_filters( 'post_types_allowed_on_home_page', array_keys( get_post_types( array( 'show_in_home_page_list' => true ) ) ) );
4474+
44734475
// Most likely case.
4474-
if ( 'posts' === get_option( 'show_on_front' ) && $this->is_home() ) {
4476+
if ( in_array( get_option( 'show_on_front' ), $post_types_allowed_on_home_page, true ) && $this->is_home() ) {
44754477
return true;
44764478
} elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' )
44774479
&& $this->is_page( get_option( 'page_on_front' ) )

0 commit comments

Comments
 (0)