• I have a page (article.php) that is outside the loop, the theme, in fact it’s sitting on the root. I am trying to pass to it some variables (post_title and perhaps ID) from my last 3 posts with the “News” category. Another page reads from article.php. Here is an example of what I need to print to this page…….

    <?php
    print "{articles : [
    	{id: 1, title : \"Post Title 1\", picture : \"large/1.jpg\", thumb : \"small/1.jpg\"},
    	{id : 2, title : \"Post Title 2\", picture : \"large/2.jpg\", thumb : \"small/2.jpg\"},
    	{id : 3, title : \"Post Title 3\", picture : \"large/3.jpg\", thumb : \"small/3.jpg\"}
    ]}";
    ?>

    Here is the query and script I have been trying to run to get it to output the above….

    <?php
    include_once('wp-config.php');
    include_once('wp-includes/wp-db.php');
    $query = $wpdb->get_results("SELECT * FROM $wpdb->wp_posts, $wpdb->wp_post2cat WHERE ID = post_id AND category_id = 7");
    print "{articles : [";
    $num=1;
    while ($results = mysql_fetch_object($query)) {
    print "{id : ".$results->ID.", title : \"".$results->post_title."\", picture : \"large/".$num.".jpg\", thumb : \"small/".$num.".jpg\"}";
    $num++;
    }
    print "]}";
    ?>

    Hoping someone can help me figure this out. Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I am a code illiterate, hence I’d go in this way:
    Adding this at the top of your article.php

    <?php
    require('./path-to-your-blog/wp-blog-header.php');
    ?>

    and then I could use any WP function/template tag in the file; so a mini-loop (see The_Loop with query_post (cat X showpost 3) would do it.

    just my $0.02

    EDIT. Oh, and because even I can do it, I moved it from advanced to simple how-to 🙂

    Thread Starter bgajus

    (@bgajus)

    Thanks! It took some time for me to figure out, mainly because I dont know all the WP function/template tags. I got it working though using the method you recommended….Thanks for the quick response moshu! =)

    Not sure exactly what you want to accomplish here, but you can totally customise WordPress by creating your own template http://codex.wordpress.org/Templates then make a new Page (not Post) and assigning the template to it.

    There are lots of functions/tags you can use:
    http://codex.wordpress.org/Template_Tags

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need Help with Custom Query’ is closed to new replies.