城市简码_在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 use shortcodes to add different elements such as pricing grid, event calendar, etc into WordPress. In this article, we will share 7 essential tips for using shortcodes in WordPress.

WordPress中的简码使您无需实际编写任何代码即可将各种功能添加到帖子,页面和小部件中。 因此,名称为Shortcode 。 许多WordPress插件和某些主题使用短代码向WordPress中添加不同的元素,例如定价网格,事件日历等。 在本文中,我们将分享7个在WordPress中使用短代码的基本提示。

Using Shortcodes
WordPress中的简码是什么? (What are Shortcodes in WordPress?)

Any content added in a WordPress site goes through some security checks and filters. These security checks and filters make sure that no malicious code goes into posts, pages, comments, etc. This means you can’t directly write code in these areas.

WordPress网站中添加的所有内容均经过一些安全检查和过滤器。 这些安全检查和过滤器可确保帖子,页面,评论等中没有恶意代码。这意味着您不能直接在这些区域中编写代码。

On the other hand, sometimes you just need to add code in these areas. Shortcodes provide a way to do that.

另一方面,有时您只需要在这些区域中添加代码。 简码提供了一种方法。

Shortcodes make it easier to add other elements into WordPress posts. For example, you can add a beautiful responsive slider using a simple shortcode. You can create a complex survey form in WordPress without writing a single line of code.

使用简码可以更轻松地将其他元素添加到WordPress帖子中。 例如,您可以使用简单的简码添加漂亮的响应式滑块 。 您可以在WordPress中创建复杂的调查表,而无需编写任何代码。

Having said that, lets take a look at some tips for using shortcodes in WordPress.

话虽如此,让我们看一下在WordPress中使用短代码的一些技巧。

提示1.知道何时不使用简码 (Tip 1. Know When Not to Use Shortcodes)

Shortcodes are great, but using shortcodes in every post is not a great idea. There are many WordPress themes out there that proudly claim to have 200+ shortcodes.

短代码很棒,但是在每个帖子中使用短代码并不是一个好主意。 有许多WordPress主题自豪地宣称拥有200多个短代码。

However if you use a shortcode in every post, then you are forever tied to the specific theme that’s providing the shortcode.

但是,如果您在每个帖子中都使用简码,那么您将永远与提供简码的特定主题联系在一起。

If you’re using a theme specific shortcode to create call-to-action buttons inside your posts or pages, then you should look at using our guide on how to add CSS buttons in WordPress without using shortcodes.

如果您使用主题特定的简码在帖子或页面内创建号召性用语按钮,那么您应该查看如何在WordPress中添加CSS按钮而不使用简码的指南。

If you’re using finding yourself adding the shortcode in every theme to add the same element such as a banner ad or signature text at the end of your post, then you may want to use a plugin or hire a developer to code that directly into your theme.

如果您发现自己在每个主题中都添加了简码,以在帖子末尾添加相同的元素(例如横幅广告或签名文字),那么您可能希望使用插件或雇用开发人员将其直接编码为您的主题。

This will make it easier to style that element and easily remove it should you decide to do that.

这样可以更轻松地设置该元素的样式,并在您决定执行此操作时轻松删除它。

Remember, if you use a shortcode in every post and later want to remove it, then you will have to edit all the posts to manually remove it. Although there is an easier way that we will show you later in this article.

请记住,如果您在每个帖子中都使用了一个短代码,以后又想删除它,那么您将必须编辑所有帖子以手动将其删除。 尽管有一种更简单的方法,我们将在本文后面向您展示。

提示2:将来验证您的简码 (Tip 2: Future Proof Your Shortcodes)

Shortcodes are great, but if it’s provided by your theme, then you may want to think twice about excessively using it. Why?

短代码很棒,但是如果您的主题提供了它,那么您可能要三思而后行地过度使用它。 为什么?

Because if you change your theme, then your next theme most likely will not have the same shortcode.

因为如果您更改主题,那么下一个主题很可能将没有相同的短代码。

The best way to prevent that is adding your in a site-specific plugin.

防止这种情况的最好方法是将您添加到特定站点的插件中

Simply copy and paste the shortcode snippet from your theme’s functions.php file, and then paste it in your site-specific plugin.

只需从主题的functions.php文件中复制并粘贴简短代码段,然后将其粘贴到特定于站点的插件中即可。

提示3:如何在WordPress主题中搜索简码 (Tip 3: How to Search for Shortcode in Your WordPress Theme)

In order to future proof your shortcode, you must know what the shortcode function looks like and how to find it in your theme.

为了将来证明您的简码,您必须知道简码函数的外观以及如何在主题中找到它。

First you need to open your theme’s folder which is usually found in /wp-content/themes/your-theme-name/

首先,您需要打开主题的文件夹,该文件夹通常位于/ wp-content / themes / your-theme-name /

You want to look inside the functions.php file or if the theme has an includes folder, then inside there.

您想查看functions.php文件内部,或者如果主题具有包含文件夹,则在其中。

Open the files and search for the term add_shortcode.

打开文件并搜索术语add_shortcode。

Here’s an example of what a shortcode snippet looks like:

这是一个简短代码片段的示例:


function my_shortcode_function() { 
$i = '<p>Hello World!</p>';
return $i;
} 
add_shortcode('my-shortcode', 'my_shortcode_function');

This code creates a shortcode ‘my-shortcode’, which returns a simple text greeting and can be embedded into a WordPress post or page like this:

这段代码创建了一个短代码“ my-shortcode”,该代码返回一个简单的文本问候,并且可以像这样嵌入到WordPress帖子或页面中:

