Falls man auf seine wordpress Installation von einer unabhängigen Seite (d.h. ein "leeres" PHP-Script) zugreifen möchte, kann man wie folgt die wordpress API einbinden:
API Einbinden
<?php
/<em> include wordpress API </em>/
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?> |
<?php
/<em> include wordpress API </em>/
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
Nun stehen einem im Script alle [http://codex.wordpress.org/Function_Reference/|Funktionen von wordpress] zur Verfügung!
Hier zwei Beispiele, wie man damit die letzten drei posts abruft und wie man selbst einen neuen post erstellt:
Die letzten 3 Posts abrufen:
<?php
global $post;
$args = array( 'posts_per_page' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br />
<?php endforeach; ?> |
<?php
global $post;
$args = array( 'posts_per_page' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br />
<?php endforeach; ?>
Einen neuen Post erstellen (sehr nützlich bei Migrationen von einem anderen CMS)
/**
<ul>
* A function used to programmatically create a post in WordPress. The slug, author ID, and title
* are defined within the context of the function.
*
* @returns -1 if the post was never created, -2 if a post with the same title exists, or the ID
* of the post if successful.
*/
</ul>
function programmatically_create_post() {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
// Setup the author, slug, and title for the post
$author_id = 1;
$slug = 'example-post';
$title = 'My Example Post';
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'post'
)
);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
add_filter( 'after_setup_theme', 'programmatically_create_post' );
$post_id = programmatically_create_post()
if( -1 == $post_id || -2 == $post_id ) {
// The post wasn't created or the page already exists
} // end if |
/**
<ul>
* A function used to programmatically create a post in WordPress. The slug, author ID, and title
* are defined within the context of the function.
*
* @returns -1 if the post was never created, -2 if a post with the same title exists, or the ID
* of the post if successful.
*/
</ul>
function programmatically_create_post() {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
// Setup the author, slug, and title for the post
$author_id = 1;
$slug = 'example-post';
$title = 'My Example Post';
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'post'
)
);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
add_filter( 'after_setup_theme', 'programmatically_create_post' );
$post_id = programmatically_create_post()
if( -1 == $post_id || -2 == $post_id ) {
// The post wasn't created or the page already exists
} // end if
Quellen