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.
Related
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");
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();
I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.
Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/
You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>
Someone explain to me what either the server or browser is doing here:
Integrated a wordpress blogs last 3 posts to display on home page of non wordpress site and simply was going to apply some inline CSS styling to the post:
<?php
$posts = get_posts('numberposts=3&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post );
echo '<span style="color:blue;font-size:10px;">'.the_date().'</span><br>';
the_title();
the_excerpt();
endforeach;
?>
The styling does not occur because the echo line is being rendered by the browser/server/god/chaos or whatever AFTER the the_date() variable????
Actual rendered code:
<div id="blog-box">
November 11, 2013<span style="color:blue;font-size:10px;"></span><br>Hello world!<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
</div>
Sorry if I'm missing something obvious but i don't get why this is not surrounding the date with the span code being echoed.
Thanks.
the_date() has a built in echo.
Try this
echo '<span style="color:blue;font-size:10px;">';
the_date();
echo '</span><br>';
I wrote this PHP code to put on a wordpress page template:
<?php
query_posts('showposts=10&cat=7');
while (have_posts()) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; ?>
When I view the page I don't see any result and the right bar of the browser continue to reduce itself. I have understand that the code create an infinite loop.
Where I mistake?
Thanks
Firstly, you should not use query_posts. It's too invasive for simple loops, and messes with the entire WP_Query. Also showposts should be posts_per_page.
Secondly, it's hard to gauge what this issue is without more context. Perhaps pastebin your entire page, and edit it into your question. My guess is a loop within a loop, and should stop at like a 100 posts. (10 X 10) but if it's reset anywhere else if could very well go infinite!
Use this code instead to create loops:
$custom_query = new WP_Query( 'posts_per_page=10' );
if($custom_query->have_posts()) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
//global $post; // for stuff like $post->post_name
// Post stuff here
// the_title();
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Look at the WordPress codex for more details. http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
You should use an if statement in your loop:
<?php
query_posts('showposts=10&cat=7');
if ( have_posts() ): while ( have_posts() ) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; endif; ?>
Without further information, I would say that in each loop of the while statement, the function is returning the first row of the data? So each time the while loop executes you are calling the function again, which is returning the same row over and over again without actually iterating through the result set.