WordPress.org

Ready to get started?Download WordPress

Forums

Display IF Homepage Only (4 posts)

  1. sarahjanes88
    Member
    Posted 1 year ago #

    Hi guys,

    There was a section on my homepage in the header.php where I wanted to add content from a page ID, it worked for me and looks like this.

    <?php
    $page_id = 1493;

    $page_data = get_page( $page_id );

    echo apply_filters('the_content', $page_data->post_content);
    ?>

    Because this is in the header.php file it is displaying on every page, blog, posts etc when I only want it on the homepage.

    What code do I add to say IF Homepage to display Else don't display the content????

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Conditional_Tags

    depending if you have a static front page or if the posts page is on te front, use either is_front_page() or is_home();

    example:

    <?php if( is_home() ) :
    $page_id = 1493;
    $page_data = get_page( $page_id );
    echo apply_filters('the_content', $page_data->post_content);
    endif; ?>
  3. sarahjanes88
    Member
    Posted 1 year ago #

    Hi alchymyth, thank you very much it is working now using the is_front_page() , I appreaciate the help!

    One more problem is the next line, where I want to display the DIV only if is_front_page() , It's not working because I have a plugin to display also within the DIV. What is wrong with this code?

    <?php if(is_front_page()) :
    
    	<div class="featuredimg" style="bottom: 0px;">
    
    <?php if (function_exists('vslider')) { vslider('homeslide'); }?>
    
    	</div>							
    
    	endif; ?>
  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    you need to close the php tags when switching to html code, and open them again when going back to php code;

    <?php if(is_front_page()) : ?>
    
    	<div class="featuredimg" style="bottom: 0px;">
    
    <?php if (function_exists('vslider')) { vslider('homeslide'); }?>
    
    	</div>							
    
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic