hello, all:

I am trying to make it so I can show a banner according to a blog articles's category(ries). per example if a posting (or postings) appears under category 101, then show banner1, if it appears on category 102, show banner2, if category 103 show banner3 and so forth. Problem is a post can belong to several categories, along 2 separate tables, in one-to-many relationship. I want the script to 'see" that a post appears in any of these specific categories and show its appropriate banner. This is a Wordpress blog setup.

After I created a mysql script to bring all records into my webpage, I then created a another if-else statement to call up each banner accordingly, something like "if category xxx appears show banner1, else if category zzz appears then show banner2, else if category yyy appears then show banner3, else show ALL banners.

So, right now, the problem is, if an article is tagged with ONLY that specific category, it works fine, it shows proper banner, BUT if an article is tagged across several categories, INCLUDING the one specific one (like belonging to categories 101, 12, 6, 200, etc), then it show's all banners, I figure cause it's obviously reading all other categories, and therefore shows all banners. But it should show ONLY the one banner associated with the specific category in question (in this case 101).

Here are the 2 related tables which have the common post id field (see example entries, and there are many more fields, but I summarized to make easier)

TABLE 1:   wp_posts  (posts table)


ID       post_title                 post_content            post_date     etc...
66     The Dog barks
67     The Cat Meows
68     The Duck Quacks
69     The Crocodile



TABLE 2: wp_post2cat  (Categories table)


post_id    category_id
66             101
66             565
66             756
67             101
68             130

SoI created a mysql-recordset to pull in articles based on a post's ID url value (articleBannersRS), and I added the code below where my banners are supposed to show up:

<CODE>


<?


if ($row_articleBannersRS == '101') {include('inc_banner1.php'); }


elseif ($row_articleBannersRS == '102') {include('inc_banner2.php'); }


elseif ($row_articleBannersRS == '103') {include('inc_banner3.php'); }


else {


include('inc_banner1.php');
include('inc_banner2.php');
include('inc_banner3.php');


}


?>


</CODE>

As I said, posts with single categories (like posts 67 & 68) would show proper banner, but in the case of post # 66, that belongs to several categories, it would do the final "else" statement and show "ALL banners" when I want it to only show the banner that corresponds to category 101.

Maybe I am not joining or doing my msql statement properly? this is my mysql statement:

SELECT *
FROM wp_posts, wp_post2cat
WHERE wp_posts.ID = wp_post2cat.post_id AND wp_posts.ID = (here would be post-id value-pair url string)

Well, this makes sense...

Thanks in advance!

Never mind, I was thinking something else. I'll repost in a sec.

SELECT *
FROM wp_posts, wp_post2cat
WHERE wp_posts.ID = wp_post2cat.post_id AND wp_posts.ID = (here would be post-id value-pair url string)

Why can't you just do:

SELECT *
FROM wp_posts, wp_post2cat
WHERE wp_posts.ID = wp_post2cat.post_id AND wp_posts.ID = (here would be post-id value-pair url string) and wp_post2cat.category_id = $categoryInQuestion

Do you not know which category is in question?

Thanks Rob:

hmm... yes, I know what specific categories I want to have linked to specific banners. Let me try your code and see what happens or how far i can take it...

I guess I would have to have a recordset for every banner-to-category match i wanna do, ha?? OK, let me play with it, let you know how it goes...

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.