Setting PHP constants (3 posts)

  1. beanmg
    Posted 8 years ago #

    I am creating a new theme for use on two or three sites.

    My code needs to be able to exclude/include posts with specific Categories which can simply be done if you know the Cat-id.

    Of course, Categories are numbered at set-up time and as I believe I cannot set a Category to a particular Cat-id I have decided to set global constants in one place and set a group of names to the corresponding Cat-id and use the constant throughout the theme code.

    At present I am doing this with define() in index.php at the wordpress root to be sure that the constants are available throughout the theme but I would rather set them inside my theme and out of danger of WordPress updates!


    a) am I right about Category-ids
    b) is there some place where my define() will be executed every time?


  2. grosbouff
    Posted 7 years ago #

    Same problem here !

  3. Ian Dunn
    Posted 7 years ago #

    You could set them in your theme by putting them in your header.php file, or in any php file that was included by the header.

    But... It'd probably be better to just use query_posts() if you're inside The Loop, or WP_Query() if you're outside of it.

    Something like:

    $music = query_posts('category_name=music');
    $notMusic = query_posts('cat=-5'); (if the id for the music category was 5)

    I'm there if you look through the Codex you can also find some existing functions to give you a category id when you pass it the category slug.


Topic Closed

This topic has been closed to new replies.

About this Topic