EDIT: I have managed to get this class working and I have updated the correct code. I would love not to use the [0] at the end of the value. Any way I can improve this code?
Bu sınıf, belirli bir yazı için tüm özel anahtarlarını alır. Şu anda görüntüleri ile için kullanmak ve benim sonrası tasarımda tuşları tanımlamış: 'related_image_wide' ve 'image_alt_text,' related_image '.
EDIT 2: I have finally managed to get the value the way I want it. I hope this can be usefull for anyone who finds this. The code has been updated.
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 . '" />';
}
}
Bu benim şablon kodunda ne olduğunu:
//This is inside The Loop
$article = new article($post);
echo $article->relatedImage("normal");