8

For example:

<?php $numposts = $_POST['showposts']; ?>


<?php $home_query_bottom = new WP_Query("cat=&showposts=$num_posts&offset=5"); $b = 0; ?>
<ul class="thumbs">
    <?php while ($home_query_bottom->have_posts()) : $home_query_bottom->the_post();
        $do_not_duplicate = $post->ID; $b++; ?>

        <li class="post-<?php the_ID(); ?> thumb"><?php get_the_image( array( 'custom_key' => array( 'thumbnail' ), 'default_size' => 'thumbnail', 'width' => '160', 'height' => '160' ) ); ?></li>
    <?php endwhile; wp_reset_query(); $b = 0; ?>
</ul>

The code above is on its own php file ready to be called by the main wordpress page, however I get an error saying wp_query class not found. I'm assuming it's because I am not using the header.html which probably has a bunch of includes. What do I need for that page to utilize the wp_query class?

4 Answers 4

7

You can turn the template engine off and then include the header.
// Include WordPress
define('WP_USE_THEMES', false);
require_once('PATHHERE/wp-blog-header.php');

7
  • include my header? But it's a ajax call that I want to load into my homepage
    – Adam
    Jan 9, 2011 at 20:48
  • Thats why you turn off themes. header will load all the functions you need, but since themes are turned off it wont actually output the header content.
    – MrGlass
    Jan 9, 2011 at 21:00
  • ah ok. Well i think my host disabled require_once and includes because I get URL file-access is disabled errors. Which makes me wonder how is wordpress including their essential classes etc...
    – Adam
    Jan 9, 2011 at 21:03
  • I don't think wordpress would be able to run at all if requireonce/include were somehow disabled. Perhaps you are pointing it to a directory you don't have access to?
    – MrGlass
    Jan 9, 2011 at 21:07
  • ok yes it doesn't like the absolute url paths include('mydoamin.com/wp-content/themes/fullscreen/header.php');
    – Adam
    Jan 9, 2011 at 21:13
7

You have to include the file that has the Wordpress functions located on the main directory of the Wordpress Installation:

define('WP_USE_THEMES', false);  
require_once('../../../wp-load.php');

../../../ = path to the main directory on your installation

I found a nice tutorial about this here.

0
4

You have to make the page that ajax calls a template page. Make a new page in your blog and assign the template. no headers no includes.

2

This worked for me, maybe it will help someone else.

My situation is I have a js file that uses getScript. In that script i have a bunch of .load() calls to a php file. At the top I place this.

if (file_exists("../../../wp-load.php"))
    {
    require_once("../../../wp-load.php");
    }

Change the ../ to how ever many directories your wp-load file is up.

Edit - Using WP 3.4.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.