How can I include Wordpress posts in a custom PHP file? - php

This is what i want to do:
I want /summary.php to include 5 latest posts (only the extract) from my blog, which lives in /wp.
Is there any way to include Wordpress in /summary.php and only print the html for these posts? (Maybe i should parse the rss?)

Take a look to Integrating WordPress with your Website
This is an example from that page, that shows the first ten posts in alphabetical order:
<?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_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
Use $posts = get_posts('numberposts=10'); if you want the 10 latest posts.

Probably the easiest and most elegant way to do this is to create a custom theme to live on summary.php. The WP library exposes a number of functions for easy output of articles.

I think you have answered your self their. The RSS feed will give you the content of your latest posts.
With not much work you can just pull out the data you need

You can create a "clean" template, which you can apply to 'summary' page (this page must be a wordpress page too).
You can find an example here:
http://www.tyssendesign.com.au/articles/cms/fetching-posts-in-wordpress-expressionengine-with-jquery-ajax/

you can include wp-config.php, which will pull in the rest of the API. then you will be able to use wp functions like
function get_post($postID)

Related

Required Wordpress specific category posts display on static page with newer order at the top

I have two different post categories ("Local" & "International") in WordPress and I have displayed the posts in my static page. Right now both categories post are coming on one page. Can I show only local posts in local.php and International posts in International.php page? Also another change please. Can I show newer posts at the top? Right now older post is at the top.
Following is my code.
<?php
require('wp-blog-header.php');
?>
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : start_wp(); ?>
<?php echo "<h1>";the_date();echo "</h1>"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
Assuming custom page as a page in yours custom plugin. you will need to load wp-load.php to use the WordPress function/loops to fetch the information.
you will need
require_once("../../../wp-load.php"); // ../../../ according to you file location
// now you can loop using get_posts
$posts = get_posts('numberposts=10&category=CATEGORY_ID&order=DESC&');
// loop
If this is not in WordPress, you have to connect to MySQL and the fetch the information from tables with you SELECT query.
Use
$posts = get_posts('numberposts=10&order=DESC&category=[categoryID]&orderby=post_title');
in local.php respectively International.php. Notice that order=DESC instead of order=ASC, this reverses the post order. And be sure to use the appropriate categoryIDs. You find them in your Wordpress administration area.

How do I display one (featured) post from Wordpress on my static html website?

