WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to write PHP if(is_front_page()) statment (20 posts)

  1. addesigns
    Member
    Posted 1 year ago #

    Hi,

    I'm trying to add a different stylesheet to the front page of my website which uses a mystile theme. My website responds to my edits to my css file so I know it's reading it, but it's applying it to the entire site and not just the front page:

    <?php
    // File Security Check
    if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
        die ( 'You do not have sufficient permissions to access this page!' );
    }
    ?>
    <?php
    /**
     * Header Template
     *
     * Here we setup all logic and XHTML that is required for the header section of all screens.
     *
     * @package WooFramework
     * @subpackage Template
     */
    
     global $woo_options, $woocommerce;
    
    ?><!DOCTYPE html>
    <html <?php language_attributes(); ?> class="<?php if ( $woo_options['woo_boxed_layout'] == 'true' ) echo 'boxed'; ?> <?php if (!class_exists('woocommerce')) echo 'woocommerce-deactivated'; ?>">
    <head>
    
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    
    <title><?php woo_title(''); ?></title>
    <?php woo_meta(); ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo( 'stylesheet_url' ); ?>" media="screen" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    
    <?php
    	wp_head();
    	woo_head();
    ?>
    
    </head>
    
    <body <?php body_class(); ?>>
    <?php woo_top(); ?>
    
    <div id="wrapper">
    
    	<div id="top">
    		<nav class="col-full" role="navigation">
    			<?php if ( function_exists( 'has_nav_menu' ) && has_nav_menu( 'top-menu' ) ) { ?>
    			<?php wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'top-nav', 'menu_class' => 'nav fl', 'theme_location' => 'top-menu' ) ); ?>
    			<?php } ?>
    			<?php
    				if ( class_exists( 'woocommerce' ) ) {
    					echo '<ul class="nav wc-nav">';
    					echo '<li class="cart">'.current(woocommerce_cart_link()).'</li>';
    					echo '<li class="checkout"><a href="'.esc_url($woocommerce->cart->get_checkout_url()).'">'.__('Checkout','woothemes').'</a></li>';
    					echo get_search_form();
    					echo '</ul>';
    				}
    			?>
    		</nav>
    	</div><!-- /#top -->
    
        <?php woo_header_before(); ?>
    
    	<header id="header" class="col-full">
    
    	    <hgroup>
    
    	    	 <?php
    			    $logo = esc_url( get_template_directory_uri() . '/images/logo.png' );
    				if ( isset( $woo_options['woo_logo'] ) && $woo_options['woo_logo'] != '' ) { $logo = $woo_options['woo_logo']; }
    				if ( isset( $woo_options['woo_logo'] ) && $woo_options['woo_logo'] != '' && is_ssl() ) { $logo = preg_replace("/^http:/", "https:", $woo_options['woo_logo']); }
    			?>
    			<?php if ( ! isset( $woo_options['woo_texttitle'] ) || $woo_options['woo_texttitle'] != 'true' ) { ?>
    			    <a id="logo" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php esc_attr( get_bloginfo( 'description' ) ); ?>">
    			    	<img src="<?php echo $logo; ?>" alt="<?php esc_attr( get_bloginfo( 'name' ) ); ?>" />
    			    </a>
    		    <?php } ?>
    
    			<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
    			<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
    			<h3 class="nav-toggle"><a href="#navigation">☰ <span><?php _e('Navigation', 'woothemes'); ?></span></a></h3>
    
    		</hgroup>
    
            <?php woo_nav_before(); ?>
    
    		<nav id="navigation" class="col-full" role="navigation">
    
    			<?php
    			if ( function_exists( 'has_nav_menu' ) && has_nav_menu( 'primary-menu' ) ) {
    				wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'main-nav', 'menu_class' => 'nav fr', 'theme_location' => 'primary-menu' ) );
    			} else {
    			?>
    	        <ul id="main-nav" class="nav fl">
    				<?php if ( is_page() ) $highlight = 'page_item'; else $highlight = 'page_item current_page_item'; ?>
    				<li class="<?php echo $highlight; ?>"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php _e( 'Home', 'woothemes' ); ?></a></li>
    				<?php wp_list_pages( 'sort_column=menu_order&depth=6&title_li=&exclude=' ); ?>
    			</ul><!-- /#nav -->
    	        <?php } ?>
    
    <?php if( is_front_page() ) {
    <link rel="stylesheet" type="text/css" href="http://ashleydavisdesigns.com/wp-content/themes/mystile/css/frontpage.css" /> ?>
    
    		</nav><!-- /#navigation -->
    
    		<?php woo_nav_after(); ?>
    
    	</header><!-- /#header -->
    
    <?php if( is_front_page() ) echo do_shortcode('[promoslider category="glamolousintro" width="930px" height="600px"]'); ?>
    
    <?php } ?>
    
    	<?php woo_content_before(); ?>
  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    My website responds to my edits to my css file so I know it's reading it

    Have you prepended your CSS selectors with .home ?

  3. addesigns
    Member
    Posted 1 year ago #

    The code I originally entered to get the stylesheet applied is:

    <?php if( is_front_page() ) { ?>
    <link rel="stylesheet" type="text/css" href="http://ashleydavisdesigns.com/wp-content/themes/mystile/css/frontpage.css" />

    but it applied it to the entire website instead of just the front page so that's why I tried to add a ?> to the end of the statement, but then I got an error that there was a > where one shouldn't be located.

    Any help would be greatly appreciated!

  4. addesigns
    Member
    Posted 1 year ago #

    No, I haven't...

    If I'm making a separate stylesheet and only calling it if it's the front page, I shouldn't have to, should I?

  5. Andrew
    Forum Moderator
    Posted 1 year ago #

    If I'm making a separate stylesheet and only calling it if it's the front page, I shouldn't have to, should I?

    No, you shouldn't have to.

    Try this;

    <?php if( is_front_page() ) { ?>
     <link rel="stylesheet" type="text/css" href="http://ashleydavisdesigns.com/wp-content/themes/mystile/css/frontpage.css" />
    <?php } ?>
  6. addesigns
    Member
    Posted 1 year ago #

    I tried that before too and this is the error I get:

    Parse error: syntax error, unexpected '}' in /home/content/d/a/v/davisad0/html/wp-content/themes/mystile/header.php on line 117

    Could it be where I'm placing it within my header.php file?

  7. Andrew
    Forum Moderator
    Posted 1 year ago #

    Whereabouts are you placing it in your header.php file?

  8. addesigns
    Member
    Posted 1 year ago #

    The first post is of my header.php file.

    I put it before: </nav><!-- /#navigation -->
    and after: <?php woo_nav_before(); ?>

  9. addesigns
    Member
    Posted 1 year ago #

    <?php
    // File Security Check
    if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
        die ( 'You do not have sufficient permissions to access this page!' );
    }
    ?>
    <?php
    /**
     * Header Template
     *
     * Here we setup all logic and XHTML that is required for the header section of all screens.
     *
     * @package WooFramework
     * @subpackage Template
     */
    
     global $woo_options, $woocommerce;
    
    ?><!DOCTYPE html>
    <html <?php language_attributes(); ?> class="<?php if ( $woo_options['woo_boxed_layout'] == 'true' ) echo 'boxed'; ?> <?php if (!class_exists('woocommerce')) echo 'woocommerce-deactivated'; ?>">
    <head>
    
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    
    <title><?php woo_title(''); ?></title>
    <?php woo_meta(); ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo( 'stylesheet_url' ); ?>" media="screen" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    
    <?php
    	wp_head();
    	woo_head();
    ?>
    
    </head>
    
    <body <?php body_class(); ?>>
    <?php woo_top(); ?>
    
    <div id="wrapper">
    
    	<div id="top">
    		<nav class="col-full" role="navigation">
    			<?php if ( function_exists( 'has_nav_menu' ) && has_nav_menu( 'top-menu' ) ) { ?>
    			<?php wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'top-nav', 'menu_class' => 'nav fl', 'theme_location' => 'top-menu' ) ); ?>
    			<?php } ?>
    			<?php
    				if ( class_exists( 'woocommerce' ) ) {
    					echo '<ul class="nav wc-nav">';
    					echo '<li class="cart">'.current(woocommerce_cart_link()).'</li>';
    					echo '<li class="checkout"><a href="'.esc_url($woocommerce->cart->get_checkout_url()).'">'.__('Checkout','woothemes').'</a></li>';
    					echo get_search_form();
    					echo '</ul>';
    				}
    			?>
    		</nav>
    	</div><!-- /#top -->
    
        <?php woo_header_before(); ?>
    
    	<header id="header" class="col-full">
    
    	    <hgroup>
    
    	    	 <?php
    			    $logo = esc_url( get_template_directory_uri() . '/images/logo.png' );
    				if ( isset( $woo_options['woo_logo'] ) && $woo_options['woo_logo'] != '' ) { $logo = $woo_options['woo_logo']; }
    				if ( isset( $woo_options['woo_logo'] ) && $woo_options['woo_logo'] != '' && is_ssl() ) { $logo = preg_replace("/^http:/", "https:", $woo_options['woo_logo']); }
    			?>
    			<?php if ( ! isset( $woo_options['woo_texttitle'] ) || $woo_options['woo_texttitle'] != 'true' ) { ?>
    			    <a id="logo" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php esc_attr( get_bloginfo( 'description' ) ); ?>">
    			    	<img src="<?php echo $logo; ?>" alt="<?php esc_attr( get_bloginfo( 'name' ) ); ?>" />
    			    </a>
    		    <?php } ?>
    
    			<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
    			<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
    			<h3 class="nav-toggle"><a href="#navigation">☰ <span><?php _e('Navigation', 'woothemes'); ?></span></a></h3>
    
    		</hgroup>
    
            <?php woo_nav_before(); ?>
    
    		<nav id="navigation" class="col-full" role="navigation">
    
    			<?php
    			if ( function_exists( 'has_nav_menu' ) && has_nav_menu( 'primary-menu' ) ) {
    				wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'main-nav', 'menu_class' => 'nav fr', 'theme_location' => 'primary-menu' ) );
    			} else {
    			?>
    	        <ul id="main-nav" class="nav fl">
    				<?php if ( is_page() ) $highlight = 'page_item'; else $highlight = 'page_item current_page_item'; ?>
    				<li class="<?php echo $highlight; ?>"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php _e( 'Home', 'woothemes' ); ?></a></li>
    				<?php wp_list_pages( 'sort_column=menu_order&depth=6&title_li=&exclude=' ); ?>
    			</ul><!-- /#nav -->
    	        <?php } ?>
    
    <?php if( is_front_page() ) { ?>
    <link rel="stylesheet" type="text/css" href="http://ashleydavisdesigns.com/wp-content/themes/mystile/css/frontpage.css" />
    
    		</nav><!-- /#navigation -->
    
    		<?php woo_nav_after(); ?>
    
    	</header><!-- /#header -->
    
    <?php if( is_front_page() ) echo do_shortcode('[promoslider category="glamolousintro" width="930px" height="600px"]'); ?>
    
    <?php } ?>
    
    	<?php woo_content_before(); ?>
  10. addesigns
    Member
    Posted 1 year ago #

    No clue how to fix it Andrew?

  11. Andrew
    Forum Moderator
    Posted 1 year ago #

    I have, but I'm weary to advise as I hadn't realised you're using a Woocommerce template.

    What is your theme's name?

  12. addesigns
    Member
    Posted 1 year ago #

    Mystile, a woocommerce theme

  13. esmi
    Forum Moderator
    Posted 1 year ago #

    I'm sorry but as you are using a commercially supported theme, you need to seek support from the theme's developers.
    http://support.woothemes.com/

  14. addesigns
    Member
    Posted 1 year ago #

    That link just sent me to a login page...no clue what to put into it. So I can't get any suggestions on here about howo to write a php if(is_front_page()) statement??? That's wordpress related.

  15. esmi
    Forum Moderator
    Posted 1 year ago #

    Try http://www.woothemes.com/support/

    We do not support commercial themes as it is not our place to deprive theme developers of any income that they may derive from support services.

  16. addesigns
    Member
    Posted 1 year ago #

    This is a free theme...
    So if anyone gets a free theme from anywhere other than wordpress.org themes, they can't ask questions on the forum regarding basic wordpress functions and statements? It's not like it's theme specific.

  17. esmi
    Forum Moderator
    Posted 1 year ago #

    This is very theme-specific. And the wordpress.org support forums are meant for the core application, themes and plugins that are distributed on wordpress.org - not elsewhere.

  18. addesigns
    Member
    Posted 1 year ago #

    if(is_front_page()) was not made up by woocommerce because I've had wordpress.org themes that included this statement. So, switch over to a wordpress.org theme and ask this same question on how to write the statement and I can get results?

  19. esmi
    Forum Moderator
    Posted 1 year ago #

    I'd recommend that you didn't load different stylesheets based on specific pages but used the output generated by the body_class() function to target your CSS at specific pages within the main stylesheet.

  20. addesigns
    Member
    Posted 1 year ago #

    Turns out, it was working correctly all along. It was something else of theirs given me the impression that it wasn't behaving correctly.

Topic Closed

This topic has been closed to new replies.

About this Topic