WordPress.org

Ready to get started?Download WordPress

Forums

Setting PHP constants (3 posts)

  1. beanmg
    Member
    Posted 6 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!

    So

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

    Beano

  2. grosbouff
    Member
    Posted 5 years ago #

    Same problem here !

  3. Ian Dunn
    Member
    Posted 5 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.

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

Topic Closed

This topic has been closed to new replies.

About this Topic