自定义帖子类型

时间:2016-02-27 23:35:09

标签: php wordpress

我正在为wordpress项目构建我的自定义帖子类型,但我不明白为什么在互联网上的某些例子中可能会找到类似这样的内容:

register_post_type('omb_prodotti',
    array(
      'labels' => array(
        'name' => __( 'customposttype' ),
        'singular_name' => __( 'customposttype' )
      )

什么是“__(”用于?是否与翻译有关?难道不能像这样写它:

register_post_type('omb_prodotti',
    array(
      'labels' => array(
        'name' => 'customposttype',
        'singular_name' => 'customposttype'
      )

很抱歉,我是PHP和构建自定义WP主题的初学者。

非常感谢。

2 个答案:

答案 0 :(得分:2)

在搜索引擎中搜索__是不可能的,但使用wordpress double underscore我在WordPress文档中找到了答案:

  

描述

     

从translate()中检索已翻译的字符串。

     

用法

     

<?php $translated_text = __( $text, $domain ); ?>

Source

正如您所写,这是翻译内容的功能。关于您的问题,如果您不调用__()函数,则不会翻译字符串,它只会显示customposttype。对于__('customposttype'),它将加载转换,这可能是一个相关的字符串。你必须查看代码才能找到相应的字符串。

答案 1 :(得分:1)

如果你准确地搜索WP文档,你可能很难找到它 - 除非你知道它的用途是什么,i18n

在那里你会发现这个函数用于国际化(又名i18n),或者将字符串翻译成其他语言。

这取决于您将使用代码的内容 - 如果不是您将分发给其他人使用的内容,那么您实际上并不需要担心它。

但是,如果它是您要发布的插件而其他人将使用,那么您实际上需要了解如何使用它。

WP中有四个功能可以执行此操作:

// Returns the translated string
__('My String to Translate', 'mytextdomain');

// Echos the translated string
_e('My String to Translate', 'mytextdomain');

// Returns the translated string, using different translations based on "domain"
_x('My String to Translate', 'mytextdomain', 'domain');

// Echos the translated string, using different translations based on "domain"

_ex('My String to Translate', 'mytextdomain');

请注意,翻译不是“自动” - 它是从主题或插件提供的文件中读取的(有人必须手动创建/翻译每个字符串)。

这篇文章非常有用:http://wpengineer.com/2237/whats-the-difference-between-__-_e-_x-and-_ex/

相关问题