WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] have_posts() wrong result in archive.php (8 posts)

  1. surpriserom
    Member
    Posted 10 months ago #

    hi,
    i was programming my archive.php file for listing my custom post file,
    and i was using the casual loop

    if (have_posts()) :
        while (have_posts()) : the_post();
        my code here
    endwhile;
    endif;

    i have make only 1 post before editing my archive file,
    and the loop was showing nothing
    in fact have_post() was sending false
    and when i had added

    else:
      global $wp_query;
      echo '<pre>';
      var_dump($wp_query);
      echo '</pre>';
      echo $wp_query->post_count;
    endif;

    i've seen that $wp_query was sending back for the post count 1 and the function have_post was sending false,
    has i have 1 post the post count was the right one, and when i have added a second post, they where showing correctly, so my question is does the have_post() in archive.php need too have 2 post or is it the custom field wichmake it bug?

  2. esmi
    Forum Moderator
    Posted 10 months ago #

    does the have_post() in archive.php need too have 2 post

    No. Is your post published and publically available?

  3. surpriserom
    Member
    Posted 10 months ago #

    yes,it is, and when I unpublished one of the 2 article i have it's send me also a false, and show me nothing,
    well I will have multiple article too publish so it won't be a problem, but it surprised me and confused me a bit

  4. esmi
    Forum Moderator
    Posted 10 months ago #

    Based on the code snippet you posted above, there's no reason why that would not work for 1 post if it was the only Loop on the page.

  5. surpriserom
    Member
    Posted 10 months ago #

    i think it's because it is a custom post, because if i try with default post it work

  6. surpriserom
    Member
    Posted 10 months ago #

    here is the $arg of my custom post

    $arg = array(
       'labels' => array(
               'name' => __( 'Fiche &eacute;lus', AMF_IDMTXTDMN ),
               'singular_name' => __( 'Fiche &eacute;lu', AMF_IDMTXTDMN ),
               'add_new' => __('Ajouter une fiche d&apos;&eacute;lu', AMF_IDMTXTDMN ),
               'add_new_item' => __('Ajouter une fiche &eacute;lu', AMF_IDMTXTDMN ),
               'edit_item' => __('Editer une fiche &eacute;lu', AMF_IDMTXTDMN ),
               'new_item' => __('Nouvel fiche &eacute;lu', AMF_IDMTXTDMN ),
               'view_item' => __('Voir la fiche &eacute;lu', AMF_IDMTXTDMN ),
               'search_items' =>  __('Rechercher une fiche &eacute;lu', AMF_IDMTXTDMN ),
               'not_found' =>  __('Fiche &eacute;lu non trouvé', AMF_IDMTXTDMN ),
               'not_found_in_trash' => __('Fiche &eacute;lu non trouvé dans la corbeille', AMF_IDMTXTDMN ),
               'parent_item_colon' => ''),
       'rewrite'  => array( 'slug' => 'fiche-elu' ),
       'supports' => array('thumbnail', 'title')
    );
  7. esmi
    Forum Moderator
    Posted 10 months ago #

    If this is a custom post type, then you need to add that to your custom query using post_type.

  8. surpriserom
    Member
    Posted 10 months ago #

    if found in the archive.php, it was bundled in the theme that i have the function the_post(); who was called before calling the have_posts()
    the theme base is WPUtilities and i just see that he was calling the_post() in the beginning of some file :/
    thanks for help esmi

Reply

You must log in to post.

About this Topic