Integrating Wordpress page into existing HTML site - php

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.

Related

php code order being changed by server?

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

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

error wordpress, adjusted sidebar.php to show latest 10 posts

I'm trying to edit my sidebar.php file in my current them WP is using to display the last # of posts (only the titles) as links.
I tried using the example of http://codex.wordpress.org/Integrating_WordPress_with_Your_Website but I always get the error on the line that states where the file wp-blog-header can be found.
the error when opening the index blog page where the sidebar should be shown:
// Get the last 3 posts.
Warning: require(/blog/folder/wp-blog-header.php) [function.require]: failed to open stream: No such file or directory in /blog/folder/wp-content/themes/default/sidebar.php on line 7
So what is wrong?
Is there a way to permanently embed a function in my html template page that retrieves the latest few posts everytime an article is displayed on the template page?
the code:
<?php require('/the/path/to/your/wp-blog-header.php'); ?>
<?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
You don't need the require. Also, I've cleaned up the code a bit.
Try this and tell me does it work.
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) :
start_wp();
the_date();
echo "<br />";
the_title();
the_excerpt();
endforeach;
?>
How about using this one liner :
<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>
all you have to do is post it in sidebar.php
neater, no ?

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.

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