开发者

Using array with page slugs

开发者 https://www.devze.com 2023-04-08 20:47 出处:网络
开发者_JS百科I\'m using the following code to display images based on the category id: <?php if (is_category( \'web-design\' )){

开发者_JS百科I'm using the following code to display images based on the category id:

<?php 
if (is_category( 'web-design' )){
?>   
<img src="<?php bloginfo('template_directory'); ?>/images/slider_webdesign.png" title="خدمات تصميم وتطوير المواقع" height="200px" width="960px" />
<?php
}else if (is_category( 'printing' )){
?>
<img src="<?php bloginfo('template_directory'); ?>/images/slider_printing.png" title="تصميم مطبوعات" height="200px" width="960px" />
<?php
}else if (is_category( 'online-marketing' )){
?>

I would like to make an array of only one condition to display an image with the category slug, is that possible?


<?php

$categories = array(
  'web-design' => array(
    'image' => 'slider_webdesign.png',
    'title' => 'Title of image',
    'height' => '200',
    'width' => '960'
  ),
  'template_directory' => array(
    'image' => 'slider_printing.png',
    'title' => 'Title of image',
    'height' => '200',
    'width' => '960'
  )
);

$category = false;

/* following code might need rewriting since I'm not sure if this is the */
/* correct way of getting current category slug */

if (is_category()) {
  $slug = get_category(get_query_var('cat'))->slug;

  if (isset($categories[$slug])) {
    $category = $categories[$slug];
  }
}

?>

<?php if ($category): ?>
<img src="<?php bloginfo('template_directory'); ?>/images/<?=$category['image']?>" title="<?=$category['title']?>" height="<?=$category['height']?>" width="<?=$category['width']?>" />
<?php endif ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消