WordPress.org

Ready to get started?Download WordPress

Forums

use of query_post in template, please help (4 posts)

  1. elevensuns
    Member
    Posted 9 months ago #

    Hi there, i'm not a developer and just stumbled into a php issue i cannot resolve.

    I have a template using the following php code to query posts on a specific category and show them in a table.


    <?php query_posts(array( "cat" => 80, "orderby" => "date", "order" => "DESC", "posts_per_page" => -1 )); ?>

    <?php $count_posts = $wp_query->post_count; ?>

    <?php while (have_posts()) : the_post(); ?>

    <!-- Getting Attachments -->
    <?php
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_mime_type' => 'application/pdf',
    'post_parent' => get_the_ID()
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $attachment) {
    $attachment_url = wp_get_attachment_url($attachment->ID);
    }
    }
    ?>

    So far it shows the posts under a category number that appears on the array.

    I would like to modify this code in order to show the posts under any category that it's being browsed, not a specific one.

    This way i will be able to use it in category.php so all posts under any categories will be showed that way.

    Is ther any body that can help me with this code? thanks in advance!

    Francis

  2. TheHandOfCod
    Member
    Posted 9 months ago #

    From looking at the documentation try taking "cat" => 80 out altogether. Bear in mind that using query_posts is not recommended as it has some side effects. You might want to consider using WP_Query instead which looks like it will have less side effects and wil be more maintainable in the future.

    http://codex.wordpress.org/Function_Reference/query_posts (see the section title Caveats)

    http://codex.wordpress.org/Class_Reference/WP_Query

  3. elevensuns
    Member
    Posted 9 months ago #

    Thanks HOC, i solved it,

    I used:

    $cat_id = get_query_var('cat');

    and then

    query_posts(array( "cat" => $cat_id,

    About query_post vs Wp_Query, I get an error, I also tried get_post, but i haven't solved a post_per_page issue i have. -1 shows all posts without pagination, this result is desired, but if i use get_post i get default pagination despite the value i enter, i'm no a coder obviously, so it's a challenge for me, but it seems easy, i'm trying to brake it, thanks for your help

  4. TheHandOfCod
    Member
    Posted 9 months ago #

    pleasure, glad you fixed it and thanks for posting how. It will help others with the same problem.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.