Forum Replies Created

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter ninjaskor

    (@ninjaskor)

    No biggey,
    The Q: can I have the same variables within two different foreach-loops?
    A: Yes

    Q: Is the if ( $id == true ) {} in the foreach a valid approach?
    A: this says, if variable $id is true, do something – so I think this is valid 🙂
    But a better way may be if( !empty($id) ) that says, if the variable is not (the !) empty, do something.

    Let me konw ^^

    Peace & Love

    Thread Starter ninjaskor

    (@ninjaskor)

    No, if I use the break; on any of the two loops I get only one image.

    so question now:

    Q: Is the if ( $id == true ) {} in the foreach a valid approach? And can I have the same variables within two different foreach-loops? (no, I do not want to pass the variable value, just let them have the same name for simplicity.

    Thread Starter ninjaskor

    (@ninjaskor)

    hmm… the break; seemes to make the foreach only display one image with a limited number (get_post_gallery_ids($post->ID,4);), likeso:

    <?php 
    
    $galleryarray = get_post_gallery_ids($post->ID,4);
    
    foreach ($galleryarray as $id) { 
    
    if ( $id == true ) {
    
    $imagesize = wp_get_attachment_image_src( $id, 'xsmall' ); 
    
    $attachment_meta = wp_get_attachment( $id );
    
    ?>
    
    <figure id="<?php echo $id; ?>" data-name="<?php echo $id; ?>" style="background-image: url(<?php echo $imagesize[0]; ?>);">
    
    </figure>
    
    <?php break; }
    
    } ?>

    So I removed the break; from this loop…

    But without a limitation number: get_post_gallery_ids($post->ID);
    the break; works!

    Is it important to have the break; or can I just exclude it from both foreach loops?

    Any takers?

    ninjaskor

    (@ninjaskor)

    Skip the UI!! And make this 5 stars fo stats 😉

    ninjaskor

    (@ninjaskor)

    Sorz, did miss the word backend – the previous link is for frontend!

    So if your are looking for backend, check this support thread:
    https://ww.wp.xz.cn/support/topic/in-admin-metabox-with-thumbnail-or-medium-image?replies=3

    ninjaskor

    (@ninjaskor)

    Thread Starter ninjaskor

    (@ninjaskor)

    So, now i’m only looking for an extra eye on the php, is it valid? It works, np, but all feedback is welcome!

    For an example of what is possible (and what I did), see the code, and in relevans with my question – is my ‘if’ in the foreach followed by ‘break;’ a good way to go?

    <?php 
    
    $galleryarray = get_post_gallery_ids($post->ID);
    
    foreach ($galleryarray as $id) { 
    
    if ( $id == true ) {
    
    $imagesize = wp_get_attachment_image_src( $id, 'medium' );
    
    $attachment_meta = wp_get_attachment( $id );
    
    $media_caption = $attachment_meta['caption'];
    
    $media_description = $attachment_meta['description'];
    
    ?>
    
    	<figure id="<?php echo $id; ?>" data-name="<?php echo $id; ?>" style="background-image: url(<?php echo $imagesize[0]; ?>);">
    
    		<?php if ( $media_caption == true || $media_description == true ) { ?>
    
    			<input id="info<?php echo $id; ?>" class="nav-check" type="checkbox">
    
    			<label for="info<?php echo $id; ?>" class="nav-button info" data-title="Show Info"></label>
    
    			<figcaption>
    
    			<?php
    
    			if ( $media_caption == true ) { ?>
    
    				<h3><?php echo $media_caption; ?></h3>
    
    			<?php } 
    
    			if ( $media_description == true ) { ?>
    
    				<p><?php echo $media_description; ?></p>
    
    			<?php } ?>
    
    			</figcaption>
    
    		<?php } ?>
    
    	</figure>
    
    <?php break;
    
    }
    
    } ?>
    Thread Starter ninjaskor

    (@ninjaskor)

    Hopefully you can see the potentials with this plugin above. And I hate limitations, so this plugin is perfect.

    I will mark this question as solved and start a new thread to see if someone can give a hint about how to see if the array is empty.

    Happy coding!

    Thread Starter ninjaskor

    (@ninjaskor)

    Two down, one to go! 😀

    So, you want to get image meta like title, caption, description or whatnot?

    Then you need to add following in functions php (code is regardles of anything, use it to whatever image from the db – everywhere):

    function wp_get_attachment( $attachment_id ) {
    
    	$attachment = get_post( $attachment_id );
    
    	return array(
    	    'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
    	    'caption' => $attachment->post_excerpt,
    	    'description' => $attachment->post_content,
    	    'href' => get_permalink( $attachment->ID ),
    	    'src' => $attachment->guid,
    	    'title' => $attachment->post_title
    	);
    
    }

    But hey! How to display it?

    So, once again I edited the code and added a variable with our $id, then select the info I want to retrieve, like:

    <?php 
    
    $galleryarray = get_post_gallery_ids($post->ID);
    
    foreach ($galleryarray as $id) {
    	// select your image size
    	$imagesize = wp_get_attachment_image_src( $id, 'xsmall' );
    	// get all the meta with our $id
    	$attachment_meta = wp_get_attachment( $id );
    
    	?>
    
    	<figure style="background-image: url(<?php echo $imagesize[0]; ?>);">
    
    		<a data-name="<?php echo $id; ?>" id="<?php echo $id; ?>" href="<?php the_permalink(); echo '#' . $id; ?>">
    
    		</a>
    
    		<figcaption><h3>
    
    		<?php
    		// I wish to display the image Title, so...
    		echo $attachment_meta['title']; ?>
    
    		</h3></figcaption>
    
    	</figure>
    
    <?php } ?>

    PAO! Ninjaslap.

    Question to creator of plugin,
    Is there a better way? Or simply integrate the function for the plugin?
    I mean, the meta for every image is supernice to have handy 😀

    And do someone know how to not display -anything- if the called array is empty with the foreach?

    All best, /ninjaskor

    Thread Starter ninjaskor

    (@ninjaskor)

    One down, two to go!

    I added a variable that fetchs $id in foreach and gets the image size, superb for loadtime on different media!

    For custom image size (declared in functions.php, see here):

    <?php 
    
    $galleryarray = get_post_gallery_ids($post->ID);
    
    foreach ($galleryarray as $id) { 
    
    $imagesize = wp_get_attachment_image_src( $id, 'medium' );
    
    ?>
    
    <figure style="background-image: url(<?php echo $imagesize[0]; ?>);">
    
    	<a data-name="<?php echo $id; ?>" id="<?php echo $id; ?>" href="<?php the_permalink(); echo '#' . $id; ?>"></a>
    
    </figure>
    
    <?php } ?>

    Sorz for spelling 😉

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