SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Sep 2009
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $args and Queries and template parts

    I have a wordpress template part called "grid-loop.php" that has a query that shows a grid of items. Initially, in this template part, I pass $args to it. It works fine. I'm trying to set it up differently now. Let me explain.

    I now have a page template (page-news.php) that contains the args:

    PHP Code:
    <?php
        $args 
    = array(
        
    'category_name' => 'news',
    ); 
    ?>
    Beneath that I have this:

    PHP Code:
    <?php get_template_part'grid''loop' ); ?>

    The grid-loop contains this:

    PHP Code:
        $the_query = new WP_Query$args ); 
    So basically the $args is in a separate document from the actual WP_Query. It doesn't work. Maybe it can't work this way or I'm doing something wrong? When I put the arguments with the query in the same document, it works fine. Suggestions?

  2. #2
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Looking at http://codex.wordpress.org/Function_..._template_part, and your code below:
    PHP Code:
    <?php get_template_part'grid''loop' ); ?>
    it is looking for a file called grid-loop.php. However your file is called page-news.php, so this is probably your issue.
    Ryan B | My Blog | Twitter

  3. #3
    SitePoint Addict
    Join Date
    Sep 2009
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    Looking at http://codex.wordpress.org/Function_..._template_part, and your code below:
    PHP Code:
    <?php get_template_part'grid''loop' ); ?>
    it is looking for a file called grid-loop.php. However your file is called page-news.php, so this is probably your issue.
    Actually page-news.php loads the template part grid-loop.php

    Page-News.php
    PHP Code:
    <?php get_header(); ?>
    <!-- News Page -->
    <section class="grid">
        <!-- Grid -->
        <?php get_template_part'grid''loop' ); ?>

    </section>

    <?php get_footer(); ?>
    My question is about whether I can setup the variable "$args" within page-news.php but have grid-loop.php load the variable. Does that make sense?

  4. #4
    SitePoint Addict
    Join Date
    Sep 2009
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For example do I need something like Post and Get? Of which I don't entirely know how to use yet.

  5. #5
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rpeg View Post
    My question is about whether I can setup the variable "$args" within page-news.php but have grid-loop.php load the variable. Does that make sense?
    Take a look at the page I linked to. If your two files are in the same directory, then it seems like it will get it fine.
    Ryan B | My Blog | Twitter

  6. #6
    SitePoint Addict
    Join Date
    Sep 2009
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    Take a look at the page I linked to. If your two files are in the same directory, then it seems like it will get it fine.
    Well, I haven't had problems using the template parts before. This particular task is not working and I suppose I haven't got a clue. Strange.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •