Conditional get template part

My index page look like this…

    <div id="ad-id">google ad code goes here </div><br />
    <div id="post-id">My posts are goes here</div>

I use

Read More
    get_template_part('index') 

in another page template. But I want index’s posts only NOT google ads.

Help me..

Thanks in advance..)

Related posts

Leave a Reply

1 comment

  1. In your index template, you’ll want to add a conditional around your Google ad. The subject of that conditional is up to you, and will depend on your exact needs. For instance, if you want it to show on the homepage but not other pages, you can check if is_home(). If your needs are more abstract, you can define a global variable and set it as appropriate before your get_template_part call, e.g. $GLOBALS['show_ad'] = true; and then only display the Google ad if that’s true.

    Edit: Examples

    Say you want to show this only on your homepage. Here’s what your index page should look like…

    <?php if ( is_home() ) : ?>
    <div id="ad-id">google ad code goes here </div><br />
    <?php endif ?>
    <div id="post-id">My posts are goes here</div>
    

    Or, say your needs are more abstract. Here are both pages with a global variable.

    index page:

    <?php if ( isset( $GLOBALS['show_ad'] ) && true == $GLOBALS['show_ad'] ) : ?>
    <div id="ad-id">google ad code goes here </div><br />
    <?php endif ?>
    <div id="post-id">My posts are goes here</div>
    

    Calling template:

    # Show the ad
    $GLOBALS['show_ad'] = true;
    get_template_part( 'index' );
    

    … or…

    # Don't show the ad
    $GLOBALS['show_ad'] = false;
    get_template_part( 'index' );