WordPress 개발 중 짧은 코드의 실현 및 관련 함수 사용 기교
짧은 코드의 실현 원리는 WP의 일부 동작에 갈고리와 필터 함수를 넣는 것과 같다. 짧은 코드는 단지 봉인된 문장의 출력 내용에 대한 필터를 거쳤을 뿐 일부 주제 기능이 말한 것처럼 그렇게 충격적이고 깊이가 없다.다음은 간단한 예입니다.
function myName() {//
return "My name's XiangZi !";
}
//
//xz
// [xz] myName
add_shortcode('xz', 'myName');
그러면 저희가 문장에 [xz]를 입력하면 얻을 수 있어요.
My name's XiangZi !
짧은 코드의 전삼을 좀 더 깊이 있게 이용하면 뒷글에서 말하겠습니다. 오늘은 짧은 코드의 전삼 메커니즘이 좀 고급스러운 예만 말씀드리겠습니다.
function myName($array,$content) {
var_dump($array);
var_dump($content);
}
add_shortcode('xz', 'myName');
기사를 편집할 때 다음을 입력합니다.
[xz a="1" b="2" c="3"] [/xz]
함수에서 우리는 다음과 같은 것을 얻을 수 있다.
//$array ,
//
$array = array('a'=>'1','b'=>'2','c'=>'3');
//$content
$content = ' ';
shortcode_atts는 코드 플러그인을 짧게 하기 때문이 아니라, 나도 이 함수를 사용하지 않을 거야.shortcodeatts 함수는 주로 짧은 코드에서 변수를 캡처하는 초기 값을 설정하는 데 사용됩니다.이것은 매우 실용적인 함수이다. 사실 이 함수의 진정한 역할은 수조에 있다. 왜냐하면 우리가 짧은 코드에서 캡처한 매개 변수는 모두 수조 형식이기 때문이다.
shortcode_tts 함수는 함수 이름에 의심받지 않도록 자세히 설명합니다.WordPress에서는 주로 짧은 코드 매개 변수의 기본값을 설정하는 데 사용됩니다. 만약 우리가 코드를 추출하여 다른 곳에 사용한다면, 이 함수는 기존의 그룹의 기본값을 설정하는 데 도움을 줄 수 있습니다.
shortcode_atts 함수는 이 함수를 사용하면 매우 간단하다.
shortcode_atts(array(
"url" => 'http://PangBu.Com'
), $url)
위 코드는 $url 그룹 키 값을 url의 구성원 기본값으로 설정하는 것을 의미합니다.http://PangBu.Com'다른 곳에서는 쓸모가 많지 않은 것 같지만, 일부 게으름뱅이들에게는 항상 잊어버리거나 그룹의 수치를 설정하는 것이 귀찮을 때 이 함수가 매우 좋다.
shortcode_atts 함수 설명
/**
* Combine user attributes with known attributes and fill in defaults when needed.
*
* The pairs should be considered to be all of the attributes which are
* supported by the caller and given as a list. The returned attributes will
* only contain the attributes in the $pairs list.
*
* If the $atts list has unsupported attributes, then they will be ignored and
* removed from the final returned list.
*
* @since 2.5
*
* @param array $pairs Entire list of supported attributes and their defaults.
* @param array $atts User defined attributes in shortcode tag.
* @return array Combined and filtered attribute list.
*/
function shortcode_atts($pairs, $atts) {
$atts = (array)$atts;
$out = array();
foreach($pairs as $name => $default) {
if ( array_key_exists($name, $atts) )
$out[$name] = $atts[$name];
else
$out[$name] = $default;
}
return $out;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.