[resolved] Simple PHP Question (9 posts)

  1. misterpatrick67
    Posted 6 years ago #

    Hello all,

    I am setting up a simple loop but a unsure how to do this in PHP. Any help appreciated. The gist of it is:

       $directory = "<?php the_permalink(); ?> /project/large/";

    This doesn't work. How do I use template tags within PHP? Thanks!

  2. goto10
    Posted 6 years ago #

    Template tags

    It's not clear from your post where your loop is, but it looks like maybe you want something like this?

       $directory = get_bloginfo( 'url' ) . '/project/large/';
  3. misterpatrick67
    Posted 6 years ago #

    The problem is, that echos the directory but for some odd reason, doesn't seem to work with glob. This returns the permalink, but it echos it, it doesn't actually scan the directory.

    $directory = the_permalink() . "/large/";
    $images = glob("" . $directory . "*.jpg");

    I just don't quite get how glob works. What the script is doing is scanning the directory in the project folder for image files, these are then output via a foreach statement.

  4. Michael
    Forum Moderator
    Posted 6 years ago #

    if you want to use the permalink in a variable, you better use get-permalink()


  5. misterpatrick67
    Posted 6 years ago #

    Urgh, this thing hates me. I can return the permalink on it's own just fine, but as soon as I put it in a variable it stops working. For example, this

    $directory = get_permalink() . "/large/";
    echo $directory;

    Return /large/

    What am I doing wrong with defining $directory?


  6. misterpatrick67
    Posted 6 years ago #

    I really can't figure out why this won't work. The permalink that it is passing is fine, it just doesn't work. For example, this works:

    $directory = "." . "images/";
    $images = glob($directory . "*.jpg");

    This does not:

    $directory = get_permalink($post->ID);
    $images = glob($directory . "images/*.jpg");

    This is driving me batty. Any ideas? The problem is that using '.' to get the root URL is fine, except I need each directory scan to be in the directory of that project. So for one page the permlink will be http://myurl.com/project_1/images/ and for the next page it will be http://myurl.com/project_2/images/ etc.

  7. Michael
    Forum Moderator
    Posted 6 years ago #

    imho, @goto10 is right to use 'get_bloginfo()' - i missed that with my suggestion :-(


    can you clarify with an example of absolute file paths what you tried to get as 'permalink' in your first post?

    if it is:
    (which is basically get_bloginfo('template_url') )
    then the ideas in this snippet might get you further:

    echo get_bloginfo('template_url'); echo '<br/>';
    echo get_bloginfo('url'); echo '<br/>';
    $directory = str_replace(get_bloginfo('url'),'',get_bloginfo('template_url')); // remove the root url
    $directory = substr($directory,1,strlen($directory)-1); //remove the leading slash /
    echo 'directory: '.$directory; echo '<br/>';
    $files = glob($directory.'/images/*.jpg');
    foreach($files as $file) {
    echo $file;
    echo '<br/>';
    // make an array of the images
    $img_lib[] = str_replace($directory.'/images/','',$file); //remove the path from file

    (the echos are only to show at any point what is found)
    hope this helps a bit, good luck ;-)

  8. misterpatrick67
    Posted 6 years ago #

    Closer! I am getting things returned now. I am a little confused on getting the results of the array into the images. Here's the foreach that I have been using that works fine as long as everything is always in the same directory. Sorry the echos are a little convoluted, whenever I start messing with the quotes things start breaking so replacing things is usually one of my last steps.

    foreach($files as $file)
        echo "<div class='video'>";
        echo '<a href="';
        echo ''?><?php echo substr($file,1) ?><?php echo'" />';
        echo '<img src="http://myurl.com/'?><?php echo substr($file,1) ?><?php echo'" />';
        echo "</a></div>";
  9. misterpatrick67
    Posted 6 years ago #

    I've figured it out and am using scandir instead of glob. Here's what's working for those who are trying to do this in the future. You have to make sure that the directory names match your slug names.

    	$post_slug = $post_obj->post_name;
    	$dir = $post_slug . '/large/'; //my jpegs are all in this directory
    	$scan = scandir($dir);
    	for ($i = 0; $i<count($scan); $i++) {
    	if ($scan[$i] != '.' && $scan[$i] != '..') {
    		if (strpos($scan[$i], '.jpg') !== false) {
    			echo '<div class="lightbox">
    		          <a href="http://www.myurl.com/'  . $dir . $scan[$i] . '">
    				<img src="http://www.myurl.com/' . $dir . $scan[$i] . '" alt="' . $scan[$i] . '" />

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.