WordPress.org

Ready to get started?Download WordPress

Forums

get_cat_id and compare to cat_id in sql table (5 posts)

  1. artinh82
    Member
    Posted 5 years ago #

    Hello,
    I'm not as skilled with PHP and MySQL as some of you in this forum. So I would really appreciate if somebody can help me with the following.

    Using PHP, what is the best way of getting the cat_id for each post, then comparing it to a value in a MySQL table. And if both values are equal then get the content of that value in that table and echo it on my page.

    Here is an example of what i mean:
    I have 3 categories
    1. Cars (cat_id is 10)
    2. Bikes (cat_id is 11)
    3. Planes (cat_id is 12)

    I have created a new table and named it "info" which contains the following:
    id -- cat_id -- text
    1 -- 10 -- some text for every post in the cars category
    2 -- 11 -- some text for every post in the bikes category
    3 -- 12 -- some text for every post in the planes category

    Now lets say I have published a post in the "Cars" category (which is cat_id 10)
    Within the WP loop i want to get the cat_id for the post and compare it to the cat_id in the "info" table, then echo the appropriate text for each category/cat_id.

    Thank you in advance.

    Artin H.

  2. iridiax
    Member
    Posted 5 years ago #

  3. artinh82
    Member
    Posted 5 years ago #

    Hi iridiax,
    Thank you for your reply.
    That is actually very similar to what I'm looking for. I tried it and it seemed to work in most cases.

    But I also forgot to mention, that some of my posts are published in two categories.

    i.e.:
    Post1 is published in "Cars" & "BMW"
    Post2 is published is "Bikes" & "Yamaha"

    I tried the following

    <?php 
    
    if ( in_category( 'cars' ) &amp;&amp; ('bmw')) {
    	echo "Something about Cars and BMW";
    } else {
    	echo "Something else";
    }
    ?>

    That doesn't work. Actually the code it self does work, it just does seem to recognize the category, therefor it echos "Something else".

    Any suggestions.

    Thank you.
    Artin

  4. artinh82
    Member
    Posted 4 years ago #

    I'm still need some help with the above. Thank you

  5. Aldi
    Member
    Posted 4 years ago #

    Try:

    <?php 
    
    if ( in_category( 'cars' ) && in_category('bmw')) {
    	echo "Something about Cars and BMW";
    } else {
    	echo "Something else";
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic