您可以在WordPress中将自定义帖子类型的帖子设置为静态主页吗?
在阅读设置中,在首页显示下,我可以选择使用静态页面。
我可以将帖子用作我的主页吗?
我正在运行多站点,并希望每个站点的主页都是在" Agency"自定义帖子类型。他们只允许有一个帖子,这将是他们的主页。
我似乎无法将帖子作为静态主页发布。
有什么建议吗?
答案 0 :(得分:1)
首先创建自定义模板
在您的模板中,查询您的CPT(自定义帖子类型),
现在,在wp-admin
中创建页面并分配您创建的模板。
现在转到设置 - >>阅读 - >选择您在wp-admin
...
我希望你能得到它..
答案 1 :(得分:0)
是的,我们可以将自定义帖子类型(任意帖子)设置为您网站的首页, 步骤进行:
1)创建自定义帖子类型,例如。 LANDING_PAGE
2)添加你喜欢的帖子(主页/服务/关于/任何...)
使用创建theme_option.php在wordpress主题定制器中创建自定义下拉字段为“SELECT HOME PAGE”并编写此代码 -
<?php
if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'theme_name_Customize_Misc_Control' ) ) :
class theme_name_Customize_Misc_Control extends WP_Customize_Control {
public function render_content() {
global $post;
switch ( $this->type ) {
default:
case 'description' :
echo '
<p class="description">' . $this->description . '</p>';
break;
case 'heading':
echo '<h3>' . esc_html( $this->label ) . '</h3>';
break;
case 'line' :
echo '<hr />';
break;
}
}
}
endif;
// Create your custom panel in your theme using customize_register function
function theme_name_logo_settings($wp_customize) {
$wp_customize->add_section('theme_name_theme_panel', array(
'title' => 'theme_name Theme Global Setting',
'description' => '',
'priority' => 120,
));
}
add_action('customize_register', 'theme_name_logo_settings');
// Creating dropdown
if( class_exists( 'WP_Customize_Control' ) ):
class WP_Customize_Latest_Post_Control extends WP_Customize_Control {
public $type = 'latest_post_dropdown';
public $post_type = 'custom_post_type_name';
public function render_content() {
$latest = new WP_Query( array(
'post_type' => $this->post_type,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
));
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?>>
<?php
while( $latest->have_posts() ) {
$latest->the_post();
echo "<option " . selected( $this->value(), get_the_ID() ) . " value='" . get_the_ID() . "'>" . the_title( '', '', false ) . "</option>";
}
?>
</select>
</label>
<?php
}
}
endif;
function theme_name_pages($wp_customize) {
$wp_customize->add_setting('home_page_control');
$wp_customize->add_control(
new WP_Customize_Latest_Post_Control(
$wp_customize,
'home_page_control',
array(
'label' => __( 'Select A Home Page', 'theme_name' ),
'section' => 'theme_name_theme_panel',
'settings' => 'home_page_control',
'post_type' => 'custom_post_type_name'
)
)
);
}
add_action('customize_register', 'theme_name_pages');
//Static custom post for landing
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' => 'custom_post_type_name',
'posts_per_page' => -1
];
// Get the post type posts with get_posts to allow non hierarchical post types
$new_pages = get_posts( $args );
// If we only need custom post types
$pages = $new_pages;
return $pages;
}, 10, 2 );
并在functions.php中添加以下功能
add_action( 'pre_get_posts', 'set_custom_post_type_posts_as_homepage' );
function set_custom_post_type_posts_as_homepage( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'post_type', array( 'post', 'custom_post_type_name') );
}
}
最后创建一个php文件为(home.php)
按照语法
进行调用 $postid = get_theme_mod( 'home_page_control');
if($postid == ""){ echo "Please select a page to show on home page in appearance > customize"; }
如果出现任何问题,请更改永久链接设置并尝试使用。
答案 2 :(得分:0)
您可以创建新的静态页面,为此页面创建新模板,并在模板中添加
header("Location: https://your-site.com/url-of-post-for-main-page/");