• Resolved Darkmirror

    (@darkmirror)


    I am using query post in my template. I don’t want to create like multiple template pages for certain categories. Instead, i want to use the number i put in the post as a category id to show the posts.

    How can i fetch the number from the post and implement it into my template?

    Many thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • This is one way to do what you want.

    • Use a Custom Field (named category-to-show, or similar) to hold the category id number.
    • Write a template to get the Custom Field and use the category number in a call to query_posts().
    • Create a Page for each category, put the category id in the custom field and assign the custom template.
    Thread Starter Darkmirror

    (@darkmirror)

    How can i get the value from the custom field in the template. Is there any specific code that i need to implement?

    You use the get_post_meta() function. Your code would look something like this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $cat_id = get_post_meta($post->ID,'category-to-show',true);
    if (intval($cat_id)) :
       query_posts("cat=$cat_id&paged=$paged");
       if (have_posts()) : while (have_posts()) : the_post();
          // your code to display posts
          endwhile;
       else :
          // code for no posts in category
       endif;
    else :
       // code for no category in custom field
    endif;
    Thread Starter Darkmirror

    (@darkmirror)

    Thank you very much!… I’ve been waiting for someone to help me..^.^ I am a high school student and a noob to php and wordpress haha… Coding is very fun!

    Good luck with it!!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Query_post Question’ is closed to new replies.