Skip to main content

用WordPress插件Polylang实现在博客文章页面自动显示语言切换开关

也许你和我这种挨踢砖家一样奇葩,用多种语言写博客。我的WordPress使用Polylang显示文章的中英文版本。一般我需要在文章里加一个"Language Switcher"标签来显示语言切换。添加这个标签不是那么容易,我得把它记在某个地方,需要的时候复制、粘贴到文章里。在每篇文章里添加这个标记有点儿麻烦。另外,这个标记必须配合"Read More"标记之后使用,如果语言切换标记出现在"Read More"之前会导致首页上出现多个语言切换标记,每个语言切换标记无法知道每篇文章对应的URL是多少。看看我是如何完美解决这个问题的吧...



既要在首页不显示又要在每篇文章里显示语言开关,这就需要在文章模板里调用插件了。文章模板是content-single.php。如果直接把插件标记放在模板里是没用的。感谢伟大的WordPress发布了一个函数the_widget()。该函数会根据输入的参数输出插件的内容。一共有三个输入的参数: $widget、$instance$args。

$widget是插件的PHP类名称。这个可不好找啊!我查看了这个插件的所有代码,最终在polylang/include/widget-languages.php这个文件里找到Polylang的PHP类名称叫PLL_Widget_Languages。另外我需要在文章里只显示其他语言的切换开关,当前语言的开关是不需要显示的。插件管理界面本身有个选项叫Hides the current language,可惜这个选项不是全局设定,所以我必须找到这个选项对应的变量名并输入到the_widget()。根据插件源代码,正确的写法是在$instance中添加,具体为the_widget( 'PLL_Widget_Languages','hide_current=1' )。最后一个参数$args是有关HTML代码的,不用管他,默认值就可以。

千言万语汇成一句话,你只要在content-single.php的某个位置添加代码the_widget( 'PLL_Widget_Languages','hide_current=1' )即可。哈哈!

Popular posts from this blog

Connect-NsxtServer shows "Unable to connect to the remote server"

When you run Connect-NsxtServer in the PowerCLI, it may show "Unable to connect to the remote server".  Because the error message is a little bit confusing with other login issues. It's not easy to troubleshoot. The actual reason is the NSX-T uses a self-signed certificate, and the PowerCLI cannot accept the certificate automatically. The fix is super easy. You need to set the PowerCLI to ignore the invalid certificate with the following command: Set-PowerCLIConfiguration -Scope User -InvalidCertificateAction:Ignore -Confirm:$false

Setup Terraform and Ansible for Windows provisionon CentOS

Provisioning Windows machines with Terraform is easy. Configuring Windows machines with Ansible is also not complex. However, it's a little bit challenging to combine them. The following steps are some ideas about handling a Windows machine from provisioning to post configuration without modifying the winrm configuration on the guest operating system. Install required repos for yum. yum -y install https://repo.ius.io/ius-release-el7.rpm yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm yum -y install epel-release yum -y install yum-utils yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo Install  Terraform . sudo yum -y install terraform Install  Ansible . sudo yum -y install ansible Install  Kerberos . yum -y install gcc python-devel krb5-devel krb5-libs krb5-workstation

How to List All Users in Terraform Cloud

Terraform has a rich API. However, the API documentation does not mention how to list all users. We can leverage the organization membership API and the PowerShell command  Invoke-RestMethod  to get a user list. 1. Create an organization token in Terraform Cloud. 2. Create the token variable ( $Token ) in PowerShell. $Token = "abcde" 3. Create the API parameters variable in PowerShell. $params = @{ Uri = "https://app.terraform.io/api/v2/organizations/ZHENGWU/organization-memberships?page%5Bsize%5D=100" Authentication = "Bearer" Token = $Token ContentType = "application/vnd.api+json" } Note: You need to replace ZHENGWU with your own organization name. And I used 100 at the end of the URI to retrieve the first 100 users. It can be any number.  4. Retrieve the API return and list the user's email address. $Test = Invoke-RestMethod @params $Test.data.attributes.email