php code order being changed by server? - php

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>';

Related

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 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();

Integrating Wordpress page into existing HTML site

I've run into a bit of a wall with a site I'm designing for a client. This was originally a very low budget static site but the client now needs the ability to edit some content from time to time. I've heard that you can just embed a wordpress page into existing HTML site and have followed the steps on the Codex site but can't seem to get it working. Any help is greatly appreciated.
So the wordpress page I'm trying to embed is the following:
http://octagonclubmiami.com/cs/
And the code I'm using is as follows:
This is posted at the beginning of the php file
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('cs/wp-blog-header.php');
?>
And this is within the body of the php file
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
The actual page I'm trying to embed into is the following:
http://octagonclubmiami.com/community_service_test.php
As it sits it's currently displaying I guess some sample post instead of the page mentioned earlier.
Thanks in advance!
The better solution (I think) is to use get_post() and specify the ID number of the post/page you wish to output:
<?php
$page = get_post( 'ID NUMBER HERE' );
setup_postdata( $page );
?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
You can identify the post's ID number in the WordPress back-end.

show wordpress post outside non wordpress php page

I need to display the wordpress blog posts in a non wordpress php page. I have tried the following code.
<?php
// Include WordPress
define('WP_USE_THEMES', false);
//exact path for wp-load.php.
// This file is kept in the root of wordpress install
require('http://test.com/wordpress/blog/wp-load.php');
//Query wordpress for latest 4 posts
query_posts('showposts=5');
?>
<?php while (have_posts ()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
But it showed me the following error
Fatal error: Call to undefined function query_posts()
How to fix this?
please look into the following line in your code
require('http://test.com/wordpress/blog/wp-load.php');
in the require function you should use the relative or physical path. You should not include the url.
Since you need the wordpress database and framework this seems very unlikely to work at all. Try getting XML, RSS or JSON data from your wordpress which you can fetch with a self-made script.
For external integration, it's likely more reliable to approach this via RSS route. Simplest and possibly laziest way of making this work is by use of simplexml_load_file (and HTTP streams.)
$t = simplexml_load_file( "http://blogs.voanews.com/breaking-news/feed/" );
foreach( $t->channel->item as $item ) {
printf(
"<div>%s <a href='%s'>%s</a></div><hr/>",
$item->description,
$item->link,
$item->title
);
}
This outputs the feed as you'd expect to see it. Note that this doesn't use any kind of caching, so every page request hits the original feed.
<div>Some Chinese officials are furious at Apple's iPhone for apparently
helping users have too much of a good time. Chinese media say the complaints
surround the iPhone's voice-activated personal assistant, known as
“Siri,” which has been helping some users find prostitutes and
brothels. The Mandarin language version can apparently present users with as
many as [...] <a href='...(snip)...)'>iPhone Under Fire in China over
Prostitution</a></div>
You could use the rss option, you could write a new, hidden code and read data using that file ... in JSON format
Maybe the first thing to do is search for a extention/plugin/module that does this for you;
You are not the first one who wanted to do this, i think :p
For Displaying recent posts outside wordpress setup, first include wp-load.php file.
require( './blog/wp-load.php' );
// Load the recent top 10 posts
$args = array( 'posts_per_page' => 10, 'post_status'=>"any", 'post_type'=>"post", 'orderby'=>"date","order"=> "DESC", "suppress_filters"=>true);
$postslist = get_posts( $args );
Now you can loop through $postlist variable.
foreach ($postslist as $post) : setup_postdata($post);?>
<ul class="media-list">
<li class="media">
<div class="media-left"> <?php the_post_thumbnail( array(80,80));?> </div>
<div class="media-body">
<a target="_blank" href="<?php the_permalink();?>"><h5 class="media-heading"><?php the_title(); ?></h5></a>
<p><?php echo $post->post_content; ?></p>
</div>
</li>
</ul>
<?php endforeach;
After you include the wp-load.php you have to instantiate the query like this:
$wp_query = new \WP_Query();
$wp_query->query('showposts=5');
After that your loop should look like this:
<?php while ($wp_query->have_posts()) :
$wp_query->the_post();
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>

access and display wordpress posts from out of wordpress

I have a commercial site (php), and have a Wordpress blog in a subdirectory. I need to display latest posts at homepage which is out of Wordpress :/
site:
http://www.blabla.com
blog:
http://www.blabla.com/blog/
So I need to display posts at www.blabla.com/index.php. How can I access Wordpress functionality?
Thanks a lot! appreciate!
The easiest way is to consume your Wordpress RSS feed.
Download it using file_get_contents() or cURL for more control.
Parse it with simpleXML and output it.
You'll probably want to cache it somewhere... you could use APC user functions or PEAR::Cache_Lite.
Edit: the code would look something like this (you'd want more error checking and stuff - this is just to get you started):
$xmlText = file_get_contents('http://www.blabla.com/blog/feed/');
$xml = simplexml_load_string($xmlText);
foreach ($xml->item as $item)
{
echo 'Blog Post: <a href="' . htmlentities((string)$item->link) . '">'
. htmlentities((string)$item->title) . '</a>';
echo '<p>' . (string)$item->description . '</p>';
}
Using WordPress best practices, you shouldn't be loading wp-blog-header.php, but rather wp-load.php, as it was specifically created for this purpose.
After this, use either the WP_Query object or get_posts(). An example of how to use WP_Query is available on The Loop page on the WordPress codex. Although using either of these doesn't matter if you use them from outside WordPress, there's less chance of something interfering, such as GET parameters.
For example, using WP_Query:
<?php
$my_query = new WP_Query('showposts=3');
while ($my_query->have_posts()): $my_query->the_post();
?>
<h1><?php the_title() ?></h1>
<?php endwhile; ?>
Or, using get_posts():
<?php
global $post;
$posts = get_posts('showposts=3');
foreach($posts as $post) :
?>
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
Hope this helps! :)
hey just found a solution online;
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
works great!
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I guess the easiest solution is to take posts directly from database.

Categories