城市简码_如何在WordPress中添加简码? (入门指南)

城市简码

Shortcodes are an easy way to add dynamic content into your WordPress posts, pages, and sidebars.

简码是将动态内容添加到WordPress帖子,页面和侧边栏中的简便方法。

Many WordPress plugins and themes use shortcodes to add specialized content like contact forms, image galleries, sliders, and more.

许多WordPress插件和主题使用短代码来添加特殊内容,例如联系表单,图像库,滑块等。

In this article, we will show you how to easily add a shortcode in WordPress. We will also show you how to create your own custom shortcodes in WordPress.

在本文中,我们将向您展示如何轻松地在WordPress中添加简码。 我们还将向您展示如何在WordPress中创建自己的自定义短代码。

Adding a shortcode in WordPress
什么是简码? (What are Shortcodes?)

Shortcodes in WordPress are code shortcuts that help you add dynamic content in WordPress posts, pages, and sidebar widgets. They are displayed inside square brackets like this:

WordPress中的短代码是代码快捷方式,可帮助您在WordPress帖子,页面和侧边栏小部件中添加动态内容。 它们显示在方括号内,如下所示:

[myshortcode]

[myshortcode]

To better understand shortcodes, lets take a look at the background of why they were added in the first place.

为了更好地理解短代码,让我们来看看为什么首先添加短代码的背景。

WordPress filters all content to make sure that no one uses posts and page content to insert malicious code in the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.

WordPress过滤所有内容,以确保没有人使用帖子和页面内容在数据库中插入恶意代码。 这意味着您可以在帖子中编写基本HTML,但不能编写PHP代码。

But what if you wanted to run some custom code inside your posts to display related posts, banner ads, contact forms, galleries, etc?

但是,如果您想在帖子中运行一些自定义代码以显示相关的帖子,横幅广告,联系表,画廊等,该怎么办?

This is where Shortcode API comes in.

这就是Shortcode API的来源。

Basically, it allows developers to add their code inside a function and then register that function with WordPress as a shortcode, so users can easily use it without having any coding knowledge.

基本上,它使开发人员可以将他们的代码添加到一个函数中,然后将该函数作为简码注册到WordPress,因此用户可以轻松使用它而无需任何编码知识。

When WordPress finds the shortcode it will automatically run the code associated with it.

当WordPress找到简码时,它将自动运行与其关联的代码。

Let’s see how to easily add shortcodes in your WordPress posts and pages.

让我们看看如何在WordPress帖子和页面中轻松添加短代码。

在WordPress帖子和页面中添加简码 (Adding a Shortcode in WordPress Posts and Pages)

First, you need to edit the post and page where you want to add the shortcode. After that, you need to click on the add block button to insert a shortcode block.

首先,您需要编辑要在其中添加简码的帖子和页面。 之后,您需要单击添加块按钮以插入一个简码块。

Adding shortcode block in WordPress

After adding the shortcode block, you can simply enter your shortcode in the block settings. The shortcode will be provided by various WordPress plugins that you might be using such as WPForms for contact forms, OptinMonster for email marketing forms, WP Call button for inserting a click to call button, etc.

添加简码块后,您只需在块设置中输入简码即可。 短码将通过各种WordPress插件来提供,你可能如使用是WPForms的接触形式 , OptinMonster对于电子邮件营销的形式, WP呼叫按钮插入一个点击通话按钮 ,等等。

Insert shortcode in the block editor

To learn more about using blocks, see our Gutenberg tutorial for more details.

要了解有关使用块的更多信息,请参见我们的Gutenberg教程以获取更多详细信息。

You can now save your post or page and preview your changes to see the shortcode in action.

现在,您可以保存您的帖子或页面并预览更改,以查看正在使用的短代码。

在WordPress侧边栏小部件中添加简码 (Adding a Shortcode in WordPress Sidebar Widgets)

You can also use shortcodes in WordPress sidebar widgets. Simply visit the Appearance » Widgets page and add the ‘Text’ widget to a sidebar.

