• Resolved pmdci

    (@pmdci)


    Hi there,

    I am trying to make a custom page template, which for custom post types I created in WordPress 3.0 beta 2 install (it is a development environment).

    BACKGROUND
    POST TYPES: Besides the existing post type that comes with WordPress by default (simply called post), I have created a custom post type called partner.

    I use the default post type post for properties (eg: houses, condos, flats, etc). These are shown everywhere on the site, where the user can browse them based on:
    a) categories: (I used the default WordPress category called category that include terms such as Flat, House, Condo, etc)
    b) cities: I created a custom taxonomy called city that include terms such as Madrid, Rome, New York, etc).

    NOTE: The custom post type called partner doesn’t appear anywhere on the site since I used the following conditionals on my index.php (see this post – thanks a lot to user julia.lasarte for her help!):

    <?php
    //home
    if (is_home()) {
             query_posts($query_string . '&tag=featured&post_type=post&orderby=rand');
    }
    else {
    	 query_posts($query_string.'&post_type=post&orderby=rand');
    }
    ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    [...]

    NOW FOR WHAT I DID SO FAR (which didn’t work of course :-])

    1. I created a page called partners (eg: https://www.mysite.com/partners/) which I want to use to display the custom post type partner.

    2. I created a page-partners.php file for the theme and it load as expected. I tested it with some PHP gibberish (such as echo 'hello world' and all that) and I can see that the template loads fine when I visit my /partners/ page. So no doubts that the corrected page template is loaded by the /partners/ page.

    3. Bearing in mind that my page-partners.php is at this point an empty page, what I tried to do next is copy and paste the contents of my index.php. However it didn’t work (by design?). Instead of returning the posts as my index.php page normally does with the posts, it returned the code in the <?php else : ?> statement right after the loop, which is just a basic message, like:
    <div class="title"><h2>No matches have been found</h2></div>

    4. What I did next is to try to play with the query_posts() on my page template for the /partners/ page (page-partners.php). I first I tried the code: query_posts($query_string.'&post_type=partner'); but it didn’t work. No matter what I add, subtract or if I remove the query_posts() from code completly, the page-partners.php template never return me any posts.

    SO I AM STUCK CAN SOMEONE PLEASE HELP ME? ??
    As a test I changed the query_posts() in my index.php file and added the argument post_type=partner and I got posts of type partner. So I ruled out any problems with my custom post type.

    Also, bearing in mind that I copy and pasted the whole index.php file inside the /partners/ page-partners.php template file and it returned the no posts found message, I am pretty sure the way I am doing it isn’t right.

    Could someone please give me a hand?

    Thanks in advamce
    P.

Viewing 3 replies - 1 through 3 (of 3 total)
  • This should list your post_type partners:

    <?php
    $args=array(
      'post_type' => 'partners',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Partners';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        the_content();
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thread Starter pmdci

    (@pmdci)

    Hi there MichaelH!

    Thank you for your reply. I appreciate it. However, I did found another way to do it (can you believe, looking at some old page-blog.php template file I had in another WordPress site!

    I confess that the_loop is my Achilles heel concerning WordPress, and I am not a great PHP coder so I’d like to share with you guys what I did. Perhaps it is better, worse, or just another way? I did find one caveat concerning custom taxonomies using this method (see CAVEAT below).

    Well the code I had was a bit outdated so I had a search on Google and I found an updated version of this method.

    Basically, what I did is I made a copy of my index.php template file to another file called page-partners.php file to be used for the /partners/ template.

    Here is how my loop looks like:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php the_title(); ?>
    [...]
    [etc... you get the idea]

    NOTE: I removed the html tags and the arguments I used in the_content() for the sake of showing you folks a clean code. Anyway the idea is that index.php and page-partners.php will have different layout and css anyway, and the_loop is likely to be different as well.

    So what I did is to edit my page-partners.php file and I replaced the following code that was inherited from the index.php file, which comes just before my loop:

    <?php
    if (is_home()) {
             query_posts($query_string . '&tag=featured&post_type=post&orderby=rand');
    }
    else {
    	 query_posts($query_string.'&post_type=post&orderby=rand');
    }
    ?>

    And I replaced it with the following code:

    <?php
    $paged = get_query_var("paged");
    query_posts("&post_type=partner&paged=" . $paged);
    global $more;
    $more = 0;
    ?>

    Now this seems to be working perfectly (and without messing other parts of my site, which is always important). Well of course it doesn’t look pretty since (as I said before), these two post types (the default post and the custom partner) I am using have completly different content. So what I need to do now is to edit my page-parners.php template and change the layout and CSS codes, IDs and classes, etc.

    So what do you guys think of my approach? Is there any caveats you are aware of? Is it better, worse, or the same? I would be happy to hear your opinions.

    ONE CAVEAT FOUND (perhaps this is by design?)
    I did found one little nuisance. When I browse the categories of my site (eg: /flats/ or /houses/, I can use a query in the URL for my custom taxonomy called city and it works like a charm. But with my custom post type partner, it doesn’t work.

    For example, if I add to my URL: /house/?city=rome
    — It will return to me all the posts in the category house that only have the city Rome assigned to them (rome is a term of my custom taxonomy called city).

    I have also assigned the custom taxonomy city to my post of type partner. However, if I try the above example of using a query in the URL, it doesn’t work.

    For example, if I add to my URL: /partners/?city=rome
    — Instead of returning all the posts of type partner that only have the city Rome assigned to them, I still returns me all the partners regardless of their city.

    Any ideas of what I might be doing wrong?

    Thanks in advance,
    P.

    CREDITS
    I found the solution I used above on this site’s forum, which points credits to a forum post here in www.ads-software.com.

    Thread Starter pmdci

    (@pmdci)

    Hi MichaelH,

    Just a quick note. I did try your code as well besides the solution I found and I have seen no practical differences in the functionality between both.

    NOTE: I am quite a newbie with PHP/WordPress (max I used to do is some minor modifications on themes) so I do realise I might be just neophyte on the subject not to see the differences.

    But in the meantime I think it is fair to change this topic status to resolved since what I was initialy trying to achieve seems to be working.

    As for my issue with the queries in the URL, I moved the question to here.

    Thanks a lot for your help!

    Cheers,
    P.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom blog page for a custom post type’ is closed to new replies.