城市简码

tech2023-01-24  65

城市简码

The proliferation of WordPress sites throughout the Web has delivered some amazing  page designs that are striking in their originality, helping to create a unique web presence.

整个Web站点中WordPress网站的激增提供了一些令人惊叹的页面设计,这些页面设计具有独创性,有助于创建独特的Web形象。

A general benefit of WordPress is that it allows people with little code experience the opportunity to load a theme, install some plugins and deliver a competent and visually appealing website.

WordPress的一般好处是,它使几乎没有代码经验的人就有机会加载主题,安装一些插件并提供一个功能强大且外观吸引人的网站。

However, if you aspire to creating a unique web page design, you can learn to customise your page designs. Today we are going to see how some of these amazing page designs are created within the WordPress themes, and help you create your own design templates to suit various situations.

但是,如果您想创建一个独特的网页设计,则可以学习自定义您的网页设计。 今天,我们将看到如何在WordPress主题中创建其中一些惊人的页面设计,并帮助您创建适合各种情况的自己的设计模板。

这些设计如何运作? (How Do These Designs Work?)

Each theme contains its own type of implementation for the custom page designs.

每个主题都包含其自己的用于自定义页面设计的实现类型。

Basically, there are two main techniques for providing custom page designs.

基本上,提供定制页面设计有两种主要技术。

页面模板 (Page Templates)

There are certain type of pages for displaying categories, posts, and pages inside a default theme. We can add our own page designs using WordPress page templates. All you have to do is define the template name as a comment on top of the page. The following code contains a basic page template.

某些类型的页面用于显示默认主题内的类别,帖子和页面。 我们可以使用WordPress页面模板添加自己的页面设计。 您要做的就是将模板名称定义为页面顶部的注释。 以下代码包含一个基本页面模板。

<?php /* Template Name: My Custom Template */ get_header(); ?>   // Content   <?php get_sidebar(); ?>   <?php get_footer(); ?>

Once the template is defined, you can assign it to a new page being added, as shown in the following screen.

定义模板后,您可以将其分配给要添加的新页面,如以下屏幕所示。

简码 (Shortcodes)

Shortcodes are reusable piece of code snippets which acts as shortcuts to provide complex functionalities and display options.

短代码是可重用的一段代码,用作提供复杂功能和显示选项的快捷方式。

Generally, shortcode design templates  are placed inside the page editor and you can add or modify with necessary data to generate custom designs.

通常,短代码设计模板放置在页面编辑器中,您可以添加或修改必要的数据以生成自定义设计。

The following image shows a shortcode used for creating a design with tabs.

下图显示了用于使用标签创建设计的简码。

Each of the above approaches has its own pluses and minuses in different situations. Choosing the right one for the right occasion is the challenge as a WordPress developer.

上述每种方法在不同情况下都有其自身的优缺点。 在合适的场合选择合适的人是作为WordPress开发人员的挑战。

Inexperienced developers tend to misuse both of these techniques, so here we will be looking at the things to consider before choosing shortcodes or page templates to provide custom designs.

没有经验的开发人员往往会滥用这两种技术,因此在这里,我们将在选择短代码或页面模板以提供自定义设计之前考虑要考虑的事项。

自定义设计如何适合页面? (How Does Custom Design Fit into Pages?)

A normal page in WordPress consists of several sub templates such as header, sidebar, content and footer.

WordPress中的普通页面由几个子模板组成,例如页眉,侧边栏,内容和页脚。

First, we are going to look at the structure of a web page using the following screen.

首先,我们将使用以下屏幕来查看网页的结构。

Pages will contain a header, a footer and content as mandatory components. The sidebar is optional and we can align it to the right or left side depending on our preference.

页面将包含页眉,页脚和内容作为必选组件。 侧边栏是可选的,我们可以根据自己的喜好将其与右侧或左侧对齐。

Now, let’s see how custom designs created with shortcodes and page templates fit into the above page.

现在,让我们看看使用短代码和页面模板创建的自定义设计如何适合以上页面。

Shortcodes can be inserted inside the content editor in the page creation area on the admin dashboard. Therefore, any design generated from a shortcode will apply to the page content only. We cannot use shortcodes to create custom designs for header, footer and sidebar, unless we customize the template files.