[my-shortcode]

[my-shortcode]

Useful guide: how to create a shortcode in WordPress.

有用的指南:如何在WordPress中创建简码

提示4:在小部件中使用简码 (Tip 4: Using Shortcodes in Widgets)

Using a Shortcode in WordPress widgets

Often users think that shortcodes are limited to posts and pages, but they are not. You can use it inside your WordPress text widgets.

通常,用户认为短代码仅限于帖子和页面,但并非如此。 您可以在WordPress文本小部件中使用它。

Simply drag and drop a text widget to your sidebar and add your shortcode inside it.

只需将文本小部件拖放到侧边栏,然后在其中添加您的简码即可。

Remember, this feature is not enabled by default in WordPress. In case you can’t see your shortcode in a widget, then you need to add this code in your theme’s functions.php file or a site-specific plugin.

请记住,WordPress默认情况下未启用此功能。 如果您无法在小部件中看到您的短代码,则需要在主题的functions.php文件或特定于站点的插件中添加此代码。


add_filter('widget_text', 'do_shortcode');

技巧5.在主题文件中添加简码 (Tip 5. Add Shortcode in Theme Files)

If for some reason, you find a need to output the shortcode inside a non-widget area of your theme, then you can use your shortcodes there as well.

如果由于某种原因,您发现需要在主题的非小部件区域内输出简码,那么您也可以在其中使用简码。

Let’s assume you have created a custom page template, and you want to include a shortcode to display a contact form. Simply add your shortcode, like this:

假设您已经创建了一个自定义页面模板 ,并且想要包含一个短代码来显示联系表单。 只需添加您的简码,如下所示:


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

提示6.隐藏破损的简码 (Tip 6. Hiding a Broken Shortcode)

Often users change their themes without realizing that their old shortcodes will not work. Sometimes, they find out after months when a user visits their old post to find odd text there.

通常,用户在更改主题时没有意识到他们的旧短代码将无法使用。 有时,他们会在几个月后用户访问其旧帖子以在其中找到奇数文字时发现。

Well, you have two ways to fix it. You can either manually go and remove the shortcode from every post, or you can simply hide the broken shortcode.

好了,您有两种方法可以修复它。 您既可以手动删除每个帖子中的简码,也可以隐藏隐藏的简码。

All you need to do is add the following code in your theme’s functions.php file or a site-specific plugin.

您所需要做的就是在主题的functions.php文件或特定于站点的插件中添加以下代码。


add_shortcode( 'shortcodetag', '__return_false' );

This code adds back the orphan shortcode with no output. Don’t forget to replace shortcodetag with your shortcode name.

此代码添加回无输出的孤立短代码。 不要忘记将shortcodetag替换为您的简称。

技巧7.查找帖子中使用的简码 (Tip 7. Finding Shortcodes used in Posts)

In case you don’t want to use the hack in Tip 6, and rather want to remove all shortcodes manually, then the first step is to find all posts that are using the shortcode.

如果您不想使用技巧6中的技巧,而是想手动删除所有简码,则第一步是查找所有正在使用简码的帖子。

You can use this code in your theme’s functions.php file or a site-specific plugin to do the hard work for you.

您可以在主题的functions.php文件或特定于站点的插件中使用此代码来为您完成艰苦的工作。


function wpb_find_shortcode($atts, $content=null) { 
ob_start();
extract( shortcode_atts( array(
		'find' => '',
	), $atts ) );

$string = $atts['find'];

$args = array(
	's' => $string,
	);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
        echo '<ul>';
	while ( $the_query->have_posts() ) {
	$the_query->the_post(); ?>
	<li><a href="<?php  the_permalink() ?>"><?php the_title(); ?></a></li>
	<?php
	}
        echo '</ul>';
} else {
        echo "Sorry no posts found"; 
}

wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('shortcodefinder', 'wpb_find_shortcode'); 

This code simply creates a shortcode called shortcodefinder. It runs a WordPress query and lists posts with a given shortcode tag.

这段代码只是创建了一个称为shortcodefinder的简shortcodefinder 。 它运行一个WordPress查询并列出具有给定shortcode标签的帖子。

For example, if you wanted to find all posts containing shortcode [contact-form] then you would simply enter [shortcodefinder find=’contact-form’] in a WordPress page and save it. Now if you preview the page you will be able to see all the posts containing the shortcode.

例如,如果您想查找所有包含简码[contact-form]的帖子,则只需在WordPress页面中输入[shortcodefinder find ='contact-form']并将其保存。 现在,如果您预览页面,您将能够看到所有包含简码的帖子。

For more detailed instructions checkout our guide on how to find and hide unused shortcodes in WordPress.

有关更详细的说明,请查看我们的指南,了解如何在WordPress中查找和隐藏未使用的短代码

提示:我现在可以使用哪些简码? (Bonus Tip: Which Shortcodes Can I Use right Now?)

Often users don’t know which shortcodes are available to them. The shortcode reference plugin provides a list and details about available shortcodes in your WordPress site.

用户通常不知道他们可以使用哪些短代码。 简码参考插件提供了WordPress网站中可用简码的列表和详细信息。

We hope these tips helped you learn how to use shortcodes and make the most out of them in WordPress like a pro.

我们希望这些技巧能帮助您学习如何使用简码,并像专家一样充分利用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 Google+.

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

翻译自: https://www.wpbeginner.com/beginners-guide/7-essential-tips-for-using-shortcodes-in-wordpress/

城市简码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值