I am trying to get latest wordpress posts on a static page outside the wordpress. I'm trying following code but it does not work (ie. I do not get any output).
<?php
$config_file = $_SERVER['DOCUMENT_ROOT'].'/wp/wp-config.php';
include($config_file);
$postlist = get_posts('numberposts=5');
foreach ($postlist as $post) : ?>
<p><?php the_title(); ?></p>
<?php endforeach; ?>
The wp-config file only stores those variables for use in other scripts. It doesn't define any of the wordpress functions. I believe what you are looking for would be something like this.
define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
After that, you should be able to use wordpress functions.
Related
I have a website using HTML & bootstrap and I want to add my blog to this site. I want to keep all my design the way it is, including my navigation. I don't want users to go to my WordPress page. Instead, I just want to integrate the blog part (adding the full blog from WP into my site).
My understanding is that I need to use the Loop to integrate it, but I am having trouble. Can someone please help me? Currently, I'm using the code below, but it cuts off each blog from the beginning and it's not showing the full posts:
<?php require('../wordpress/wp-blog-header.php');?>
<?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;?>
There must be a way to show my full blog posts on my site. Any help would be appreciated!!
So I figured it out after reading and reading lol.. here's the solution. It was very simple.
All I needed was to replace:
<?php the_excerpt(); ?> with the content <?php the_content(); ?>
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.
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; ?>
I have some custom post types, such as 'review'. I can't seem to find out how to make a section (e.g., www.mysite.com/reviews/) which works like the blog home page, but lists reviews instead of posts (with pagination and everything). I'd like to use a separate template for it too.
Create a page called reviews and then create a new template in your theme folder called page-reviews.php. Add all the necessary elements to the template and include a query_posts in front of your post loop. It should look like this:
<?php query_posts("post_type=reviews"); ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" >
<h2><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div><!-- Post ends -->
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, we could not find what you were looking for</p>
<?php endif; wp_reset_query(); ?>
You need help getting .htaccess to convert your categories into hard URLs. I thought WP did this automatically so you'll want to look and make sure you have set up the directory permissions on WP so it can write to your .htaccess file.
Please read this guide and it will be cleared up.
Duplicate the file in your theme called "single.php" and rename it to "single-reviews.php"
Since "single.php" is used for your regular posts, you can append the name of the custom post type to the end of "single-" to automatically use that.
Now once inside of the file "single-reviews.php" you can adjust the layout to be how ever you want.
If you get a 404 error, or it is not showing you the correct layout, you may need to flush the rewrite rules. You can do this two ways.
Go to the permalinks page in your backend and it will sometimes auto flush them.
The best way to do it is in your "functions.php" file in your theme directory add the following code:
add_action ( 'init', 'flush_rewrite_rules' );
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Create a new page called reviews. Create a new page template that calls the custom post type. Assign the page template to the page...
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)