可以在管理控制台的页面创建区域中的内容编辑器中插入简码 。 因此,任何由简码生成的设计都将仅应用于页面内容。 除非我们自定义模板文件,否则我们不能使用简码为页眉,页脚和侧栏创建自定义设计。

Page template is placed inside the theme as a separate file. So we can customize the page template file to add different types of designs for each of the four components displayed in the above screen.

页面模板作为单独的文件放置在主题内。 因此,我们可以自定义页面模板文件,为上述屏幕中显示的四个组件的每一个添加不同类型的设计。

结论 (Conclusion)

Page templates should be preferred over shortcodes in situations where we need a custom design for the complete page. Shortcodes are suitable for adding custom designs to the main content area.

在我们需要对整个页面进行自定义设计的情况下,应该优先使用页面模板而不是短代码。 简码适合将定制设计添加到主要内容区域。

将数据填充到定制设计中 (Populating Data into Custom Designs)

Theme developers can’t just create design templates with sample data and ask the users to manage their own data by changing the source files. WordPress is a CMS and users should be able to populate data for custom designs using the admin area.

主题开发人员不仅可以使用示例数据创建设计模板,还不能要求用户通过更改源文件来管理自己的数据。 WordPress是CMS,用户应该可以使用管理区域来填充自定义设计的数据。

WordPress provides the ability to use custom fields for adding dynamic data to pages. But non technical users will not be familiar with adding necessary keys and values to custom fields.

WordPress提供了使用自定义字段向页面添加动态数据的功能。 但是非技术用户不会熟悉向自定义字段添加必要的键和值。

Therefore, most theme designers use theme’s options panel to populate data to the predefined page templates. The disadvantage of this approach is that you can only have one page per page template. Allowing data for multiple pages  will make the options panel very complex to handle.

因此,大多数主题设计人员使用主题的选项面板将数据填充到预定义的页面模板中。 这种方法的缺点是每个页面模板只能有一页。 允许多页数据将使选项面板的处理非常复杂。

Following is a sample options panel used to populate data to a home page.

以下是一个示例选项面板,用于将数据填充到主页。

On the other hand, populating data with shortcodes is much easier. In the tinyMCE editor we can create custom buttons to insert shortcodes. Once the button is clicked, shortcode will be placed inside the page editor and you can populate the shortcode with necessary data as required.

另一方面,用短代码填充数据要容易得多。 在tinyMCE编辑器中,我们可以创建自定义按钮来插入短代码。 单击按钮后,简码将放置在页面编辑器中,您可以根据需要用必要的数据填充简码。

The following image shows a content editor with shortcode buttons for generating custom designs.

下图显示了带有短代码按钮的内容编辑器,用于生成自定义设计。

This is the most preferred and widely used technique for providing custom designs.We can create multiple pages with the same shortcode design template and populate relevant data as attributes.

这是提供定制设计的最优选和广泛使用的技术。我们可以使用相同的短代码设计模板创建多个页面,并填充相关数据作为属性。

结论 (Conclusion)

Shortcodes are much more effective in scenarios when you need to use the same design template across multiple pages.

当您需要在多个页面中使用相同的设计模板时,短代码在方案中会更加有效。

将定制设计添加到现有主题 (Adding Custom Designs to Existing Themes)

Premium themes typically contain a set of built in page templates or custom page designs. However, let’s assume we want to create a new design templates that is not available by default.

优质主题 包含一组内置页面模板或自定义页面设计。 但是,假设我们要创建一个默认情况下不可用的新设计模板。

Let’s look at the possible solutions with both shortcodes and page templates.

让我们看一下使用短代码和页面模板的可能解决方案。

Page Templates

页面模板

We can create the new design as a page template and copy it into the theme folder to get it working instantly. Each and every page template is placed inside the active theme folder. As soon as we change the theme, all the pages with our new design will fall apart since the page template is not available in the new theme.

我们可以将新设计创建为页面模板,然后将其复制到主题文件夹中,以使其立即起作用。 每个页面模板都放置在活动主题文件夹中。 更改主题后,具有新设计的所有页面都会崩溃,因为新主题中没有页面模板。

Shortcodes

简码

In this case we can create the new design as a shortcode and use it in a plugin instead of including in theme files, allowing us to use it across multiple themes as a shortcode.