您还可以在WordPress侧边栏小部件中使用简码。 只需访问外观»小部件页面,然后将“文本”小部件添加到侧边栏中。

Now you can paste your shortcode inside the text area of the widget.

现在,您可以将您的简码粘贴到小部件的文本区域内。

Adding shortcode in sidebar widget

Don’t forget to click on the ‘Save’ button to store your widget settings.

不要忘记单击“保存”按钮来存储您的小部件设置。

After that, you can visit your WordPress website to see the live preview of the shortcode in the sidebar widget.

之后,您可以访问WordPress网站以在侧边栏小部件中查看短代码的实时预览。

在旧的WordPress经典编辑器中添加简码 (Adding a Shortcode in Old WordPress Classic Editor)

If you are still using the old classic editor in WordPress, then here is how you can add shortcodes to your WordPress posts and pages.

如果您仍在WordPress中使用旧的经典编辑器 ,则可以通过以下方法将短代码添加到WordPress帖子和页面中。

Simply edit the post and page where you want to add the shortcode. You can paste the shortcode anywhere inside the content editor where you want it to be displayed. Just make sure the shortcode is in its own line.

只需编辑您要在其中添加简码的帖子和页面。 您可以将短代码粘贴到内容编辑器中希望显示的任何位置。 只要确保简码在自己的行中即可。

Shortcode classic editor

Don’t forget to save your changes. After that you can preview your post and page to see the shortcode in action.

不要忘记保存您的更改。 之后,您可以预览您的帖子和页面以查看正在使用的简码。

如何在WordPress主题文件中添加简码 (How to Add a Shortcode in WordPress Theme Files)

Shortcodes are meant to be used inside WordPress posts, pages, and widgets. However, sometimes you may want to use a shortcode inside a WordPress theme file.

简码旨在在WordPress帖子,页面和小部件内使用。 但是,有时您可能想在WordPress主题文件中使用简码。

WordPress makes it easy to do that, but you will need to edit your WordPress theme files. If you haven’t done this before, then see our guide on how to copy and paste code in WordPress.

WordPress可以轻松做到这一点,但是您将需要编辑WordPress主题文件。 如果您以前没有做过,请参阅有关如何在WordPress中复制和粘贴代码的指南。

Basically, you can add a shortcode to any WordPress theme template by simply adding the following code.

基本上,您只需添加以下代码即可向任何WordPress主题模板添加简码。


<?php echo do_shortcode("[your_shortcode]"); ?>

WordPress will now look for the shortcode and display its output in your theme template.

WordPress现在将寻找简码并在主题模板中显示其输出。

如何在WordPress中创建自己的自定义简码 (How to Create Your Own Custom Shortcode in WordPress)

Shortcodes can be really useful when you want to add dynamic content or custom code inside the WordPress post and pages. However, if you want to create a custom shortcode, then it requires some coding experience.

当您想在WordPress帖子和页面中添加动态内容或自定义代码时,简码会非常有用。 但是,如果要创建自定义的简码,则需要一定的编码经验。

If you are comfortable with writing PHP code, then here is a sample code that you can use as a template.

如果您愿意编写PHP代码,那么这里是示例代码,您可以将其用作模板。


// function that runs when shortcode is called
function wpb_demo_shortcode() { 

// Things that you want to do. 
$message = 'Hello world!'; 

// Output needs to be return
return $message;
} 
// register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode'); 

In this code, we first created a function that runs some code and returns the output. After that, we created a new shortcode called ‘greeting’ and told WordPress to run the function we created.

在此代码中,我们首先创建了一个运行一些代码并返回输出的函数。 之后,我们创建了一个名为“ greeting”的新短代码,并告诉WordPress运行我们创建的功能。

You can now use add this shortcode to your posts, pages, and widgets using the following code:

现在,您可以使用以下代码,将此简码添加到帖子,页面和小部件中:

[greeting]

[问候]

It will run the function you created and show the desired output.

它将运行您创建的函数并显示所需的输出。

Now let’s take a look at a more practical usage of a shortcode. In this example, we will display a Google AdSense banner inside a shortcode.

