SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 30
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    WordPress: Call a certain category

    Hello!

    I am using wordpress 2.7.0 and I have a question.

    I am trying to make a table that calls information from a certain category.

    For example, I have this mix of php and html that almost does what I want.


    (I know this code does not generate a full table. The code I have below does not account for the titles of the columns)



    PHP Code:
    <tr><td align=left><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></td><td><center><a href="<?php the_permalink() ?>" rel="nofollow" title="Permanent Link to <?php the_title(); ?>"><?php the_time('F - j - Y'); ?></a></center></td><td><center><?php the_views() ?></center></td></tr>

    The category it calls information from is the category of the page where the code is. I do not want it to call the category of the page that I am on. I want to be able to specify a particular category. If possible, I would like to specify it by the categories slug.


    Does anyone know what I can do to make it so I can specify the category?


    Thanks in advance.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If these will be the only posts you display anywhere on the page, then you can use query_posts to change which posts are retrieved:

    http://codex.wordpress.org/Template_Tags/query_posts

    Otherwise, you need to create another loop yourself, using get_posts

    http://codex.wordpress.org/Template_Tags/get_posts

    There are usage examples at both pages above that include how to retrieve by category

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks again for your fast help!


    Im not entirely sure which one I need. I think the second link is the one I should go with.


    Purhaps some more info may help.


    I have about 200 posts that each have a short summary on them. All of these posts are in 1 category (Summary).

    I have installed a plugin that allows php to run within a post. So now I am trying to use the table that I pasted in my previous post below the summary on any given summary page. Each summary page would have a different category listed in the table, so I can not simply put generic code in my single-post.php.



    With this new info, which direction would be the best way to proceed.

    I am not overly familar with php so any extra guidance is greatly appreciated.

    -Thanks again.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you're putting this code into individual posts, then the second one is the only one you can use. It will let you retrieve all the posts in the summary category, and you can put your table code within the loop you create.

    PHP Code:
    <?php

    global $post;
    $myposts get_posts('category=ID_OF_SUMMARY_CATEGORY');
    foreach(
    $myposts as $post) :

    ?>

    Your Table Code Here

    <?php endforeach; ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks! I will give this a try now and let you know how it worked out!

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    I gave what you said a try and nothing shows up.

    I think I may have the category ID number incorrectly entered.




    PHP Code:
    <?phpglobal $post;
    $myposts get_posts('422');
    foreach(
    $myposts as $post) :?>
    <center><table class="catlisting" width="80%"><tr>
        <th style="width:60%">Title</th>
        <th style="width:25%">Added</th>
        <th style="width:15%">Views</th>
      </tr>
    <tr><td align=left><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></td><td><center><a href="<?php the_permalink() ?>" rel="nofollow" title="Permanent Link to <?php the_title(); ?>"><?php the_time('F - j - Y'); ?></a></center></td><td><center><?php the_views() ?></center></td></tr>

    </table></center>
    <?php endforeach; ?>

    I have also tried

    PHP Code:
    $myposts get_posts('category=ID_422'); 
    PHP Code:
    $myposts get_posts(422); 
    PHP Code:
    $myposts get_posts('category=422'); 
    and none of them seem to work. Could you specify what it should be?


    Thanks alot for your help.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The last one is correct assuming 422 is correct. It was my mistake in leaving something out in my example. The number of posts to retrieve is a required argument. So it would be:

    $myposts = get_posts('category=422&numberposts=5');

    That would get 5 posts from category 422. You can retrieve all the posts by using -1 as the number of posts.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    I just gave
    PHP Code:
    <?phpglobal $post;
    $myposts get_posts('category=422&numberposts=5');
    foreach(
    $myposts as $post) :?>
    <center><table class="catlisting" width="80%"><tr>
        <th style="width:60%">Title</th>
        <th style="width:25%">Added</th>
        <th style="width:15%">Views</th>
      </tr>
    <tr><td align=left><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></td><td><center><a href="<?php the_permalink() ?>" rel="nofollow" title="Permanent Link to <?php the_title(); ?>"><?php the_time('F - j - Y'); ?></a></center></td><td><center><?php the_views() ?></center></td></tr>

    </table></center>
    <?php endforeach; ?>
    A try and it doesnt seem to have fixed the problem. I wanted to rule out the possibility of your code having an issue with the post php plugin I found, so I tried your code on my single.php file. It caused the page to load/not load (depending on how you look at it) a blank white page.


    Any idea what could be messing it up?


    Thanks again.

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    global $post should be on its own line, not mashed into the <?php

    Also, do you have a plugin installed which is providing this "the_views" function? As it's not part of WordPress.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alas! How could I have missed that?!?

    It works great now [=.


    Im quite satisfied with how it works now, but out of curiosity, is it possible to replace the cat id number with the slug of the cat?


    Thanks again!

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it is. I'm tempted to sound like a professor and say "and I leave it to you as an exercise to find out how". I did already give you links to the pages with the answer.

    You go to the link for get_posts(), and scroll down to the "Parameters" section. This tells you it takes the same parameters as query_posts() and links you there (which is another link I already shared). You click the link and right on screen is "Category Parameters" which tells you you can use category_name instead of category to specify the category by name.

  12. #12
    Is Still Alive silver trophybronze trophy RetroNetro's Avatar
    Join Date
    Jul 2003
    Location
    Texas
    Posts
    6,883
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  13. #13
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan: Sorry for the slow reply. I was out to dinner :/ .

    You have been a tremendous help!


    BPartch: Thanks!


    Thanks again to you both.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan or anyone else:

    I have run into a problem that I didnt expect to by specifying a category. Is it possible to alter this code so that it looks at a custom field instead of directly at a category slug? For example, if I wanted it to look at a custom feild named info. It would then take which ever value was in the info custom field and then use it as the category slug to generate the list.


    Thanks again!

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A custom field on the post you're embedding the code in?

    PHP Code:
    <?php
    global $post;

    $slug get_post_meta(get_the_ID(), "info"true);

    $myposts get_posts('category_name=' $slug '&numberposts=5'); 

    ...
    Or a custom field on the posts you want to retrieve?

  16. #16
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    You are a huge help!

    The custom field is on the post that I am embedding this code on. The info in the custom field is the slug of the category I wish to display.


    For example, this code is on a post called "My List". "My List" has custom field info=books. The list should show all entries from the category with books as its slug.


    Hopefully that makes sense.

    Thanks again.

  17. #17
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's the code I just gave then

  18. #18
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I will give that a try now.


    On another note, I just noticed a problem with the original code.

    It seems to call the views from the post that you are viewing and not the specified category.


    I am currently using the WP-PostViews plugin from http://wordpress.org/extend/plugins/wp-postviews to find the views. Do you happen to know how I can pass the variables so that it displays the correct views?


    if not, I will try my luck at the support forum for the plugin [=

    Thanks alot.

  19. #19
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    I tried your new code and I can not seem to get it to work.

    This is what I have:

    PHP Code:
    <?php
    global $post;
    $slug get_post_meta(get_the_ID(), "info"true);
    $myposts get_posts('category_name=' $slug '&numberposts=5');
    <
    tr><td align=left><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></td><td><center><a href="<?php the_permalink() ?>" rel="nofollow" title="Permanent Link to <?php the_title(); ?>"><?php the_time('F - j - Y'); ?></a></center></td><td><center><?php the_views() ?></center></td></tr> 
    <?php endforeach; ?>

    I have a custom field named info with the data books also.


    Any ideas what could be wrong?


    Thanks again for all of your help!

    -edit-
    ****When I said I couldnt get it to work I mean that I get the white page error again.*****

  20. #20
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You've failed to start a loop over the posts you retrieved, and leave PHP mode ( ?> ) before writing in HTML. The "..." in my code meant there's more, which is identical to the code you were using before, below that point.

  21. #21
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh I see!

    Thanks Dan, it works again!


    Do you happen to know a simple way to pass the variables to the view counter? Its perfectly fine if you dont know - I realize you do not directly support random plugins. I just thought Id ask since you seem to be extremely talented at this type of thing.



    Thanks again for your patience with me!

  22. #22
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    I looked into the problem with the plugin and I was given this lead.

    "The code you are using is wrong. foreach() is already depreciated. Refer to http://codex.wordpress.org/Template_Tags/query_posts"


    I have spent alot of time trying to figure out exactly what replaces foreach, but im having no luck. Do you happen to know?


    Thanks again!

  23. #23
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan or anyone else,


    Ignoring the issue with the plugin, I have found another conflict with the code.



    Currently I have this code below the code Dan provided me with:

    PHP Code:
    <?php if( in_category(185) ) : ?>

    <?php else : ?>
    code here
    <?php endif; ?>
    The problem is it always does what is in the else and not what is in category 185 even though the page is category 185. Whats strange is if I put Dan's code below my other code, my other code works.


    Any ideas as to how I should fix it?

    I suspect the foreach() is part of the problem, but I have yet to find a way to replace it.


    Thanks in advance.

  24. #24
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    in_category() is looking at $post, which you've overwritten in the loop, so it no longer references the page but whatever the last post to be retrieved in your get_posts() was.

    Keep your code up top, and use it to set some variable which you can use later where the display will actually take place

  25. #25
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks for the reply.

    Im not exactly sure how to call the variable at a later time. Would you happen to know of a guide to it or maybe have an example that I can try out?


    Thanks again. [=


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •