5个时髦的WordPress代码片段可自定义您的评论

tech2023-01-06  113

First the phrase was “there’s an app for that”. Now, the prevailing sentiment is “there’s a plugin for that” — which isn’t always a good thing. Many of the customizations you envision for your WordPress comments section can easily be made in your theme files, so you can get the polished comments section you want without bloating your site with even more plugins.

首先,这句话是“为此有一个应用程序”。 现在,普遍的看法是“有一个插件” —这并不总是一件好事。 您为WordPress注释部分设想的许多自定义设置都可以轻松地在主题文件中进行,因此您可以获取所需的精美注释部分,而不会因使用更多插件而使网站膨胀。

Not a programmer? You don’t have to be. Below, you’ll find simple codes with clear customization instructions. If you’re having problems, check out the tips for suggestions on how to correct them.Please note that this was written for the Twenty Eleven theme, so if your site is based on another theme, some file names may be different.

不是程序员? 您不必如此。 在下面,您将找到带有清晰的自定义说明的简单代码。 如果遇到问题,请查看提示,以获取有关如何纠正问题的建议。 请注意,这是为“二十一”主题编写的,因此,如果您的网站基于另一个主题,则某些文件名可能会有所不同。

显示评论/引用数量 (Display the Number of Comments/Trackbacks)

Most templates begin their comments section with a message on how many comments there are. But what if you also want to display a comment tally somewhere else on the post, like the top? This code snippet lets you place a comment tally and message anywhere in the single page loop, and customize which message comes back for zero, one, and multiple responses.

大多数模板在其注释部分的开头都会显示一条消息,说明有多少条注释。 但是,如果您还想在帖子的其他位置(例如顶部)显示评论提示,该怎么办? 此代码段使您可以在单页循环中的任何位置放置注释提示和消息,并自定义返回的消息是否为零,一和多个响应。

Template file:

模板文件:

content-single.php

content-single.php

Raw Code:

原始代码:

<?php comments_number( 'no responses', 'one response', '% responses' ); ?>.

How to Use it:

如何使用它:

The raw code shows the most basic messages to display for zero, one, and multiple messages. The following example shows the message wrapped in a p tag with a short message (“Join the discussion!”) preceding the dynamic text. Simply switch out the raw code messages for what you want to say. Perhaps you want to be more conversational, like below:

原始代码显示了要显示的零,一和多条消息的最基本消息。 以下示例显示了包装在p标记中的消息,在动态文本之前带有一条短消息(“加入讨论!”)。 只需将原始代码消息切换为您要说的内容即可。 也许您想进行更多的对话,如下所示:

<p> Join the discussion! <?php comments_number( 'You get to have the first comment!', 'One comment so far - what can you add?', '% people have commented - add your two cents!' ); ?> </p>

Example output:

输出示例:

no comments: “You get to have the first comment!”

没有评论:“您将有第一条评论!”

one comment: “One comment so far – what can you add?”

一条评论:“到目前为止有一条评论-您能添加什么?”

multiple comments: “3 people have commented – add your two cents!”

多则评论:“有3个人发表了评论-加2分!”

Tips:

提示:

If the code breaks your page, check to see if you accidentally put apostrophes in the string text. If so, use the following HTML code to display apostrophes: &rsquo;

如果代码使您的页面中断,请检查是否在字符串文本中不小心加了撇号。 如果是这样,请使用以下HTML代码显示撇号: &rsquo;

Building on this knowledge, you can easily tweak the way your comments section displays its text. Just open your comments.php file and search for the text that is currently displaying (default for TwentyEleven begins with “One thought on…”, and replace the string text with your own.

基于此知识,您可以轻松调整注释部分显示其文本的方式。 只需打开您的comment.php文件并搜索当前显示的文本(TwentyEleven的默认值以“一个想法……”开头,然后用您自己的字符串文本替换)。

Learn more about this code:

了解有关此代码的更多信息:

http://codex.wordpress.org/Function_Reference/comments_number

http://codex.wordpress.org/Function_Reference/comments_number

自定义默认头像 (Customize Your Default Avatar)

Many blog visitors aren’t registered on Gravatar, so their avatar will show whatever your default is set as. If your comments section needs a little more personality than the ever-present Mystery Man, why not create one unique to your site?

许多博客访问者未在Gravatar上注册,因此其头像将显示您设置的默认值。 如果您的评论部分需要比永远存在的Mystery Man多一些个性,为什么不为您的网站创建一个独特的呢?

Template file:

模板文件:

functions.php

functions.php

Raw Code:

原始代码:

add_filter( 'avatar_defaults', 'new_default_avatar' ); function new_default_avatar ( $avatar_defaults ) { $new_avatar_url = get_bloginfo( 'template_directory' ) . '/images/new_default_avatar.png'; $avatar_defaults[$new_avatar_url] = 'My Custom Avatar'; return $avatar_defaults; }

How to Use it:

如何使用它:

Create your image at 60px by 60px, and upload it to the images folder in your theme.

以60px x 60px创建图片,并将其上传到主题中的images文件夹。

In the code, replace the image path and file name (‘/images/new_default_avatar.png’) with your own. If you want to name your default avatar, you can do so by replacing “Your New Default Avatar” with your chosen name. This name will only show up in your settings page, which we’re about to go to.

在代码中,用您自己的图像路径和文件名('/images/new_default_avatar.png')替换。 如果要命名默认头像,则可以使用所选名称替换“ Your New Default Avatar”。 此名称只会显示在您将要转到的设置页面中。

On your Dashboard, go to Settings > Discussion, where you should see your new custom avatar. Select it, click Save, and enjoy!

在控制台上,转到“设置”>“讨论”,您将在其中看到新的自定义头像。 选择它,单击保存,然后享受!

Tips:

提示:

If you must place your image somewhere other than the main template directory, or for some reason it won’t link properly, try replacing get_bloginfo( 'template_directory' ) . '/images/new_default_avatar.png'; with the full (http:// and all) URL leading to that image.

如果必须将映像放置在主模板目录之外的其他位置,或者由于某些原因无法正确链接,请尝试替换get_bloginfo( 'template_directory' ) . '/images/new_default_avatar.png'; get_bloginfo( 'template_directory' ) . '/images/new_default_avatar.png'; 带有指向该图像的完整(http://和所有)URL。

If you’d like to style the avatar with CSS, open your stylesheet and add a style for:  .commentlist .avatar {}

如果您想使用CSS设置头像样式,请打开样式表并为以下样式添加样式: .commentlist .avatar {}

Learn more about this code:

了解有关此代码的更多信息:

http://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress

http://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress

从联系表中删除URL或其他字段 (Remove URL or Other Fields from Contact Form)

The default fields in most contact forms are Name, Email, Website, and Comment. While all of those fields have excellent reasons for being there, some would prefer to leave out certain ones — most famously the website field, which spammers often try to take advantage of.

大多数联系表单中的默认字段是“姓名”,“电子邮件”,“网站”和“评论”。 尽管所有这些字段都有很好的理由,但是有些字段还是希望省略某些字段-最著名的是网站字段,垃圾邮件发送者经常试图利用这些字段。

This easy amendment to your functions.php file will take care of those unwanted fields in a flash!

这个对您的functions.php文件的简单修改将在一瞬间处理那些不需要的字段!

Template file:

模板文件:

functions.php

functions.php

Raw Code:

原始代码:

add_filter('comment_form_default_fields', 'remove_url'); function remove_url($val) { $val['url'] = ''; return $val; }

How to use it:

如何使用它:

This code, pasted as is, will remove the website field. Other fields can be removed by replacing ‘url’ with ’email’ or ‘author’ (name) in all three places it exists in the code.

此代码按原样粘贴,将删除网站字段。 通过在代码中存在的所有三个位置中将“ url”替换为“ email”或“ author”(名称),可以删除其他字段。

Learn more about this code:

了解有关此代码的更多信息:

http://codex.wordpress.org/Function_Reference/comment_form

http://codex.wordpress.org/Function_Reference/comment_form

防止您的Blog自行发送 (Keep Your Blog from Pinging Itself)

It’s nice to get trackbacks from sites that have linked to your article. It’s not nice to get pinged every time you link within your own website. Before you reach for that plugin, try adding this simple snippet to your functions.

从链接到您文章的网站上获取引用是很好的。 每次您在自己的网站中链接时都被ping通是不好的。 在接触该插件之前,请尝试将此简单代码段添加到您的函数中。

Template file:

模板文件:

functions.php

functions.php

Raw Code:

原始代码:

function disable_self_ping( &$links ) { foreach ( $links as $l => $link ) if ( 0 === strpos( $link, get_option( 'home' ) ) ) unset($links[$l]); } add_action( 'pre_ping', 'disable_self_ping' );

How to Use it:

如何使用它:

Simply paste the code at the end of your functions.php file. It doesn’t get much simpler!

只需将代码粘贴到functions.php文件的末尾。 它没有变得更简单!

Learn more about this code:

了解有关此代码的更多信息:

http://wp-snippets.com/disable-self-trackbacks/

http://wp-snippets.com/disable-self-trackbacks/

更改“留下回复”文本 (Change “Leave a Reply” Text)

If “Leave a Reply” sounds little cold and impersonal to you, you might consider changing it up with this code:

如果“留下答复”听起来有点冷漠和不客气,那么您可以考虑使用以下代码对其进行更改:

Template file:

模板文件:

comments.php

comments.php

Raw Code:

原始代码:

<?php comment_form(array('title_reply'=>'Put in Your Two Cents')); ?>

How to Use it:

如何使用它:

In your theme folder’s comments.php file, search for the <?php comment_form(); ?> line close to the bottom. Replace it with the raw code above.

在主题文件夹的comment.php文件中,搜索<?php comment_form(); ?> <?php comment_form(); ?>线靠近底部。 将其替换为上面的原始代码。

Now, simply change out ‘Put in Your Two Cents’ with whatever text you want to show there.

现在,只需将要在此处显示的任何文本更改为“放入两分钱”即可。

Learn more about this code:

了解有关此代码的更多信息:

http://wp-snippets.com/disable-self-trackbacks/

http://wp-snippets.com/disable-self-trackbacks/

Have fun with these snippets; hopefully they give your comments area that extra zing!

玩这些片段。 希望他们能给您的意见提供更多的帮助!

翻译自: https://www.sitepoint.com/5-funky-wordpress-snippets-to-customize-your-comments/

最新回复(0)