在这种情况下,我们可以将新设计创建为简码,并在插件中使用它,而不是将其包含在主题文件中,从而使我们可以在多个主题中将其用作简码。

结论 (Conclusion)

Whenever you are in need of a new design template, create a shortcode inside a plugin to make templates compatible with multiple themes.

每当您需要新的设计模板时,请在插件内创建一个简码,以使模板与多个主题兼容。

如何在简码和页面模板之间进行选择 (How to Choose Between Shortcodes and Page Templates)

In the previous sections we discussed three different areas to be considered when choosing between shortcodes or page templates. Now let’s look at some general guidelines to make sure we use each of these techniques effectively.

在前面的部分中,我们讨论了在短代码或页面模板之间进行选择时要考虑的三个不同领域。 现在,让我们看一些通用准则,以确保我们有效地使用每种技术。

可重用性 (Reusability)

Shortcodes are much more suitable in situations where you want the same template to be used multiple times. Page templates are more suitable for single static pages.

在您希望多次使用同一模板的情况下,简码更适合。 页面模板更适合单个静态页面。

Pages such as Home, About, Contact will be used as standalone pages on most occasions. Hence, page templates are suitable for these pages.

在大多数情况下,“主页”,“关于”,“联系人”等页面将用作独立页面。 因此,页面模板适用于这些页面。

Dynamic pages such as Services, Portfolio, Projects are better created as shortcode templates.

更好地将动态页面(如服务,项目组合,项目)创建为短代码模板。

相依性 (Dependency)

Shortcodes can be used as independent components from the theme while page templates depend on the theme and styles.  If you are creating a design template for common usage, make sure to create it as shortcode inside a plugin to make it cross theme compatible.

短代码可用作主题的独立组件,而页面模板取决于主题和样式。 如果要创建通用设计模板,请确保在插件内将其创建为简码,以使其跨主题兼容。

可移植性 (Portability)

Shortcode templates are portable compared to page templates. You can place shortcodes anywhere in a page in any number of times. On the other hand you need a complete page to use a page template design.

与页面模板相比,简码模板是可移植的。 您可以在页面中的任意位置多次放置简码。 另一方面,您需要一个完整的页面才能使用页面模板设计。

易用性 (Usability)

Page templates are provided from the theme.  As a user, all you have to do is select the page template for any given page.

页面模板是从主题提供的。 作为用户,您要做的就是为任何给定页面选择页面模板。

Shortcodes and the necessary data are placed inside the page content editor. So there is a strong possibility that you might get the content erased by mistake. In such cases all the data with complete design will be lost.

简码和必要的数据放置在页面内容编辑器中。 因此,很可能会误删除内容。 在这种情况下,所有具有完整设计的数据都将丢失。

Page templates are permanent and hard to change by mistake, compared to shortcodes. Therefore page templates are much easier and safer to use in users perspective.

与短代码相比,页面模板是永久性的,很难被错误地更改。 因此,从用户角度来看,页面模板使用起来更加容易和安全。

总体结论 (Overall Conclusion)

There is no clear conclusion on what’s the best method for designing custom templates. Most often, it depends on combination of factors mentioned throughout this article. You have to focus on the situation and choose what suits the best in the given situation.

有什么是设计的自定义模板的最佳方法尚无明确结论。 通常,这取决于本文中提到的因素的组合。 您必须专注于情况并选择在给定情况下最适合的情况。

I hope my reflections have left you a little more able to understand the differences in using Shortcodes and Page Templates, and which to use for your own dazzling custom page designs.

我希望我的思考能使您更多地了解使用简码和页面模板的区别,并将其用于您自己令人眼花custom乱的自定义页面设计。

Now, you should be able to choose between shortcodes or page templates for the following three scenarios.

现在,您应该能够在以下三种情况下的短代码或页面模板之间进行选择。

Designing a 3 column page without sidebar

设计没有侧栏的3列页面 Designing an advertising page for your blog

为您的博客设计广告页面 Designing a pricing table to display package pricing for different products and services

设计定价表以显示不同产品和服务的包装价格

I look forward to your answers – and reasons – in the comments section below.

我期待在下面的评论部分中您的回答以及原因。

翻译自: https://www.sitepoint.com/wordpress-page-design-shortcodes-v-page-templates/

城市简码

相关资源:wordpress主题模板:追格时光轴购物主题 v3.7.rar
最新回复(0)