现在让我们看一下短代码的更实际用法。 在此示例中,我们将在简码内显示Google AdSense标语 。



// The shortcode function
function wpb_demo_shortcode_2() { 

// Advertisement code pasted inside a variable
$string .= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-format="fluid"
     data-ad-layout="in-article"
     data-ad-client="ca-pub-0123456789101112"
     data-ad-slot="9876543210"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>';

// Ad code returned
return $string; 

}
// Register shortcode
add_shortcode('my_ad_code', 'wpb_demo_shortcode_2'); 


Don’t forget to replace the ad code with your own advertisement code.

不要忘记用您自己的广告代码替换广告代码。

You can now use the [my_ad_code] shortcode inside your WordPress posts, pages, and sidebar widgets. WordPress will automatically run the function associated with the shortcode and display the advertisement code.

现在,您可以在WordPress帖子,页面和侧边栏小部件内使用[my_ad_code]简码。 WordPress将自动运行与短代码关联的功能并显示广告代码。

简码与古腾堡块 (Shortcodes vs Gutenberg Blocks)

We’re often asked by users about the differences between shortcode vs the new Gutenberg blocks.

用户经常向我们询问简码与新的Gutenberg块之间的区别。

Basically if you find shortcodes useful, then you’ll love WordPress editor blocks. Blocks allow you to do the same thing but in a more user-friendly way.

基本上,如果您发现短代码有用,那么您会喜欢WordPress编辑器块的。 块允许您以更用户友好的方式执行相同的操作。

Instead of requiring users to add a shortcode for displaying dynamic content, blocks allow users to add dynamic content inside posts / pages with a more intuitive user interface. A lot of popular WordPress plugins are switching to using Gutenberg blocks instead of shortcodes because they’re more beginner friendly.

块不要求用户添加用于显示动态内容的简码,而是允许用户使用更直观的用户界面在帖子/页面内添加动态内容。 许多流行的WordPress插件都开始使用古腾堡块而不是短代码,因为它们对初学者更加友好。

We have put together a list of the most useful Gutenberg block plugins for WordPress that you may want to try.

我们列出了您可能想尝试的最有用的WordPress古腾堡块插件列表。

If you want to create your own custom Gutenberg blocks, you can follow our step by step tutorial on how to create custom Gutenberg blocks in WordPress.

如果您想创建自己的自定义古腾堡块,可以按照我们的分步教程学习如何在WordPress中创建自定义古腾堡块 。

We hope this article helped you learn how to add a shortcode in WordPress. You may also want to see our guide on the best drag & drop WordPress page builder plugins, and how to create a custom WordPress theme without writing any code.

我们希望本文能帮助您学习如何在WordPress中添加简码。 您可能还想查看有关最佳拖放WordPress页面构建器插件的指南 ,以及如何在不编写任何代码的情况下创建自定义WordPress主题 。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-a-shortcode-in-wordpress/

城市简码

发票OFD格式转换成PDF 阅读详情

相关推荐

LVGL字体优化实战:如何将中文字库放到外部SPI Flash并动态加载(节省内部RAM)

本文详细介绍了LVGL在嵌入式UI开发中如何通过外部SPI Flash存储中文字库并动态加载,以节省内部RAM资源。通过对比不同存储介质和字库格式,提供多种实现方案及性能优化技巧,帮助开发者在资源受限设备上实现高效的中文显示。

weixin_28693683的博客 352

【WP|4】WordPress 简码(Shortcode)开发详解

简码是 WordPress 提供的一项强大功能,能够帮助开发者创建灵活、可复用的内容块。通过本文的讲解,你可以了解从基础到高级的简码开发技巧。掌握这些技巧,可以让你在开发 WordPress 主题和插件时,更加高效地实现各种动态内容和复杂功能。希望这篇文章对你的简码开发之路有所帮助。

颜淡慕潇 6837

Zephyr RTOS详解

Zephyr 是一个开源的实时操作系统(RTOS),专为资源受限的嵌入式设备设计,支持多种架构(如 ARM Cortex-M、RISC-V、x86 等)。

