0
编辑: 我设法让这个类工作,并且我更新了正确的代码。 我不想在值的末尾使用[0]。任何方式,我可以改善这个代码?试图在Wordpress中创建相关图像的类
此类将检索特定帖子的所有自定义键。目前我用它来关联图像,并且在我的文章中定义了三个关键字:'related_image','related_image_wide'和'image_alt_text。
编辑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");
信任我,这是我尝试的第一件事情之一:)但它没有奏效。但我现在正在工作。 – Steven 2009-06-13 10:01:32