我有时间看看你的问题options-reading.php 页面,它是用于在后端呈现阅读设置页面的模板。
遗憾的是,在可选择的下拉列表中,没有过滤器可以过滤或添加自定义帖子作为粘滞帖子。我们可以使用两个隐藏的过滤器,它们是
您需要决定是需要使用自定义帖子类型的帖子显示页面,还是只需要自定义帖子类型
您可以尝试以下操作:
add_filter( \'get_pages\', function ( $pages, $args )
{
// First make sure this is an admin page, if not, bail
if ( !is_admin() )
return $pages;
// Make sure that we are on the reading settings page, if not, bail
global $pagenow;
if ( \'options-reading.php\' !== $pagenow )
return $pages;
// Remove the filter to avoid infinite loop
remove_filter( current_filter(), __FUNCTION__ );
$args = [
\'post_type\' => \'my_frontpage\',
\'posts_per_page\' => -1
];
// Get the post type posts with get_posts to allow non hierarchical post types
$new_pages = get_posts( $args );
/**
* You need to decide if you want to add custom post type posts to the pages
* already in the dropdown, or just want the custom post type posts in
* the dropdown. I will handle both, just remove what is not needed
*/
// If we only need custom post types
$pages = $new_pages;
// If we need to add custom post type posts to the pages
// $pages = array_merge( $new_pages, $pages );
return $pages;
}, 10, 2 );
现在,您应该可以在下拉列表中看到自定义帖子类型的帖子。请注意,此代码也会影响博客页面的下拉列表。
为了避免这种情况,您可以使用静态计数器来计算过滤器运行的次数,然后在过滤器应用于blogpage下拉列表之前退出。过滤器将总共运行3次,如下所示:get_pages()
运行3次:
首先检查我们是否确实有页面设置为静态首页。
第二次跑步将在室内进行wp_dropdown_pages()
静态首页下拉列表使用
最后一次跑步将在wp_dropdown_pages()
blogpage下拉列表使用的
因此,基于此,我们可以尝试
add_filter( \'get_pages\', function ( $pages, $args )
{
// First make sure this is an admin page, if not, bail
if ( !is_admin() )
return $pages;
// Make sure that we are on the reading settings page, if not, bail
global $pagenow;
if ( \'options-reading.php\' !== $pagenow )
return $pages;
// Remove the filter to avoid infinite loop
remove_filter( current_filter(), __FUNCTION__ );
// Setup our static counter
static $counter = 0;
// Bail on the third run all runs after this. The third run will be 2
if ( 2 <= $counter )
return $pages;
// Update our counter
$counter++;
$args = [
\'post_type\' => \'my_frontpage\',
\'posts_per_page\' => -1
];
// Get the post type posts with get_posts to allow non hierarchical post types
$new_pages = get_posts( $args );
/**
* You need to decide if you want to add custom post type posts to the pages
* already in the dropdown, or just want the custom post type posts in
* the dropdown. I will handle both, just remove what is not needed
*/
// If we only need custom post types
$pages = $new_pages;
// If we need to add custom post type posts to the pages
// $pages = array_merge( $new_pages, $pages );
return $pages;
}, 10, 2 );
如果您访问前端并访问首页,您会发现它将重定向到单个贴子页。这是因为,默认情况下,静态首页上的主查询设置为查询
page
岗位类型。这会导致返回404
redirect_canonical()
然后重定向到单篇文章页面。这很容易解决,我们所要做的就是调整静态首页上的主查询。
add_action( \'pre_get_posts\', function ( $q )
{
if ( !is_admin() // Only target the front end
&& $q->is_main_query() // Only target the main query
&& \'page\' === get_option( \'show_on_front\' ) // Only target the static front page
) {
$q->set( \'post_type\', \'my_frontpage\' );
}
});
现在,静态首页将正确显示。
您只需设置一个模板。您只需创建front-page.php
, WordPress将自动使用它