Looping through an array in Wordpress - php

I have created a custom post type with an image gallery upload. Now I am trying to display the gallery on the front end. This is what I have so far that works to display 1 image, but if multiple images are uploaded all the urls get stuck in the src tag. So I'm guessing I should loop through that array and spit out each one separately? Would that be the route to go and if so how can I accomplish this? Any help is appreciated.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
echo '<img src="'.get_post_meta($post->ID, 'gallery-upload', true).'">';
?>
<?php endwhile; else: ?>
<p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>
EDIT:
This is what is being returned:
<img src="http%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F800x400-volbeat-mock1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1574_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_4_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F2244_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F300789_2349086884438_1168050047_32154880_1451576942_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F373795_278881222158106_278880528824842_834930_1454244548_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F20110909-121141.jpeg">

Looking at the function reference, get_post_meta ordinarily returns an array unless the third argument is set to true. Something like this should work, more or less.
<?php
foreach(get_post_meta($post->ID, 'gallery-upload') as $meta) {
foreach(explode(',', $meta) as $src) {
echo '<img src="'.htmlentities($src).'">';
}
}
?>
EDIT: Apparently gallery-upload is stored as comma-separated values. Updated my snippet above to hopefully account for this.

Related

WordPress Advanced Custom Field gallery doesn't return an array

I have ACF Plugin installed and I have a gallery filed in my post. I've tried all these docs but still getting the error :
Invalid argument supplied for `foreach()`
this happens because the input of the for each is not an array!
Do you have any clue what's wrong with this?
Do you think if I have to set something while I've defined the custom field?
<?php
$images = get_field('mygall');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I think your problem comes from the fact that you are using get_field() instead get_fields(). That's way you don't get an array.
If it still doesn't work check the documentation for get_fields() here. Try to debug it like using only get_fields() and see what is the output. If it is an empty array then it means that you are calling the function out of the loop and it can't get the post id. So do a second test with manually setting the post id like get_fields(123); and check the results. If there are no results then there is something wrong with that post. And if there are results then you can do a final test with checking what will be the result of get_fields(123, 'gallery').
All the above debugging can be wrapped in something like:
echo '<pre>';
print_r( get_fields(123) );
echo '</pre>';
Basically this will give you some idea of what is the structure of the data that you get from this function and how you can manipulate it so to get what you need.

ACF page while loop breaks footer while loop

