1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[HELP] Modifying WP Plugin

Discussion in 'Black Hat SEO' started by Andi49, Jun 20, 2010.

  1. Andi49

    Andi49 Newbie

    Joined:
    Apr 26, 2009
    Messages:
    26
    Likes Received:
    1
    Hi,

    I have a WP-Plugin with the following code:

    PHP:
    $mypost wp_get_single_post($post_IDARRAY_A);
    $mytitle $mypost['post_title'];

    Now I want, that $mytitle also includes the category, where the post was made in.
    So for example if I had a post about biking with the title "Biking is fun!" in the category "biking" I would like the title to be:"Biking is fun! - biking"

    I tried changing this to:
    PHP:
    $mytitle $mypost['post_title'] . "-" get_the_category($post_ID);
    but it does not work.

    Can anybody give me a hint on how to add the category to $mytitle?

    Thank you guys :)
     
  2. plasmaflux

    plasmaflux Registered Member

    Joined:
    Feb 20, 2010
    Messages:
    91
    Likes Received:
    37
    Sometimes get_the_category() returns an array of all categories related to the post_ID. I haven't played with it, but it's possible that it's returning an array with a single element, which you can't just spit right out.

    Your best bet is to throw some debugging code in their so you can see the actual value of get_the_category. If it is, in fact, an array, you'll need to do some sort of additional processing to either flatten the array or display the first value.

    Try pasting this right under your $mypost/$mytitle lines of code:

    PHP:
    global $post;
    $categories get_the_category($post->ID);

    echo 
    "<pre>";
    var_dump($categories);
    echo 
    "</pre>";
    exit();

    Depending on where in the loop this code is called, you'll either get a white screen with a raw dump of the categories associated with that post ID, or you may have to check in the page source and search for the
    HTML:
    <pre>
    tags.

    Noteworthy is that the above code uses $post->ID rather than $post_ID. Maybe that has something to do with it?

    For more info, check out the WP Codex page on get_the_category():

    Code:
    http://codex.wordpress.org/Function_Reference/get_the_category
     
  3. Andi49

    Andi49 Newbie

    Joined:
    Apr 26, 2009
    Messages:
    26
    Likes Received:
    1
    thx plasmaflux.

    You were right I get an array of an object that looks like this:

    Code:
    array(1) {
      [0]=>
      object(stdClass)#318 (15) {
        ["term_id"]=>
        &string(1) "1"
        ["name"]=>
        &string(13) "Uncategorized"
        ["slug"]=>
        &string(13) "uncategorized"
        ["term_group"]=>
        string(1) "0"
        ["term_taxonomy_id"]=>
        string(1) "1"
        ["taxonomy"]=>
        string(8) "category"
        ["description"]=>
        &string(0) ""
        ["parent"]=>
        &string(1) "0"
        ["count"]=>
        &string(2) "56"
        ["cat_ID"]=>
        &string(1) "1"
        ["category_count"]=>
        &string(2) "56"
        ["category_description"]=>
        &string(0) ""
        ["cat_name"]=>
        &string(13) "Uncategorized"
        ["category_nicename"]=>
        &string(13) "uncategorized"
        ["category_parent"]=>
        &string(1) "0"
      }
    }
    
    Well now I need to know how to access the
    ["name"] of this object and it should work I guess.

    Do you know how to do this?
     
  4. Andi49

    Andi49 Newbie

    Joined:
    Apr 26, 2009
    Messages:
    26
    Likes Received:
    1
    I got it :)

    Code:
    $mytitle = $mypost['post_title'] . " - " . $mypostcategories[0]->cat_name;
     
  5. plasmaflux

    plasmaflux Registered Member

    Joined:
    Feb 20, 2010
    Messages:
    91
    Likes Received:
    37
    Excellent!

    Glad you got it sorted out. Nothing like kicking some Wordpress ass to make one's day a little brighter.

    :D
     
    • Thanks Thanks x 1
  6. Andi49

    Andi49 Newbie

    Joined:
    Apr 26, 2009
    Messages:
    26
    Likes Received:
    1
    yeah I need some training with PHP ;) thx for the support