php [WordPress] - 在CPT页面上替换gettext

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [WordPress] - 在CPT页面上替换gettext相关的知识,希望对你有一定的参考价值。

<?php
/*
Plugin Name: Replace gettext on CPT pages
Plugin URI: https://premium.wpmudev.org/
Description: Replace gettext on CPT pages
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/

//TAGS: WordPress, gettext
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_Replace_Gettext_On_CPT' ) ) {
	
	class WPMUDEV_Replace_Gettext_On_CPT {

		private static $_instance = null;
		public static $cpt = 'YOUR CPT';

		public static function get_instance() {

			if( is_null( self::$_instance ) ){
				self::$_instance = new WPMUDEV_Replace_Gettext_On_CPT();
			}

			return self::$_instance;
		}

		public function __construct() {

			add_filter('gettext', array($this, 'replace_gettext'), 20, 3);
			add_filter('gettext_with_context', array($this, 'replace_gettext_with_context'), 20, 4);

		}

		public function replacements(){

			$replacments = array(
				'default' => array(
					'Categories' => 'Replacement for Categories',
					'Post Comment' => 'Replacement for Post Comment',
					'Leave a Reply' => 'Replacement for Leave a Reply'
					),
				'twentyseventeen' => array(
					'Next Post' => 'Replacement for Next Post',
					'Next' => 'Replacement for Next',
					'Previous' => 'Replacement for Previous'
					)
			);

			return apply_filters( 'WPMUDEV_Replace_Gettext_On_CPT/replacements', $replacments );
		}


		public function replace_gettext(  $transtext, $normtext, $domain  ){			

			global $post;

			if( ! $post instanceof WP_Post || $post->post_type != self::$cpt ){
				return $transtext;
			}

			$replacements = $this->replacements();
			$replacement = isset( $replacements[ $domain ][ $normtext ] ) ? $replacements[ $domain ][ $normtext ] : $transtext;
		
			return $replacement;

		}

		public function replace_gettext_with_context( $translations, $text, $context, $domain ){			
			return $this->replace_gettext( $translations, $text, $domain );
		}
	}

	add_action( 'plugins_loaded', function(){
		$GLOBALS['WPMUDEV_Replace_Gettext_On_CPT'] = WPMUDEV_Replace_Gettext_On_CPT::get_instance();
	}, 9999 );
}

以上是关于php [WordPress] - 在CPT页面上替换gettext的主要内容,如果未能解决你的问题,请参考以下文章

php WordPress休息api定制;丢弃端点,添加新字段,在api中显示cpt

php WordPress自定义分类与CPT相同的Slug

从存档调用的 CPT 页面上的 Wordpress 上一个和下一个链接

php 重力形成多文件上传器字段到wordpress媒体附件并添加到CPT的元

通过Wordpress循环通过ID循环CPT

Wordpress - 自定义帖子类型存档页面