WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Simple PHP Question (9 posts)

  1. misterpatrick67
    Member
    Posted 4 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:

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

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

  2. goto10
    Member
    Posted 4 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?

    <?php
       $directory = get_bloginfo( 'url' ) . '/project/large/';
    ?>
  3. misterpatrick67
    Member
    Posted 4 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. alchymyth
    Forum Moderator
    Posted 4 years ago #

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

    http://codex.wordpress.org/Function_Reference/get_permalink

  5. misterpatrick67
    Member
    Posted 4 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?

    Thanks!

  6. misterpatrick67
    Member
    Posted 4 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. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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:
    http://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 ;-)

  8. misterpatrick67
    Member
    Posted 4 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
    Member
    Posted 4 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.

    <?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="http://www.myurl.com/'  . $dir . $scan[$i] . '">
    				<img src="http://www.myurl.com/' . $dir . $scan[$i] . '" alt="' . $scan[$i] . '" />
    				</a>
    				</div>';
    		}
    	}
    	};
    	?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.