Forums

[resolved] Codex example not working (8 posts)

  1. tstruyf
    Member
    Posted 2 years ago #

    hi,

    I'm trying to select all posts from one category. I do this multiple times one a page. So the codex says I need to make a new object:

    (from: the codex)

    <?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php endwhile; ?>

    I made a category named special_cat en I tried to replace 'special_cat' with an existing category..., but the array always return empty!

    Anyone has the same problem?

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    What have you put in place of..

    <!-- Do special_cat stuff... -->

    If you havn't replaced that with anything then you won't get anything.. that's to be expected..

    If you're simpy giving an example above and you're saying the query is returning an empty result, does removing the category parameter then yield results?

    If it does, then i'd guess there's a problem with the name (category_name parameter is case sensitive iirc).

    Try a simple cat name like Test or test, then create 1 test post in that cat and give it a try in your query above.

    Process of elimination..

  3. tstruyf
    Member
    Posted 2 years ago #

    offcourse I replaced <!-- Do special_cat stuff... -->

    my exact code now:

    <?php $my_query = new WP_Query('showposts=1'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p>hello world</p>
    <?php endwhile; ?>

    The output is 3 times "hello world". I have a total of 7 post.
    Why 3? To me it's a total mystery!

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    It should be 1, since you have showposts=1 ...

    You shouldn't be placing this inside an existing loop, else you'll get this result for an equal amount of times to the current loop..

    If the existing loop has 3 matches, and runs the above code each time, you'll get hello world that amount of times.

  5. tstruyf
    Member
    Posted 2 years ago #

    no, here my total page code

    <?php
    /**
     * Template: Index.php
    */
    
    get_header();
    ?>
    
    <div class="inhoudcontainer"> 				
    
    			<?php $my_query = new WP_Query('category_name=news'); ?>
    
    			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    			  <?php the_title()?>
    			  <br /><br />
    			<?php endwhile; ?>
    
    </div> <!-- END INHOUDCONTAINER-->
    
    <?php get_footer(); ?>

    I have a category calles news

  6. Mark / t31os
    Moderator
    Posted 2 years ago #

    Are you using this as a template file?

    If this a template file, and the file is named index.php, and also resides in your theme folder, you'll end up with a conflict, since index.php is a reserved name for the root file of the theme.

    Do you need this file for attaching to pages? because that's what "Page Templates" are for, which is what the top most "Template:" does, to denote that it is a Page Template file.

    If you're attaching this to a page, no problem there, i just think if you're naming it index.php the file will be having a conflict of interest.

    Template names also shouldn't read like this.

    * Template: Index.php

    but like so...

    * Template: filename

    without the extension (although the file must be a PHP file).

  7. tstruyf
    Member
    Posted 2 years ago #

    I copied my theme to another Worpress 2.8.4 installation and it worked fine!

    So I installed wordpress allover on my localhost and the code works !!

    still, many thanks for your help !

  8. Mark / t31os
    Moderator
    Posted 2 years ago #

    :) Good stuff...

    Mark resolved if all is ok (just to the right of the post).. ---->

Topic Closed

This topic has been closed to new replies.

About this Topic