我很难弄清楚如何在我正在创建的网站中拥有多个标题。以及公文包的列宽。我想添加一个在网站中显示标题的选项。右上角,默认为左。并且能够在公文包中设置列宽。
我想知道如何加载顶部和右侧的头,并能够在JavaScript文件中进行一些自定义,因为jquery.blog.js中的SidebarWidth是220。
<?php
if of_get_option ('header_position','top') :
get_header( 'top' );
elseif of_get_option ('header_position','right') :
get_header( 'right' );
else :
get_header();
endif;
?>
现在我需要他们理解用户选择页眉顶部,如果选择右侧选项,我希望页眉位于顶部或右侧。
<?php
wp_register_script('your-script', $script-url, $dependencies, false, false);
$pass_to_js = array();
if of_get_option ('header_position','top') :
$pass_to_js['sidebarWidth'] = /* a number */;
get_header( 'top' );
elseif of_get_option ('header_position','right') :
$pass_to_js['sidebarWidth'] = 220;
get_header( 'right' );
else :
$pass_to_js['sidebarWidth'] = /* another number */;
get_header();
endif;
wp_localize_script('your-script', 'js-array-name', $pass_to_js);
wp_enqueue_script('your-script');
?>
在上面的代码中,如果elseif和endif行出现错误。///在我的投资组合案例中
wp_register_script('luna_portfolio', get_template_directory_uri() . '/js/jquery.portfolio.js', 'jquery', true);
wp_localize_script('luna_portfolio', 'lunaAjax', array('ajaxurl'=>admin_url('admin-ajax.php'),'nonce' => wp_create_nonce('ajax-nonce')));
和js部分
function luna_getCols(){
var cols = 1;
var windowWidth = jQuery(window).width();
if(windowWidth>=380 && windowWidth<1160) cols = 3;
else if(windowWidth>=1160 && windowWidth<1640) cols = 4;
else if(windowWidth>=1640 && windowWidth<2100) cols = 5;
else if(windowWidth>=2100) cols = 6;
return cols;
}
是的,使用该代码,然后在主题目录中放入文件header.php
、header-top.php
和header-right.php
,每个文件都有不同的头代码。有关页眉模板化的详细信息,请访问https://codex.wordpress.org/Function_Reference/get_header.
对于问题的js方面,您可以根据条件将变量从php传递到js文件。你的代码看起来有点。。。像这样:
<?php
wp_register_script('your-script', $script-url, $dependencies, false, false);
$pass_to_js = array();
if (of_get_option ('header_position','top')) :
$pass_to_js['sidebarWidth'] = /* a number */;
get_header( 'top' );
elseif (of_get_option ('header_position','right')) :
$pass_to_js['sidebarWidth'] = 220;
get_header( 'right' );
else :
$pass_to_js['sidebarWidth'] = /* another number */;
get_header();
endif;
wp_localize_script('your-script', 'js-array-name', $pass_to_js);
wp_enqueue_script('your-script');
?>
然后,在您的js文件中,js-array-name
将作为全局数组存在。你在任何地方都把sidebarWidth
称为
js-array-name.sidebarWidth