SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)

    An array to display entries that are in category A and B, A and C (WordPress)

    Hello,

    not sure whether this is the correct forum (as it's related to WordPress) but, given it's PHP, I hope it's acceptable.

    I know nothing about PHP and am trying to display one entry that belongs to category a (featured category) and b (other category), one entry belonging to category a and c, and so on.
    Categories b, c, d, e, f, etc. are not sub categories. They are all top level categories.

    I assume one would do that with an array?

    In my own primitive terms, here is what I mean:

    if entry is in the featured category (a) AND if entry is in category (b),
    then output entry,

    if entry is in the featured category (a) AND if entry is in category (c),
    then output entry,

    else, don't display anything.

    So, basically, I'm trying to get entries to display that are in category a and category b, in category a and category c.

    I haven't a clue how I'd go about translating this into a proper PHP function for WP.

    Any pointers are much appreciated.

    Thanks!
    Maleika E. A. | Rockatee | Twitter | Dribbble



  2. #2
    Serial Site Creator ToddW's Avatar
    Join Date
    Feb 2004
    Posts
    791
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you kind of confused yourself...

    It sounds to me like you want to list one entry (post?) that is in category A and also in category B.

    And then you also want to list one entry(post?) that is in category A and also in category C.

    What do you mean by so-on? Do you mean that you want to list one entry (post?) that is in category D and category E too, etc, etc??

  3. #3
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the confusion.

    What I want to do is display entries that are in category a AND category b, in category a AND category c, category a AND category d. The "so on" was meant to denote that I'd have entries that belong to categories a AND e, a AND f, a AND h, a AND z.

    Edited to add:

    If I'm not mistaking, then there are two conditions that need to be met. The first condition is that the entry is in category a. The second condition is that the entry must also be in another category (namely either b, c, d, e, f, g, ...), so something like:

    if category == a & category == b then output entry,
    if category == a& category == c then output entry,

    If either the first or the second isn't true, then just output nothing. Only if both conditions are met, the entry should be displayed.

    Thanks.
    Maleika E. A. | Rockatee | Twitter | Dribbble



  4. #4
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    I'm thinking this might be a better example with code. The issue that I'd like to display posts that are in category a as the main condition. When that condition is met, then the loop would come into play outputting the category b.

    In this example, the loop outputs category b:

    Code PHP:
                        <?php query_posts('cat=b&showposts=1'); ?>
                        <?php $posts = get_posts('category=b&numberposts=1&offset=0'); 
                        foreach ($posts as $post) : start_wp(); ?>
                        <?php the_title(); ?>
                        <?php the_excerpt(); ?>
                        <?php endforeach; ?>

    So (and I know this isn't correct but I hope it visualizes the idea), what I'm looking to do is for the loop to take action if it belongs to category a (featured category), and if the entry is in category a, then display the entry that is in category a and also in category b which would in turn allow me to display one featured entry of each category:

    Code PHP:
    <?php if (cat_id == a) {
                        <?php query_posts('cat=b&showposts=1'); ?>
                        <?php $posts = get_posts('category=b&numberposts=1&offset=0'); 
                        foreach ($posts as $post) : start_wp(); ?>
                        <?php the_title(); ?>
                        <?php the_excerpt(); ?>
                        <?php endforeach; ?>
    } else {
    // do something else
    }
    ?>

    Output example (where "featured" is category A), ergo displaying one featured entry for each category:



    Edit: I think this is a lot more difficult than I had imagined. After lots of searching, I found the Arthemia Theme which does exactly what I am trying to convey (see sidebar), so I think it's best if I purchase that theme.
    Last edited by kohoutek; May 2, 2009 at 18:31.
    Maleika E. A. | Rockatee | Twitter | Dribbble



  5. #5
    Serial Site Creator ToddW's Avatar
    Join Date
    Feb 2004
    Posts
    791
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kohoutek View Post
    I'm thinking this might be a better example with code. The issue that I'd like to display posts that are in category a as the main condition. When that condition is met, then the loop would come into play outputting the category b.

    In this example, the loop outputs category b:

    Code PHP:
                        <?php query_posts('cat=b&showposts=1'); ?>
                        <?php $posts = get_posts('category=b&numberposts=1&offset=0'); 
                        foreach ($posts as $post) : start_wp(); ?>
                        <?php the_title(); ?>
                        <?php the_excerpt(); ?>
                        <?php endforeach; ?>

    So (and I know this isn't correct but I hope it visualizes the idea), what I'm looking to do is for the loop to take action if it belongs to category a (featured category), and if the entry is in category a, then display the entry that is in category a and also in category b which would in turn allow me to display one featured entry of each category:

    Code PHP:
    <?php if (cat_id == a) {
                        <?php query_posts('cat=b&showposts=1'); ?>
                        <?php $posts = get_posts('category=b&numberposts=1&offset=0'); 
                        foreach ($posts as $post) : start_wp(); ?>
                        <?php the_title(); ?>
                        <?php the_excerpt(); ?>
                        <?php endforeach; ?>
    } else {
    // do something else
    }
    ?>

    Output example (where "featured" is category A), ergo displaying one featured entry for each category:



    Edit: I think this is a lot more difficult than I had imagined. After lots of searching, I found the Arthemia Theme which does exactly what I am trying to convey (see sidebar), so I think it's best if I purchase that theme.
    Gotcha.
    There are other themes out there that do this too

    You define a "Featured" category in the settings then can assign the posts to that one and the others, just like you want.

    Checkout the wordpress theme directory.

  6. #6
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    Thanks, ToddW. I'll check out the WP themes repository to see if there is one that has this functionality.

    It really irks me that I'm not getting a grasp on how this could work. I just know that the ingredients would be the get_posts (or query_posts), $cat_id, some SQL, and conditionals. How to put these together to make a working sample that'd output it the way I have described goes way beyond my head.

    As I've wanted to learn PHP and SQL anyway, I might just do it now. Then I may be able to do these things all by myself in a year from now.
    Maleika E. A. | Rockatee | Twitter | Dribbble




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
  •