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.
Related
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'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.
I'm building in Wordpress and have some content that loads in using jQuery.load.
$item = '<h2>'. the_title() .'</h2><div>'. the_content() .'</div>';
//$item = include 'myrequire.php'; This doesn't seem to work.
I'd like to know how to make the php variable $item load a php file - so I can keep functions.php tidy and can just write the file like:
<h2><?php the_title(); ?><h2>
... does that make sense?
Thanks!
I followed this tutorial:
http://tomsbigbox.com/wordpress-load-more-posts-on-page-scroll/
here's the full snippet:
function getArchives($count,$offset){
query_posts('posts_per_page='.$count.'&offset='.$offset);
$posts = array();
if(have_posts()) : while(have_posts()) : the_post();
$item = '<h2>'. the_title() .'</h2><div>'. the_content() .'</div>';
//$item = include 'myrequire.php';
array_push($posts,$item); endwhile; endif;
return $posts;
};
http://www.php.net/manual/en/function.file-get-contents.php
You want to know the content of a php file?
include executes the file, you want file_get_contents
$var=file_get_contents($filename);
youcan also do it the wordpress way with get_template_part but that depends on what you want to do. I would look at the codex http://codex.wordpress.org/Function_Reference/get_template_part
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; ?>
i need to get page ID in wordpress throught php?
You want to use the_ID() within the loop.
Assuming this is for a Theme, it's as simple as this.
There is a global variable "$post" which contains the related information of the current post / page, and is actually an object. You can access information just as you access variables from an object. Remember to keep it in the while loop.
For example, confider the following:-
<?php if (have_posts()) : ?>
<?php
while (have_posts()):
the_post();
global $post;
$idPagePost = $post->ID;
endwhile;
?>
<?php endif; ?>
Now the variable "$idPagePost" will contain the ID of the current page / post.
Hope it helps.
global $wp_query;
$id = $wp_query->post->ID;
// OR:
$id = $wp_query->queried_object_id;
This will work anywhere in your themes or plugins, as long as it happens after WordPress is loaded.