Joomla:我们如何为不同的菜单 ID 分配不同的布局?

2022-01-06 00:00:00 php joomla

我正在运行 Joomla 并就以下问题寻求您的帮助.

I am running Joomla and seeking you help for the following issue.

假设我的模板中有 3 个布局,并且布局文件被命名为...

Lets say I have 3 layouts in my template and the layout files are named as...

index.php
index2.php
index3.php

我有 5 个菜单链接说....

I have 5 menu links say....

Link 1
Link 2
Link 3
Link 4
Link 5

我要找的是......

What I am looking for is......

对于 Link 1、Link 4 和 Link 5,我希望 Joomla 加载常规的 index.php 但对于 Link 2 我想要Joomla 加载 index2.php 和类似的 Link 3 我希望它加载 index3.php.

For Link 1, Link 4 and Link 5, I want Joomla to load the regular index.php but for Link 2 I want Joomla to load index2.php and similarly for Link 3 I want it to load index3.php.

我的意思是...我们如何为不同的菜单 ID 分配不同的布局?

What I mean is... How can we assign a different layouts to different menu IDs?

我知道有一个内置选项可以根据菜单 ID 选择不同的模板,但我不想只为这个功能复制模板文件.我的模板中的所有内容都相同,只是布局中的更改取决于菜单 ID.

I know there is an inbuilt option to choose a different Template based on menu ID but I do not want to duplicate the template files just for this one function. Everything in my templates is the same just the change is in the layout depending on the Menu ID.

请帮忙.

推荐答案

您使用的是商业模板还是自定义模板?您应该能够对 index.php 进行编码,以便布局由页面上加载的模块确定.然后,您可以通过模块参数中的菜单分配来控制显示哪些模块.您可以通过 CSS、Page Class Suffix 和 index.php 上的代码来控制显示的布局.

Are you using a commercial template or something custom? You should be able to code your index.php so that the layout is determined by the modules loaded on the page. You then control what modules show up by the menu assignments in the module parameters. You can control the layout being displayed through CSS, Page Class Suffix, and the code on index.php.

模板中的每个模块位置都应该是可折叠的 - 这意味着如果没有模块加载到该位置,它不会被添加到 HTML 中.使用这样的东西:

Every module position in your template should be collapsible - meaning that if no modules are loaded to the position, it does not get added to the HTML. Use something like this:

<?php if ($this->countModules('left')) : ?>
    <jdoc:include type="modules" name="left" style="xhtml" />
<?php endif; ?>

您还可以使用可以在菜单项的系统参数上设置的页面类后缀和 CSS 的组合来控制页面的布局.我将页面类后缀添加到模板的 BODY 标记中,以便我可以单独控制每个页面.

You can also use a combination of the Page Class Suffix that you can set on the System Parameters of a menu item and CSS to control the layout of the page. I add the Page Class Suffix to the BODY tag of my templates so I can control every page individually.

首先,您需要确定您所在的菜单项:

First, you need to figure out which menu item you are on:

<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = $params->get( 'pageclass_sfx' );
endif;
?>

然后您需要将其作为 ID 添加到 BODY 标签中:

Then you need to add that to the BODY tag as an ID:

<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">

现在您可以使用模块位置和 CSS 来控制每个页面.您可以实现截然不同的布局,而无需返回并触摸代码.

Now you can use module positions and CSS to control every page. You can achieve vastly different layouts without having to go back in and touch code.

相关文章