trouble w/ Wordpress functions

jlknauff

New member
Aug 25, 2008
237
1
0
In Wordpress (functions.php) I'm trying to make a function occur only if the post date is not equal to today's date.

The problem I seem to be running into is getting the post date from within functions.php rather than the theme file (page.php for example). Any ideas how to make this happen?

PHP:
<?php

function internal_linking($string) {


    $comp_date_1 = $post->post_date;  
    $comp_date_2 = date( 'Y-m-d' ); 

    if ( $comp_date_1 != $comp_date_2 ){
        do something;
    }        
    return $string;
}

add_filter('the_content', 'internal_linking');

?>
 


No dice...

Warning: Missing argument 2 for internal_linking() in /public_html/DEMO/wp-content/themes/IL/functions.php on line 3
 
No dice...

Warning: Missing argument 2 for internal_linking() in /public_html/DEMO/wp-content/themes/IL/functions.php on line 3

jesus, the error message gives you your answer...did you actually pass in the date when you called the function in your theme file?

when you call your function, you have to pass in both items, the first thing and the date
 
ahhh, i see what's going on, you're running it as a filter. You're going to have to dig into the filter system to see if it's possible to pass in additional params, because as it stands, all you're doing is basically saying "everytime I call the_content(), pass it through internal_linking() first". That's why your internal linking was just accepting a string, because the_content() just outputs a string.

Get creative
 
You can just use the global $post.

Code:
function internal_linking($string) {
    global $post;
    $comp_date_1 = date('Ymd', strtotime($post->post_date));  
    $comp_date_2 = date('Ymd'); 
    
    var_dump($comp_date_1, $comp_date_2);

    if ( $comp_date_1 != $comp_date_2 ) {
    }
     
    return $string;
}

add_filter('the_content', 'internal_linking');

Also, I fixed your date comparison.
 
Almost perfect! For some reason though, it adds the following data at the beginning of the post

PHP:
string(8) "20100503" string(8) "20100503"

Any idea how to prevent it or remove it?