We have a Wordpress News Blog on our website (http://www.litepanels.com/news/) that is separate from the rest of the static html website. I just want to take the latest story (the featured post) and display it on our home page. The Blog and our website are on the same server. I found I can grab post titles like this:
<?php
require('../news/wp-blog-header.php');
?>
<?php query_posts('showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile;?>
but I do not know PHP well, how do I grab just the featured post (which has a title, image and text as you can see in the link above)
I am testing it here: http://www.litepanels.com/newwebsite/blog_test2.php
If that works for you, it means you've got all you need except for some way to display your information. For this, Wordpress provides some template tags that can be used within the while loop. the_content is the one you need. Others can be found in the Codex.

What to include in php page to use wordpress's functions?

I have a wordpress website and I want to create a page that also has javascript and upon a certain user action the javascript calls the server with an AJAX request.
This is a GET request to a php script I created. Since I extended wordpress with my plugin I put this php in my plugin's folder.
The problem is that from this php script I want to access everything that wordpress offers, e.g. the database access, but I do not know how.
What do I have to include in this php file in order to access the functions offered by wordpress? I wanted to use database access so I included the wp-db.php file and declared the global wpdb variable, but it did not help.
Can anyone tell me how to accomplish this?
Thanks in advance!
Wordpress has it's own build in ajax: http://codex.wordpress.org/AJAX_in_Plugins
Use this instead of your own ajax script.
In this ajax script you can use all WP functions. It will also make your plugin more like wordpress standards.
As recommended in he WordPress Codex itself.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
By using the blog header no database queries are executed by default, you have to supply the get_posts that will fetch what-ever articles you need based on your application's parameters:
<?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); ?>
<?php foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>

WordPress PHP Plug-in - Post to external website via save_post command

I use stackoverflow very often and just found out in google about this one, Nice :D
Well,
i want to save my every post i write from wordpress to my 3rd (OEXChangeble) website aswell at the same time, so i need to send the info to my website, developing a plugin iguess
I need basically the permalink (and i would be able to extract the rest of the params from my wesite), but better if i can get title, tags, permalink and description(or some of it)
i understand by my google research that all i need to do is add something like
<?php
//header of plugin
function myFunctionThatSendsMyWebsite($url){
// procedure containing a file_get_contents('myqwebsite?url=') request to my website
}
add_action('page_post', 'myFunctionThatSendsMyWebsite', $permalink));
?>
I have problems thoug finding the name of variables i have missing (marked by ???). I know that $post contains all objet, how to extract the info from it (if there is), or if it's complicated, it would be enought for me with permalink
Any tip?
Thanks!
according to this link, this should work, the post id will be sent to this function automatically and you can get anything you want from a post id.
function myFunctionToSendPost($post_ID) {
$post = get_post($post_ID);
$title = $post->post_title;
$content = $post->post_content;
$permalink = get_permalink($post_ID);
...
sendToYourServer($params);
return $post_ID;
}
add_action('publish_post', 'myFunctionToSendPost');
BTW, this function is called when a post is published, you can change it to happen when saved by
add_action('save_post', 'myFunctionToSendPost');
add these lines to your theme's functions.php file.
1) While this doesn't take advantage of the save_post feature, you can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the WordPress directory on the original site. Simply modify the require() in the first block on this page to use the full path to your WordPress installation:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('/var/www/example.com/wordpress/wp-load.php');
query_posts('showposts=1');
?>
position your post with a while loop:
<?php while (have_posts()): the_post(); ?>
<?php endwhile; ?>
if you want to specify which parts of the post to display use this code:
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p>
<?php endwhile; ?>
2) How about using the rss feeds from your wordpress blog?
The following code will display a list of your feed titles with descriptions including a hyperlink to the original WordPress posts:
<?php // Load the XML file into a Simple XML object
$filename = http://feeds.feedburner.com/<em>yourfeedname</em>";
$feed = simplexml_load_file($filename);
// Iterate through the list and create the unordered list
echo "<ul>";
foreach ($feed->channel->item as $item) {
echo "<li><a href='" . $item->link . "'>" . $item->title . "</a></li>";
}
echo "</ul>";
echo "<li>" . $item->description . "</li>";
?>
3) Feedburner has a free feature called BuzzBoost where you can get your posts to show in a regular HTML website that once activated you can simply copy the script that they provide into your HTML where you want the list to appear. From your feedburner account you can adjust some elements like whether the Blog title should appear or not, the format of the date, etc...
You can also style the output using regular CSS within you websites existing CSS.
Check it out Feedburner here:
https://www.google.com/accounts/ServiceLogin?service=feedburner&continue=http%3A%2F%2Ffeedburner.google.com%2Ffb%2Fa%2Fmyfeeds&gsessionid=5q8jqacBluH1-AnXp08ZFw
Are you trying to save a copy of the post somewhere else when they first publish the post? Or on every page view?
You can trigger it when the post is saved by writing a plugin that implements the save_post hook:
http://codex.wordpress.org/Plugin_API/Action_Reference
To do it on every page, you'd probably write a plugin with a filter hook on the page (if it's something you want other people to use) or if it's just one site, you could add it to your theme.
But... Are you sure you want to do this? It seems like your keepyourlinks site might be better implemented as an update service: http://codex.wordpress.org/Update_Services

WordPress: How to display only posts that are in a certain category?

I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..
However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.
Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
I have a post that if I go to it directly works correctly, but does not show up on this page.
The post direct link.
The category id is '3' while the category name is 'typographic-posters'.
I have a custom page template for the typographic-posters page that looks like this:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Using this code however the page only shows gets the header, sidebar and nothing else..
If someone could help me out that would really help me get a handle on this filtering of wordpress categories.
Thanks for reading,
Jannis
in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.
Once obtained, just use the normal WordPress loop to access these posts.
The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.
in_category will only work outside of the loop on a single page. I
suggest using the query_posts function to solve this problem. You may
use query_posts('cat=3') or
query_posts('category_name=typographic-posters') to get the posts you
are looking for.
Once obtained, just use the normal WordPress loop to access these
posts.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Simply add before the loop:
<?php query_posts="cat=3&showposts=5">
This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).
I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.
But if you need to stick with a Page to display those posts, also see the Page of Posts example.
http://codex.wordpress.org/Template_Tags/query_posts
Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.
This plugin could also help you if you want to be able to change the displayed categories without going through the code :
http://wordpress.org/extend/plugins/advanced-category-excluder/
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need

Categories