So basically i have a main page and a footer page. Both are seperate .php files.
Im using ACF for this site.
Following the documentation, ive created a while loop for my 'flexible content' in the main page and it works, displaying all the data that gets looped and hooked from the CMS input fields.
My Problem is in the footer, i have a while loop that displays links, but it wont display unless i remove the while loop from the main page, then the links display in the footer.
I honnestly dot get why this happens ive tested allot and get my head wrapped around this, please help.
Main page code:
<?php
// check if the flexible content field has rows of data
if( have_rows('flexible_content_field_name') ):
// loop through the rows of data
while ( have_rows('flexible_content_field_name') ) : the_row();
// check current row layout
if( get_row_layout() == 'gallery' ):
// check if the nested repeater field has rows of data
if( have_rows('images') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
echo '<li><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></li>';
endwhile;
echo '</ul>';
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
<?php get_footer(); ?>
Footer Code:
<div class="links">
<?php
if( have_rows('footer_page_links', 'option') ):
var_dump("test");
while( have_rows('footer_page_links', 'option') ): the_row();
?>
<p><?php the_sub_field('footer_link_name'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php wp_footer(); ?>
I would just like to add , not even the vardump() displays in the footer if main page while loop is implemented so it never gets inside the footer loop. The footer uses ACF option page - > LINK
Also all other option fields in footer displays, if its not within the while loop. I have removed the main page while loop then the footer while loop works, and this only happens with flexible content, my other pages with loops, that does non consists of flexible content works perfectly.
So This issue is resolved on my side, after contacting the ACF guys, they made a duplicate of what i did and could not recreate my issue.
Which got me thinking since i had the latest Wordpress (Version 4.9.7) the only difference is the hosting.
What i used on localhost was XAMP Version 3.2.2, which i did not think was the problem, but it was, so upgraded to a live server and everything works as expected, so for future ref should you run into these simple unexplained code errors, check the hosting, or upgrade.

wordpress looping through posts in custom php page

I trying to loop through posts in a custom php page but no matter what I do, no posts are found
here is the code I wrote in my-custom-page.php
<?php
require_once("/wp-load.php");
get_header();?>
<div id="blog">
<?php if(have_posts()) : ?>
<?php echo"anything"; ?>
<?php endif; ?>
</div>
<?php get_footer();?>
You should require wp-load.php via the full path to this file.
Hardcoded example:
require_once("user/home/public-html/wordpress/wp-load.php");
Softcoded example (suposing your file is in the same directory as WordPress):
require_once(dirname(__FILE__)."/wp-load.php");
You have also to query the posts before you display them. So, you need to add this line to your code:
query_posts('post_type=post');
The query arguments may vary depending on what you want to display. Some of them are the member variables of the WP_Post class. Go to https://codex.wordpress.org/Class_Reference/WP_Post for reference.
Here you have a re-writing of your code that displays the titles of the 30 latest posts published:
<?php
require_once(dirname(__FILE__)."/wp-load.php");
query_posts('post_type=post&showposts=30');
get_header();?>
<div id="blog">
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
the_title();
echo '<br />';
endwhile;
else :
echo 'Sorry, no posts found.';
endif;?>
</div>
<?php get_footer();
wp_count_posts :
#return object Number of posts for each status.
you're trying to echo an object which ends in a fatal error. Furthermore if you want to see all posts the_post is not right. Look for it at the function reference : https://codex.wordpress.org/Function_Reference/the_post. I would do it other (google smth like "get all posts").
if you will use the code inside your theme
use the same code of Mr.Carlos but with out dir
require_once("/wp-load.php");

Wordpress post Loop not working as expected

I'm using a theme that uses the blog page to display all of its content on the blog page rather than an excerpt and then when i click into the post i want to show the whole content.
I'm using the following code:
$postId = get_the_ID();
$ex = the_excerpt();
if($postId == 19){
echo $ex;
}
else{
echo $content;
}
The blog page is located at post =19
I would expect only the excerpt to show on the blog page and the content to show on the post page. However both show. Also it doesnt matter if i change the number 19 in my if statement as the same happens. Can anyone see where i am going wrong?
edit made changes, screen shots:
You need to use the_excerpt(); inside the condition means where you want to display the excerpt but in case you want to get value of excerpt but does not want to display it directly then you have to use get_the_excerpt(); so you need to change
$ex = the_excerpt();
to
$ex = get_the_excerpt();
And same is the case with the_content()
Hope it helps you.
functions such as the_excerpt() are only available inside the loop or after you call the function the_post()
You may want to display only the except in your index.php
while (has_posts()) {
the_post();
the_excerpt();
}
In your single.php you may want to display the whole content
if(has_posts()) {
the_post();
the_content();
}
Use $postId = get_queried_object_id(); instead of $postId = get_the_ID();

Is the syntax problematic with this PHP code for Wordpress?

Seems like the problem with this is the PHP syntax, but no luck in Wordpress forums. This first code block generates a link to the newest post in category "posts."
<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
This next code block should display the custom field data for the latest post in "posts," with the key of the custom field being "qanda." But it doesn't and it displays nothing.
<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo get_post_meta($post->ID, "qanda", $single = true); ?>
<?php endwhile; ?>
Thanks, Mark
try renaming your second query, otherwise Wordpress will think it is already done
<?php
$my_other_query = new WP_Query('category_name=posts&showposts=1');
while ($my_other_query->have_posts()) : $my_other_query->the_post();
echo get_post_meta($post->ID, "qanda", true);
endwhile;
?>
Apart fromthat $single = true should just be true it looks OK... try var_dump instead of echo and see what you get.
You might need to name it something different. Wordpress might think that you have already done that set of posts, so it is starting at the end, which means it doesn't have anymore posts to process.

Categories