WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Loading template twice - how to do it? (10 posts)

  1. benutzerfreund
    Member
    Posted 5 months ago #

    I have a template that I'd like to use several times on a page. I use
    include( get_stylesheet_directory() . '/ads.php');
    and this works for the first instance. But on the second it doesn't. Apparently it is only possible to include it once. So I tried:
    locate_template( array( 'ads.php' ), true, false );
    But to no avail. And get_template_part doesn't do the trick either.

    Which is the correct way to do it? (I want do use it in the Loop, to include a block of ads every 3rd post on my homepage.)

  2. bcworkz
    Member
    Posted 5 months ago #

    Put the include inside a function declaration, then call the function as many times as you like. Function declarations are only evaluated once, so the include is only evaluated once.

  3. benutzerfreund
    Member
    Posted 5 months ago #

    Thanks for the tip, that works.

    But I thought using templates is the more elegant way—do I understand it correctly that there is no way calling these more than once?

  4. bcworkz
    Member
    Posted 5 months ago #

    Using include directly, yes, not possible. Most themes use get_template_part() inside the loop to do what you are trying to do. It eventually uses locate_template() once it is confirmed the template actually exists. locate_template() uses require instead of include. AFAIK the only difference is the error thrown on failure.

    That's the WP way, though it's ultimately no different than what I told you to do. The include or require is wrapped in a function and the function is called repeatedly. One is more expected than the other, I wouldn't say it is more elegant.

    I'm not sure why get_template_part() didn't work for you. We can work through it if you think it's important. Honestly, it was just easier and faster to tell you to write a function than debug code.

  5. benutzerfreund
    Member
    Posted 5 months ago #

    Thanks for the clarification.

    I narrowed the problem with get_template_part() down to this: I have a function in the ads.php which I include. When I try it without and put the function in functions.php of the theme, everything works fine.

    Apparently functions in included templates can only be included once?

  6. bcworkz
    Member
    Posted 5 months ago #

    Functions included? If you mean function declarations, then yes, they can only be declared once anytime during a particular request, no matter where declared. If you mean function call, no, you can call functions as many times on any code page or pages as you want.

    Including a code file is exactly equivalent to copying the text on the included page and pasting it where the include statement is. If you can or cannot do something on a single page, you can or cannot do the same in an included page.

  7. benutzerfreund
    Member
    Posted 5 months ago #

    Sorry, I used the wrong words. Of course I declare the function only once. This is my ads.php:

    <?php
    /**
     * The default template for ads
     * eingebunden von index.php und single.php
     * @package WordPress
     * @subpackage Twenty_Twelve
     * @since Twenty Twelve 1.0
     */
    function showAds() {
    	return ('Ads!');
    }
    echo showAds();
    ?>

    And this is the loop:

    while ( have_posts() ) : the_post(); ?>
    		<?php get_template_part( 'content', get_post_format() );
    		get_template_part(  'ads', '' ); ?>
    <?php endwhile; ?>

    This results in a page where I see "Ads!" after the first post, then the next post and then nothing. The HTML just stops after </article> without including more posts or the footer.

  8. bcworkz
    Member
    Posted 5 months ago #

    OK, I see what you're getting at now :)

    You actually are redeclaring the function declaration every time you load the template!

    Think about it.

    1. getting a template part is basically including or requiring the file each time the function is called.

    2. including or requiring is like pasting the file's content where the statement occurs.

    Thus your page sort of looks like this:

    content stuff
    function showAds() {
    return ('Ads!');
    }
    echo showAds();
    content stuff
    function showAds() {
    return ('Ads!');
    }
    echo showAds();
    content stuff
    function showAds() {
    return ('Ads!');
    }
    echo showAds();

    See the problem now? Move the function declaration to functions.php, that's what it's there for, it only gets loaded once :)

  9. benutzerfreund
    Member
    Posted 5 months ago #

    Oh my, this is embarrassing, of course it is like this. Now I got it.

    Thanks a ton for your patience with me!

  10. bcworkz
    Member
    Posted 5 months ago #

    You're most welcome!

    Don't be embarrassed, you actually had me perplexed for a bit, it's not that obvious until it's illustrated like I had done :)

Reply

You must log in to post.

About this Topic