我知道很容易从设置>阅读>首页中选择首页静态页面。然而,我正在寻找一种在主题选项中使用此功能的方法。我们可以这样做吗?
我们可以允许用户从主题选项中选择首页吗?
3 个回复
SO网友:tillinberlin
由于wp 3.4,您可以使用主题定制器来实现这一点。据我所知,“静态首页”是主题定制器的默认选项之一。
查看有关Appearance Customize Screen 在codex进行一般介绍。然后也许Customizer tutorials and documentation 可能是有用的下一步。
SO网友:Rutwick Gangurde
WordPress使用options API保存这些设置。您只需找到设置名称及其值。在您的情况下,如下所示:
//Option name - Value
//This corresponds to \'Front page displays\'
show_on_front - posts/page
//This corresponds to the Static page: Front page name
page_on_front - <page id>
//This corresponds to the Posts page: Posts page name
page_for_posts - <page id>
您所要做的就是创建一个正确显示选项的表单。你可以在WordPress设置页面中查看表单并复制它。这些名字必须与我在上面给出的名字相匹配(如果你复制WordPress的表单,它们就会匹配)。现在,当您提交表单时,只需更新如下设置://If you want the page titled \'Home\', with the ID 34 to be the homepage.
update_option(\'show_on_front\', \'page\');
update_option(\'page_on_front\', 34);
就是这样。希望这有帮助。SO网友:Ayanize
使现代化
我想出来了。我从wp-admin/options-reading.php
. 下面是示例代码。这只是添加了一个选项,选择一个页面设置为我的主题选项的首页。
add_menu_page(\'Menu Name\', \'Menu Name\', \'manage_options\', \'menu-slug\', \'menu_manage_options\');
function menu_manage_options(){
?>
<div class="wrap">
<h1><?php echo esc_html( $title ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( \'reading\' );
if ( ! in_array( get_option( \'blog_charset\' ), array( \'utf8\', \'utf-8\', \'UTF8\', \'UTF-8\' ) ) )
add_settings_field( \'blog_charset\', __( \'Encoding for pages and feeds\' ), \'options_reading_blog_charset\', \'reading\', \'default\', array( \'label_for\' => \'blog_charset\' ) );
?>
<?php if ( ! get_pages() ) : ?>
<input name="show_on_front" type="hidden" value="posts" />
<table class="form-table">
<?php
if ( \'posts\' != get_option( \'show_on_front\' ) ) :
update_option( \'show_on_front\', \'posts\' );
endif;
else :
if ( \'page\' == get_option( \'show_on_front\' ) && ! get_option( \'page_on_front\' ) && ! get_option( \'page_for_posts\' ) )
update_option( \'show_on_front\', \'posts\' );
?>
<table class="form-table">
<tr>
<th scope="row"><?php _e( \'Front page displays\' ); ?></th>
<td id="front-static-pages"><fieldset><legend class="screen-reader-text"><span><?php _e( \'Front page displays\' ); ?></span></legend>
<p><label>
<input name="show_on_front" type="radio" value="page" class="tog" <?php checked( \'page\', get_option( \'show_on_front\' ) ); ?> />
<?php printf( __( \'A <a href="%s">static page</a> (select below)\' ), \'edit.php?post_type=page\' ); ?>
</label>
</p>
<ul>
<li><label for="page_on_front"><?php printf( __( \'Front page: %s\' ), wp_dropdown_pages( array( \'name\' => \'page_on_front\', \'echo\' => 0, \'show_option_none\' => __( \'— Select —\' ), \'option_none_value\' => \'0\', \'selected\' => get_option( \'page_on_front\' ) ) ) ); ?></label></li>
</ul>
<?php if ( \'page\' == get_option( \'show_on_front\' ) && get_option( \'page_for_posts\' ) == get_option( \'page_on_front\' ) ) : ?>
<div id="front-page-warning" class="error inline"><p><?php _e( \'<strong>Warning:</strong> these pages should not be the same!\' ); ?></p></div>
<?php endif; ?>
</fieldset></td>
</tr>
<?php endif; ?>
<?php do_settings_fields( \'reading\', \'default\' ); ?>
</table>
<?php do_settings_sections( \'reading\' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}