Forums

[resolved] How to generate different images per page? (17 posts)

  1. jlau
    Member
    Posted 1 year ago #

    This is very basic question, yet I'm having the hardest time trying to figure out how to pull this off.

    I have a side navigation set to a separate template php file called "sidenav.php" and it is this file that is pulling ONLY the sub navigation for the interior pages on my site. This side navgation is prevalent on every page except for the contact and blog section. There is a spot above the nav for ONE image or THUMBNAIL.

    Here are my questions for this dilemma:

    1. How do I go about doing this?
    2. How do I apply the loop to this?
    3. What is the <?php something_image(); ?> or "tag" should I be using?
    4. How do I embed this "tag". Where should it go?
    5. How should I set up my file structure to have the tags/functions pull images from my images folder?
    6. How should I set up my images folder or how should I name the files?

    If I missed out on anything, please let me know. This is basic stuff I know, but I've spend hours already just reading WordPress documentations and it's not making any sense to me.

    Thanks for reading this post. Hope to hear from you soon!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try using get_posts if you want a secondary loop in your sidenav. However, you don't mention where these images are supposed to be pulled from.

  3. jlau
    Member
    Posted 1 year ago #

    Thanks for replying back to me so soon!

    I totally agree with you, but that's exactly what I'd like to know. The images currently reside in the "images" folder, but how do I ask the loop or the function to pull them?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    If this is just a bunch of images sitting in a folder then you don't ask the Loop to do anything. A WP Loop won't work unless the image details can be pulled from the database. You just need plain ole PHP to pick a random image and display it. The only WP functions that you'll need are get_stylesheet_directory and get_stylesheet_directory_uri.

    The former will give you the path to the images folder assuming that it's placed within your current theme's folder. And the latter will give you the image url when it's time to display it. The rest is just down to how you intend to choose a random image.

  5. jlau
    Member
    Posted 1 year ago #

    Is there a way to pull the images from the folder, but not in "random" order? The sidebar navi is a template tag piece that lives on every page, but the image/thumbnail above the navi should be dynamic. Meaning the "about" page is one specific image, the "meet us" page has a specific image, and so on....

    Thanks!

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Why not add a featured image to each page and use the_post_thumbnail?

  7. jlau
    Member
    Posted 1 year ago #

    This post comment is extremely hacked up. So pardon the confusion.

    I looked into this before. This was the site I was looking at the code: but does all this mean?

    $default_attr = array(
    			'src'	=> $src,
    			'class'	=> "attachment-$size",
    			'alt'	=> trim(strip_tags( $attachment->post_excerpt )),
    			'title'	=> trim(strip_tags( $attachment->post_title )),
    		);

    I would assume I plug the <?php the_post_thumbnail( $size, $attr ); ?> into my <img src=""/> tag? Should I use the ? If so, in the example it asks you to plug in "<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>"

    I know what $page->ID means, but how does the code know which page i am specifically in?

    Thanks!

  8. jlau
    Member
    Posted 1 year ago #

  9. jlau
    Member
    Posted 1 year ago #

    [Code moderated as per the Forum Rules. Please use the pastebin]

  10. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    how does the code know which page i am specifically in?

    The page ID is part of the results that are returned by the query - either the standard WP query or a custom query using get_posts or query_posts. Have you tried just using the_post_thumbnail()?

  11. jlau
    Member
    Posted 1 year ago #

    I actually have the the_post_thumbnail embedded into my code.

    My only confusion is trying to understand what this code means:

    <?php if ( has_post_thumbnail()) : ?>
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
       <?php the_post_thumbnail(); ?>
       </a>
     <?php endif; ?>

    Thanks!

  12. jlau
    Member
    Posted 1 year ago #

    I took out the image like you suggested and implemented the only the "the_post_thumbnail()." It's a blank now, which I guess is a good thing. But how does it know which pic goes with each page?

    Thanks!

  13. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Translation: If this post (or page) has a post_thumbnail (aka featured image), output a link to the post using the featured image thumbnail as the link "text".

    So the code is using the post thumbnail to create a link to the associated post.

  14. jlau
    Member
    Posted 1 year ago #

    Ah ha! Thank you! So...does this mean I need to change the naming scheme of the images themselves? Like for example, "about page" I would need to name the image file about-feature-image.jpg.

    About -> the page
    Feature - > the post
    Image -> image

    This image would reside in the images folder and the 'src' => $src would be $images? Assuming "src" is finding only folders.

    I know this is very rudimentary, but am I breaking this down correctly?

    Thanks!

  15. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    No - you don't need to change any image filenames. You just need to have set up a featured image for each page/post. WP stores the association between featured image & post in your database.

  16. jlau
    Member
    Posted 1 year ago #

    Thanks! Let me give this a try.

  17. jlau
    Member
    Posted 1 year ago #

    Hello again!

    I have an <img src> link attached to the navigation php file.

    <img src="<?php if ( has_post_thumbnail()) : ?>
    	   <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
    	   <?php the_post_thumbnail(); ?>
    	   </a>
    	   <?php endif; ?>"/>

    But it's not pulling up the images from my images folder.

Topic Closed

This topic has been closed to new replies.

About this Topic