根据页面/帖子Wordpress更改div类

时间:2014-03-10 11:06:27

标签: php css wordpress

在Wordpress中有更简单的方法吗?

我要做的是根据用户所在的页面/帖子更改标题类以显示不同的图像..

我的代码 - PHP     

    <?php

    //Services page

    if ( is_page( 'services' ) ) {

    echo'<header class="main_top services_page">';

     }

    //Portfolio page

    else if ( is_page( 'portfolio' ) ) {

    echo'<header class="main_top portfolio_page">';

    }

    //Digital page

    else if ( is_single( 'digital' ) ) {

    echo'<header class="main_top digital_page">';

    }

    //Social Media page

    else if ( is_single( 'social-media-2' ) ) {

    echo'<header class="main_top media_page">';

    }

    //Print page

    else if ( is_single( 'print' ) ) {

    echo'<header class="main_top print_page">';

    }


    //Home page

     else echo'<header class="main_top">';

    ?>

我为每个标题类添加了一个不同的CSS类,如下所示

  header.services_page{
    background-image: url(images/contentmarketing1-2000x450.jpg);
  }
  header.portfolio_page{
    background-image: url(images/portfolio-2000x450.jpg);
  }
  header.contact_page{
    background-image: url(images/contact-2000x450.jpg);
  }
  header.digital_page{
    background-image: url(images/digital2-2000x450.jpg);
  }
  header.media_page{
    background-image: url(images/socialmedia-2000x450.jpg);
  }
  header.print_page{
    background-image: url(images/print-2000x450.jpg);
  }
  header.audio_page{
    background-image: url(images/audio-2000x450.jpg);
  }
  header.pr_page{
    background-image: url(images/pr-2000x450.jpg);
  }
  header.cm_page{
    background-image: url(images/contentmarketing2-2000x450.jpg);
  }

谢谢! : - )

1 个答案:

答案 0 :(得分:0)

$page_post_title=get_the_title($page_post_ID);

echo'<header class="main_top $page_post_title">';

这将只用一行完成。

尝试或者你也可以修改它。