Wordpress函数get Sidebar()函数加载侧边栏模板

WordPress函数get_sidebar()函数为主题加载侧边栏模板

WordPress函数get_sidebar()函数为主题加载侧边栏模板

  在 WordPress CMS内容管理系统中,get_sidebar()函数是 WordPress 中内置函数。get_sidebar 
() WordPress PHP 函数会为主题加载侧边栏模板。如果提供了特定名称,则会包含专门的侧边栏

  推荐:[最新版]WordPress SEO插件Rank Math Pro

get_sidebar()函数基本语法

描述

  为主题加载侧边栏模板

用法

get_sidebar( 'sidebar-name', $args );
  • $name (string) (可选) – 专用侧边栏的名称。默认值:null
  • $args (array) (可选) – 传递给侧边栏模板的附加参数。默认值:array()

  推荐:WordPress函数add_network_option()添加新的网络选项

get_sidebar()函数

  在 WordPress 中,该is_dynamic_sidebar()函数通常是确定主题是否启用并使用动态侧边栏(源文件可参考这里

function get_sidebar( $name = null, $args = array() ) {
	/**
	 * Fires before the sidebar template file is loaded.
	 *
	 * @since 2.2.0
	 * @since 2.8.0 The `$name` parameter was added.
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
	 * @param array       $args Additional arguments passed to the sidebar template.
	 */
	do_action( 'get_sidebar', $name, $args );

	$templates = array();
	$name      = (string) $name;
	if ( '' !== $name ) {
		$templates[] = "sidebar-{$name}.php";
	}

	$templates[] = 'sidebar.php';

	if ( ! locate_template( $templates, true, true, $args ) ) {
		return false;
	}
}

  推荐:WordPress函数allow_subdirectory_install()检查多站点网络是否允许允许子目录安装

如何使用get_sidebar()

  简单调用,从名为“sidebar-nice-bar.php”的文件中加载一个名为“nice-bar”的专门侧边栏:

get_sidebar('nice-bar');

  左侧和右侧边栏,在主题中包含左侧和右侧边栏,文件名应为“sidebar-left.php”和“sidebar-right.php”。

get_header();
get_sidebar('left');
get_sidebar('right');
get_footer();

  多侧边栏为不同的页面加载不同的侧边栏,文件名应为“sidebar-home.php”和“sidebar-404.php”。

if (is_home()) {
    get_sidebar('home');
} elseif (is_404()) {
    get_sidebar('404');
} else {
    get_sidebar();
}

  简单的404页面创建一个简单的404错误页面模板,

get_header();
echo '<h2>Error 404 - Not Found</h2>';
get_sidebar();
get_footer();

  使用$args参数调用侧边栏(自5.5.0起)使用附加参数调用侧边栏,在“sidebar-shop.php”中,使用传递的参数:

$args = array('title' => 'Shop sidebar');
get_sidebar('shop', $args);

echo '<div id="secondary" class="widget-area sidebar-shop" role="complementary">';
echo '<h2>' . esc_html($args['title']) . '</h2>';
dynamic_sidebar('sidebar-shop');
echo '</div>';

  任何侧边栏的条件语句在调用之前检查侧边栏是否存在:

if (function_exists('register_sidebar')) {
    get_sidebar();
}

  或者,检查特定的已注册侧边栏:

if (is_active_sidebar('content-bottom')) {
    get_sidebar('content-bottom');
}

  推荐:怎么安装Genesis Framework主题框架

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数get_sidebar()函数为主题加载侧边栏模板

转载请保留链接:https://www.pythonthree.com/wordpress-get-sidebar/

滚动至顶部