Компьютер

ЧП Куликов Михаил Михайлович

Тел:

+380-(50)-589-55-02

Loading
Мы в социальных сетях...

    -  Вконтакте
    -  Facebook
    -  Google+
    -  Twitter
    -  LiveJournal
    -  Я.ру
    -  Одноклассники
    -  RSS
    -  Мой Круг
    -  YouTube
Прочее...

/images/certificate_small.png

Анализ: траст сайта it-kmm.com
Внимание!!! Заработал на 100% новый сервис.

Display WordPress content ouside of your blog

Оригинал статьи: http://www.corvidworks.com/articles/wordpress-content-on-other-pages

WordPress is a very powerful and flexible content management system (CMS) for blogs, and is among the first choices for anyone looking for a way to include dynamic content on their web sites. When using WordPress to power a news or blog section of an existing web site, authors will often want to display a list of headlines or the latest post on the front page of the site, outside of the WordPress-powered section. Using a bit of PHP and the WordPress API, this is easy to do.

This article assumes a basic familiarity with modifying WordPress templates, particularly the “loop” API.

Anoteaboutterminology

In this, as well as all my tutorials, I use the domain name example.com to refer to your domain. When you see this, substitute your actual domain name. As well, in this article I will assume you have installed WordPress into a directory called /wordpress. If you use a different name for your WordPress folder, just replace “wordpress” in any paths below with whatever name you chose.

The key to gaining access to the power of WordPress from an outside page lies in the wp-load.php file. This file loads the WordPress application and makes its API, and therefore your content, available for use. Once this file is included in a page on your site, you will be able to use any WordPress function just as if you were working in a WordPress template.

Let’s add the latest post to the front page of our site. We’ve already got a blog at www.example.com/wordpress/ but it would be nice to show the latest entry alongside the rest of the information on the front page. If your front page is already a PHP file, you’re ready to go; otherwise you’ll probably need to rename your front page file extension from .html to .php. (Be sure to test to ensure that doesn't break anything!)

The first thing we’ll do is pull in that all-powerful WordPress file, which we can do by adding this section to the very top of the page you want your post to appear on:

 <?php // Include WordPress define('WP_USE_THEMES', false); require('./wordpress/wp-load.php'); query_posts('showposts=1'); ?> 

The first line of this block is simply a comment describing what we’re doing. The second line tells WordPress not to display your templates, and the third line grabs that file I told you about. The final line performs a database query to retrieve the content we want to display on this page. In this example, we want the single most recent post, but the query_posts() documentation shows how to retrieve a variety of data.

Now we’ll move down in our page and find a good spot for this post. Once you’ve figure out where to put it, we’ll add a WordPress loop which will display the single post retrieved above:

 <?php while (have_posts()): the_post(); ?> <?php endwhile; ?> 

Once the loop is in place, we just have to decide what parts of the post to display, and write some HTML for them. Let’s say we want the post title and just an excerpt, followed by a link to the full article. We'd do something like this:

 <?php while (have_posts()): the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> <p><a href="<?php the_permalink(); ?>">Read more...</a></p> <?php endwhile; ?> 

If you’ve been writing WordPress themes, you’ll immediately recognize the template tags used here. We’re using all the same functions for the title, excerpt, permalink, etc, that we do in a WordPress template. Including wp-load.php makes this all possible.

Bonus

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

Note: PHP restrictions such as open_basedir may prevent this last example from working..



Комментарии к посту «Display WordPress content ouside of your blog»

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

*

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.

DT

Курсы валют:

UAH

1 USD = 26.525 26.178
1 EUR = 29.940 29.240
1 RUR = 00.413 00.379

Антивирусы в формате SaaS
Подписаться письмом

Спонсоры...

SP:   LF:   PP:  

ubuntu == © Куликов М.М. (2011)
Сертифицированный партнер Dr.Web Free secondary DNS uaRating Rambler's Top100 Рейтинг@Mail.ru bigmir)net TOP 100 Яндекс цитирования Ranker Рейтинг сайта в веб-каталоге misto.zp.ua TOP.zp.ua