• Resolved misterpatrick67

    (@misterpatrick67)


    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:

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

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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Template tags

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

    <?php
       $directory = get_bloginfo( 'url' ) . '/project/large/';
    ?>
    Thread Starter misterpatrick67

    (@misterpatrick67)

    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.

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

    https://codex.www.ads-software.com/Function_Reference/get_permalink

    Thread Starter misterpatrick67

    (@misterpatrick67)

    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?

    Thanks!

    Thread Starter misterpatrick67

    (@misterpatrick67)

    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 https://myurl.com/project_1/images/ and for the next page it will be https://myurl.com/project_2/images/ etc.

    imho, @goto10 is right to use ‘get_bloginfo()’ – i missed that with my suggestion ??

    @misterpatrick67

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

    if it is:
    https://www.mysite.com/wp-content/themes/mytheme
    (which is basically get_bloginfo('template_url') )
    then the ideas in this snippet might get you further:

    <?php
    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 ??

    Thread Starter misterpatrick67

    (@misterpatrick67)

    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="https://myurl.com/'?><?php echo substr($file,1) ?><?php echo'" />';
        echo "</a></div>";
    	}?>
    Thread Starter misterpatrick67

    (@misterpatrick67)

    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.

    <?php
    	$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="https://www.myurl.com/'  . $dir . $scan[$i] . '">
    				<img src="https://www.myurl.com/' . $dir . $scan[$i] . '" alt="' . $scan[$i] . '" />
    				</a>
    				</div>';
    		}
    	}
    	};
    	?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Simple PHP Question’ is closed to new replies.