0
편집 : 나는이 클래스를 작동시킬 수 있었고 올바른 코드를 업데이트했습니다. 나는 값의 끝에 [0]을 사용하지 않기를 바랄 것이다. 어떤 식 으로든이 코드를 개선 할 수 있습니까?Wordpress에서 이미지를 관련시키는 클래스를 만들려고합니다.
이 클래스는 특정 게시물에 대한 모든 사용자 지정 키를 검색합니다. 현재 관련 이미지에 사용하고 있으며 내 게시물에 'related_image', 'related_image_wide'및 'image_alt_text'키를 정의했습니다.
EDIT 2 : 나는 마침내 내가 원하는 방식으로 가치를 얻을 수 있었다. 나는 이것이 이것을 발견 한 누구에게나 유용 할 수 있기를 바란다. 코드가 업데이트되었습니다.
class article {
private $alternative_text;
private $custom_fields = array();
public function __construct($post)
{
$val = array();
$custom_field_keys = get_post_custom_keys();
foreach ($custom_field_keys as $key => $value)
{
// Custom_fields["a"] gets the value of the custom field "a"
$val = get_post_custom_values($value);
$this->custom_fields[$value] = $val[0];
}
$this->alternative_text = $this->custom_fields["image_alt_text"];
}
public function relatedImage($type)
{
// Standard image to be shown with article
if($type == 'normal')
return $this->imageURL($this->custom_fields["related_image"]);
// Wide image to be shown with article.
if($type == 'wide')
return $this->imageURL($this->custom_fields["related_image_wide"]);
// Alternative image. Often used in article listing and not in main article
if($type == 'alt')
return $this->imageURL($this->custom_fields["related_image_alternative"]);
}
private function imageURL($imgPath)
{
return '<img src="' . get_option('home') . '/wp-content/uploads' . $imgPath .'" alt="' . $this->alternative_text . '" title="' . $this->alternative_text . '" />';
}
}
이 내 템플릿 코드에서 할 것입니다 :
//This is inside The Loop
$article = new article($post);
echo $article->relatedImage("normal");
신뢰 내가 필요하면 여기
가 Wordpress codex의 링크입니다 (루프에서) 당신이가는 것 '정상'이미지를 얻을 싶어 그래서 경우, 그것은 내가 시도한 첫 번째 것들 중 하나이다 :) 그러나 그것은 작동하지 않았다. 하지만 지금은 제대로 작동합니다. – Steven