Need some quick php coding help...

6 replies
Almost 6,000 posts, and this is my first visit to this neck of the woods...

I came across an article with some code that lloks like it will solve a problem for me here:

http://www.willmaster.com/library/we...ublication.php

What I want to do is add a bit of code to the header section of a Wordpress theme depending on the category. Some of the categories would have a default code to be used unless specified in the category (subdirectory) - see the article example.

I know just enough that I could probably hack my way through this, given enough time. I was hoping that one of you more advanced types could take a quick peek and tell me what I need to do in a few lines.

Whether you do or don't, thanks for taking the time to check this out...
#coding #php #quick
  • I'm a little confused, because the question seems too easy: are you looking to determine if a post/page/custom is in a given category? If so check this out.

    If not, can you be more specific?
    {{ DiscussionBoard.errors[4128146].message }}
  • Profile picture of the author SteveJohnson
    John, there are a couple of built-in WordPress functions for testing categories. Which you use depends on what you're doing. If you're testing a single post page to see what category the post is in, you'd use in_category(). If you're testing an archive page (a page with a group of posts from a certain category) you'd use is_category().

    This tests to see if the displayed page is an archive page for the category 'widget':
    PHP Code:
    if ( is_category('widget') ) :
      do 
    something here
    endif; 
    This tests a single post page to see if the post is in the 'widget' category:
    PHP Code:
    if ( in_category('widget') ) :
      do 
    something
    endif; 
    Is that enough to get you going?
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[4130583].message }}
  • Profile picture of the author instantpaypalcash
    Jeff,

    I just checked out that post and the related one with it and this certainly seems like it should fit the bill for what he is trying to do.

    I am even going to use it myself.

    Thanks for this great link.
    {{ DiscussionBoard.errors[4130899].message }}
    • Profile picture of the author JohnMcCabe
      Thanks for the head start, guys.

      Here's what I'm trying to accomplish...

      I'm trying a twist on an affiliate scheme, where affiliates can send people to a blog post or other page, rather than straight to a sales letter. They'll code their affiliate ID into the link. Scripting on the page will insert their affiliate ID into any links on the page. If their is no affiliate ID in the referrer, then a default ID is used.

      The complication comes in when I bring in a partner to run a particular section, or category, of the site. Part of the package will include adding their affiliate ID to posts in their category in place of my default ID, while still using the default in other categories.

      Further complicating things, there may be pages outside the blog structure altogether, which is why I latched onto the original linked solution.

      So, depending on the folder (category), there would be three options for the affiliate ID:

      > The referring affiliate's ID
      > The category manager's ID if there is no affiliate ID
      > The default ID if there is no affiliate ID or manager ID

      I want to put code in the header to read the directory/category and insert the right ID into links on the page.

      Am I over thinking this?
      {{ DiscussionBoard.errors[4131089].message }}
  • Profile picture of the author JohnMcCabe
    Just a quick update, and another thank you to all who replied...

    In thinking about things, I was way over-thinking things.

    I can accomplish what I want to do by taking advantage of the multi-user ability in WP and using child themes to set up the code for the opt-in forms and pages. I found a plug-in, through Paul Myers' BEAST course, to set the default values.

    Jeff, that script you linked to will come in handy for another aspect of the project as it moves along - 'ethical bribes' tailored to the category content.

    You guys got my brain working in another direction and saved me several hours of frustration trying to hack code that wasn't suited to what I needed to do.

    Thanks again...
    {{ DiscussionBoard.errors[4137002].message }}

Trending Topics