• I’m trying to include a file in a child theme template, and having a bit of trouble – due to my amateur php skills, I’m sure.

    Here’s what I have so far:

    <img src="<?php echo get_stylesheet_directory_uri() ?>/images/fp-<?php the_ID(); ?>.jpg" alt="<?php bloginfo('name'); ?>" />
    
    <?php echo get_stylesheet_directory_uri() ?>/featured-content/fp-<?php the_ID(); ?>.htm
    
    <?php include("http://mydomain.com/wp-content/themes/Essence-RFF/featured-content/fp-56.htm ");?>
    
    <?php include("<?php echo get_stylesheet_directory_uri() ?>/featured-content/fp-<?php the_ID(); ?>.htm ");?>
    1. Line 1 works correctly to bring in an image with the current page name.
    2. Line 2 correctly echos the full path to the snippet
    3. In Line 3 I test the php include with the hard coded path from Line 2, but it fails
    4. Line 4 is the code I thought I should use to dynamically call a unique snippet file named with the page id

    Can anyone help me see what needs to be done?

    Thanks, so much!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Syntax error.

    This:

    <?php include("<?php echo get_stylesheet_directory_uri() ?>/featured-content/fp-<?php the_ID(); ?>.htm ");?>

    …should be this:

    <?php include( get_stylesheet_directory_uri() . '/featured-content/fp-' . get_the_ID() . '.htm' ); ?>

    Thread Starter cued100prof

    (@cued100prof)

    Thanks, Chip. I knew I should figure out how to get rid of that nested php. However, the include still does not work. Shouldn’t it at least work when I hard code the full URL to a specific file, as in Line 3?

    There are no error messages, or any trace of the request in the output. It’s like the line just gets skipped in processing.

    BTW, there is a php include line that’s built into the theme at the bottom of this same page that runs fine. So it seems like there is something within this div that is being handled differently, somehow.

    I’m mystified.

    1) Does “http://mydomain.com/wp-content/themes/Essence-RFF/featured-content/fp-56.htm&#8221; exist?

    2) Are you calling the include() inside the Loop?

    3) Why are you including a .htm file, instead of a .php file?

    Thread Starter cued100prof

    (@cued100prof)

    1) Yes, the file exists, and the URL works. Sorry, it’s in a protected directory so I can’t share it at the moment.

    2) Yes; this is a Page Template in the Child Theme folder. As I mentioned <?php include(TEMPLATEPATH."/sidebar_right_page.php");?> is on the same page, and works correctly.

    3) The file only contains a single paragraph of text, so I didn’t think it needed to be .php. (I have actually tried renaming it, though, to see if that would make a difference, but no cigar.)

    Thread Starter cued100prof

    (@cued100prof)

    Woah! That’s it! The security on the directory is preventing access by the system! I’ll see if I can figure out how to give the server access.

    Thanks for all your help. There’s one more lesson learned!

    2) Yes; this is a Page Template in the Child Theme folder. As I mentioned <?php include(TEMPLATEPATH.”/sidebar_right_page.php”);?> is on the same page, and works correctly.

    That’s not actually what I was asking. πŸ™‚ Just because the code is called from inside a custom page template (or any other template file or template-part file) doesn’t mean that it is executed inside the Loop. The Loop is the part that outputs the post data, e.g.

    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    // IS THE CODE CALLED HERE?
    
    endwhile; endif;

    But, to cut to the chase, try this, and tell me what gets output:

    $featured_content_filepath = get_stylesheet_directory_uri() . '/featured-content/fp-' . get_the_ID() . '.htm';
    var_dump( $featured_content_filepath  );

    Oh, and one other thing:, you need to use get_stylesheet_directory(), rather than get_stylesheet_directory_uri(). So, try this:

    <?php include( get_stylesheet_directory() . '/featured-content/fp-' . get_the_ID() . '.htm' ); ?>

    And if it still doesn’t work, tell me what is output from this:

    $featured_content_filepath = get_stylesheet_directory() . '/featured-content/fp-' . get_the_ID() . '.htm';
    var_dump( $featured_content_filepath  );

    Woah! That’s it! The security on the directory is preventing access by the system! I’ll see if I can figure out how to give the server access.

    Thanks for all your help. There’s one more lesson learned!

    This is probably because you’re using get_stylesheet_directory_uri() (which returns a URL), rather than get_stylesheet_directory() (which returns a filepath).

    The PHP include() function expects a filepath, and most servers have PHP configured NOT to allow use of URLs in include(), for security reasons.

    Thread Starter cued100prof

    (@cued100prof)

    Ok, that makes loads of sense all the way around.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘PHP Include’ is closed to new replies.