WordPress主题 - 如果改变元素太多了

时间:2013-02-22 04:02:38

标签: php wordpress

我正在研究Wordpress中一个非常复杂的主题。这是我第一个没有修改主题的主要wp项目。该网站的一部分在标题中的视觉元素略有不同,所以我这样做

<?php if(is_page(537)) { ?><div id="header-right"><?php } ?>
<?php if(!is_page(537)) { ?><div id="media-header-right"><?php } ?>

问题是我到处都有10个这样的陈述。它有效,但我不禁认为有一个更优雅的解决方案。

的header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="width=device-width" />
<title>
<?php bloginfo('name'); ?>
<?php if ( is_single() ) { ?>
&raquo; Blog Archive
<?php } ?>
<?php wp_title(); ?>
-
<?php bloginfo('description'); ?>
</title>
<link rel="shortcut icon" href="<?php bloginfo('template_url');?>/images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('template_url'); ?>/css/style.css" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/fonts/stylesheet.css" media="all">
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/slider.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/slider-theme/default.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/jquery.tweet.css" type="text/css" media="screen"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/roll-over.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.tweet.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/gmap-somalia.js"></script>
<?php wp_head(); ?>
</head>

<?php if(!is_page(537)) { ?> <body <?php body_class(); ?>> <?php } ?>
<?php if(is_page(537)) { ?><body class="media-showcase"><?php } ?>

<div class="container">
<!--Start Container -->
<div id="header">
<!--Start Header -->
<div class="logo"> <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name')?>"  width="269" height="89" rel="home"><img src="<?php bloginfo('template_url');?>/images/logo.png" width="269" height="89" alt="Global Enrichment Foundation"></a> </div>

<?php if(is_page(537)) { ?><div id="media-header-right"><?php } ?>
<?php if(!is_page(537)) { ?><div id="header-right"><?php } ?>

<!--Start Header Right -->
<?php if(is_page(537)) { ?> <div class="media-donate-buton">
<a href="<?php bloginfo('url');?>/donate" title="Donate">Donate</a> </div> <?php } ?>
<?php if(!is_page(537)) { ?>
<div class="donate-buton"> <a href="<?php bloginfo('url');?>/donate" title="Donate">Donate</a> </div>
<?php } ?>

<?php if(is_page(537)) { ?>
<div class="search">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<p>
<input type="text" class="field" name="m" id="m" placeholder="Search..." />
<input type="submit" class="submit search" name="submit" id="searchsubmit" value="ok" />
</p>
</form>
</div>
<?php } ?>
<?php if(!is_page(537)) { ?>
<div class="search">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<p>
<input type="text" class="field" name="s" id="s" placeholder="Search..." />
<input type="submit" class="submit m-search" name="submit" id="searchsubmit" value="ok" />
</p>
</form>
</div>
<?php } ?>

<?php if(is_page(537)) { ?>

<div id="media">
<h3>Archive</h3>
<select id="standard-dropdown" name="standard-dropdown" class="custom-class1 custom-class2" style="width: 148px;">
<option value="0" class="test-class-0" selected="selected">Select a Month</option>
<option value="1" class="test-class-1">January</option>
<option value="2" class="test-class-2">February</option>
<option value="3" class="test-class-3">March</option>
<option value="4" class="test-class-4">April</option>
<option value="5" class="test-class-5">May</option>
<option value="6" class="test-class-6">June</option>
<option value="7" class="test-class-7">July</option>
<option value="8" class="test-class-8">August</option>
<option value="9" class="test-class-9">September</option>
<option value="10" class="test-class-10">October</option>
<option value="11" class="test-class-11">November</option>
<option value="12" class="test-class-12">December</option>
</select>
<select name="empty-dropdown" style="width: 148px;">
<option value="0" class="test-class-0" selected="selected">Select a Year</option>
<option value="1" class="test-class-1">2009</option>
<option value="2" class="test-class-2">2010</option>
<option value="3" class="test-class-3">2012 </option>
<option value="4" disabled="disabled">2013</option>
<option value="5" disabled="disabled">2014</option>
</select>
<ul class="socialicons">
<li class="twitter"><a href="http://twitter.com/AmandaLindhout" title="twitter" target='_blank'>Twitter</a></li>
<li class="facebook"><a href="http://www.facebook.com/AmandaLindhoutPage" title="facebook" target='_blank'>Facebook</a></li>
<li class="vimeo"><a href="http://www.youtube.com/user/globalenrichment" title="you tube" target='_blank'>You Tube</a></li>
</ul>
</div>
</div>
<?php } ?>

<?php if(!is_page(537)) { ?>
<div id="latest-twitts">
<h3>The Latest</h3>
<div id='custom' class="query">&nbsp;</div><!-- twitter via jquery -->
<h3>Stay Updated</h3>
<ul class="socialicons">
<li class="twitter"><a href="http://twitter.com/AmandaLindhout" title="twitter" target='_blank'>Twitter</a></li>
<li class="facebook"><a href="http://www.facebook.com/GlobalEnrichmentFoundation" title="facebook" target='_blank'>Facebook</a></li>
<li class="vimeo"><a href="http://www.youtube.com/user/globalenrichment" title="you tube" target='_blank'>You Tube</a></li>
</ul>
</div>
<?php } ?>


<?php if(!is_page(537)) { ?>
<div id="side_bar"></div>
<?php dynamic_sidebar('Sidebar'); ?>
</div> <?php } ?>
<!--End Header-Right -->
<div class='cssmenu'>
<!--Start Menu -->
<?php wp_nav_menu( array( 'container'=>'', 'menu_id'=>'nav' ,'menu_class' => 'main-menu','theme_location' =>        'main-menu','depth'=> 2 ) ); ?>
</div>
</div>
<!--End Menu -->
</div>
<!--End Header -->
<div class="clear"></div>

1 个答案:

答案 0 :(得分:0)

添加其他页面模板或新的头文件。它更清洁。

这是一个教程,它显示了添加页面模板的最简单方法

http://wpmu.org/wordpress-custom-page-template/

以及更多信息

http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

而不是

<?php get_header(); ?> 

在您的新模板中(可能不是正确的代码,但您明白了)自定义将新标题编码到模板中。

或者您可以创建另一个头文件,并使用您当前在页面模板中只使用一次的if语句。

这个例子

if(is_page(537)) {
     get new header
 } else {
      get original header
 }