weixin_43484713的博客 4124

城市简码_在WordPress中使用简码的7个基本技巧

城市简码Shortcodes in WordPress allow you to add various functionality into posts, pages, and widgets without actually writing any code. Hence the name, Shortcode. Many WordPress plugins and some themes u...

735

AI 小说生成器本地部署完全指南

这篇文章介绍了本地部署AI小说生成器的完整指南,主要面向网文作者、内容创作者和AI爱好者。文章从环境搭建到创作实践,详细讲解了三种主流部署方案:Ollama(推荐新手)、LMStudio(图形界面)和text-generation-webui(高级定制)。内容涵盖硬件需求、系统架构、模型选择、API对接、提示词工程等关键技术环节,并提供了小说创作的特制提示词模板和参数调优建议。文章还讨论了长篇小说管理、多模型协作等进阶技巧,以及版权伦理等法律问题。最后推荐了相关工具资源和社区论坛,帮助读者实现安全、高效、可

珂珂可爱多 2385

飞书日历同步Outlook翻车?我踩过的坑和最终用Google Calendar成功的完整配置记录

本文详细记录了从Outlook日历同步到飞书时遇到的常见问题及解决方案,特别是通过Google Calendar实现稳定同步的完整配置流程。文章揭示了Outlook同步方案易翻车的原因,并提供了Google Calendar的避坑指南、高级技巧和企业级部署建议,帮助用户高效实现日历同步。

weixin_33724046的博客 319

创建 WordPress 插件(第三部分):添加简码

在这个示例中,我们将使用 [pdf id="awjune2003"] 简码,将之前转换的 PDF 嵌入到网页中。其次,我们使用了 WordPress 的 shortcode_atts() 函数,从中获取我们需要的属性数组。注册简码的代码使用了 add_shortcode() 方法,其中需要指定简码的标识符(在这个例子中是“pdf”),以及用于处理简码的方法。首先,$atts 被添加为方法的一个参数,它是一个数组,包含了简码中的所有属性。在我们的例子中,这个数组只有一个属性 “id”,表示 PDF 的名称。

2401_87189539的博客 547

JMeter压测Dubbo接口的完整落地方案(适配3.x)

Dubbo作为主流Java微服务RPC框架,其接口压测长期面临协议私有、注册发现复杂、序列化耦合度高等技术门槛。理解Dubbo基于TCP长连接、泛化调用与SPI扩展机制的核心原理,是实现稳定压测的前提;技术价值在于绕过HTTP网关直击服务核心链路,保障大促等关键场景的性能可信度;典型应用场景包括电商订单链路压测、风控/库存等内部Dubbo服务SLA验证,以及CI/CD中性能左移实践。本文聚焦Dubbo 3.x版本,提供零第三方插件、可复用、避坑的JMeter Java Sampler落地路径。

weixin_33700809的博客 555

wordpress相关简码和过滤代码

wordpress根据分类显文章,根据语言显示文字,tag标签简码,最新,热门,全部等

537

终端以太网接入Internet实验

实验过程: 1.布局 2.连接 3.配置Route0和Route1 4.配置RIP 5.static: 6.查看Route0,Route1的路由表: 7.完成PC2和server0 IP地址,子网掩码和默认网关地址的配置: 8.命令行: 9.启动PC0桌面下的PPPoe宽带连接程序: 10.启动PC1桌面下的PPPoe宽带连接程序: 11.验证PC0和PC2的ICMP报文传输过程 12.验证PC1和server0的ICMP报文传输过程 关键命令说明

chaojidagouzi的博客 4086

本地部署大模型框架 LMStudio和Ollama+WebUI对比

之前我们的辣妈系列文章介绍了很多如何使用Ollama框架+OpenWebUI做很多大模型框架下相关的事情。之后我们也尝试了LMStudio这样一体化的工具。本文介绍下LMStudio 和 Ollama+Web UI 各有优劣,具体选择取决于你的需求。

ms44的专栏 4038

wordpress简代码(短代码、shortcode)

会做WordPress主题的人应该都知道,使用include(TEMPLATEPATH. “xxx.php”)这样的PHP代码可以调用xxx.php中的内容,从而让主题结构更有条理性。 但可能很多人不知道WordPress短代码(WordPress ShrotCode API), 它从WordPress 2.5开始应用,可以让开发设计人员通过简单的函数集合创建宏代码来生成内容。代码形式类

clh604的专栏 4521

玩转 InternVL3.5 轻量级实战:从部署到优化的全记录

玩转 InternVL3.5 轻量级实战:从部署到优化的全记录

jacke121的专栏 629

城市简码_WordPress的自定义简码

城市简码 什么是简码? (What Are Shortcodes?) Shortcodes are a convenient method that WordPress implements which provides a way to generate dynamic and often complex functionality from very little input. 短代码是Wo...

culi4814的博客 809

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

城市简码_如何在WordPress主题中使用简码

城市简码Recently, we got a request from a user asking how they could use WordPress shortcodes in their WordPress themes. Shortcode is a special tag that you can enter into a post that gets replaced with d...

359

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

城市简码_在两分钟内为WordPress创建自定义简码

城市简码 I love shortcodes. If there’s a time to drop the childish quip, “Then why don’t you marry it?”— now would be the right time. That’s how much I love shortcodes in WordPress. 我喜欢简码。 如果有时间放下幼稚的...

culi4814的博客 504

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

城市简码_如何使用简码在WordPress中添加Twitter Bootstrap CSS

城市简码Adding CSS elements like tooltips, colorful buttons, and roll-over effects can help your content stand out. The problem is that most people don’t know how to use CSS to add colorful buttons, table...

cumohuo9136的博客 181

城市简码_释放WordPress简码API的强大功能

城市简码WordPress introduced the Shortcode API in its version 2.5. This API allows developers to add some more or less complex features in their plugins or themes without the user having to insert any HTM...

culi4814的博客 329

城市简码_如何为编辑者创建自定义WordPress简码

城市简码This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible. 本文是与SiteGround合作创建的系列文章的一部分。 感谢您支持使SitePoint成为可能的合作伙伴。 ...

culh2177的博客 282

城市简码_WordPress页面设计:简码v页面模板

城市简码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...

culi3182的博客 497

城市简码_创建WordPress简码

城市简码WordPress shortcodes are super handy, especially when handing off a WordPress-based website to a client. The alternative to using shortcodes is creating complicated templates, and even then, you ...

culuo8053的博客 183

城市简码_如何在WordPress中创建嵌套的简码

城市简码When an editor adds a WordPress [shortcode] to a post or page, it’s replaced by the returned output of a handler function within a plug-in or the theme’s functions.php file. Let’s create a simple ...

culh2177的博客 330

城市简码_如何使用WordPress搜索简码在帖子中添加搜索表单

城市简码Recently one of our regular users ask if there was an easy way to add the WordPress search form in your post or page content using a shortcode. Well, the answer to that question is Yes. In this ar...

cumohuo9136的博客 253

城市简码_如何从WordPress帖子中查找和删除未使用的简码

城市简码Shortcodes are great, but they are not always the best way. One disadvantage of using a plugin or theme that relies on shortcodes is that when you switch a theme or deactivate the plugin, they wil...

306

wordpress添加媒体_如何在WordPress中添加丰富的联系信息

wordpress添加媒体Web search engines like Google or Bing are trying to pull more and more semantic from the content of the websites they index every day. Specific markups like microdata and microformats ha...

776

wordpress添加媒体_如何向WordPress添加类别和子类别

WordPress comes with the ability to sort your content into categories, tags, and taxonomies. One of the major difference between categories and tags is that categories can have subcategories or child ...

cumohuo9136的博客 345
上一篇: 传智播客免费源码_2020年7个最佳播客托管比较(大多数是免费的)
下一篇: 隐藏wordpress后台_如何在WordPress中从首页隐藏帖子
cumohuo9136
博客等级 码龄11年 56粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值