• Resolved Lou Sparx

    (@roulettered)


    Hi guys,

    I’m trying to move some PHP code from my Functions.php to my Header.php.

    function hello() {
    global $post;
    if (!is_single()) {
        return;
    }
    
    //print_r($post);
    $pony = strip_tags($post->post_content);
    $pony = str_replace(array('"',"'", "\n", "\r", "\t"), ''', $pony);
    
    echo "$pony";
    }
    
    add_action('wp_head', 'hello');

    I’ve tried:

    <?PHP {
    global $post;
    if (!is_single()) {
        return;
    }
    
    //print_r($post);
    $pony = strip_tags($post->post_content);
    $pony = str_replace(array('"',"'", "\n", "\r", "\t"), ''', $pony);
    
    echo "$pony";
    }
    
    ;?>

    etc.. But this doesn’t work.

    Any help would be much appreciated.

    Thanks,
    Lou

Viewing 15 replies - 1 through 15 (of 16 total)
  • Can I ask why you’re trying to move this code?.

    Thread Starter Lou Sparx

    (@roulettered)

    I want the output in the header at the top.

    Any suggestions?

    At the top of the <head></head> section of each page or within your theme’s header area?

    Thread Starter Lou Sparx

    (@roulettered)

    I want the 1st code listed to work within my header.php instead of the functions.php but not sure how to convert it from a function to something that would work placed in my header.php.

    Thread Starter Lou Sparx

    (@roulettered)

    If I place:

    `function hello() {
    global $post;
    if (!is_single()) {
    return;
    }

    //print_r($post);
    $pony = strip_tags($post->post_content);
    $pony = str_replace(array(‘”‘,”‘”, “\n”, “\r”, “\t”), ”’, $pony);

    echo “$pony”;
    }

    add_action(‘wp_head’, ‘hello’);`

    In my header.php it doesn’t work.

    It won’t. add_action('wp_head', 'hello'); means that this function is being called when wp_head() runs in header.php. It will still echo its output within the post content of single post pages. You would need to completely re-write the function to get it to output something into your pages’ header.

    Thread Starter Lou Sparx

    (@roulettered)

    Exactly my original post.. If you check my second code example I’m trying to convert that to work within my header.php.

    If anyone can help I’d much appreciate it.

    The function is altering the single post content. Why would you have the post content in the head of a page? If that’s what you want, then you’d need to run a Loop (perhaps using get_posts) in header.php.

    Thread Starter Lou Sparx

    (@roulettered)

    With all due respect, esmi, I’ve had my question answered by several other questions followed by “needs to be re-written” which I was originally asking for help with in the first place lol.

    Thanks anyway

    That’s not how functions work, they don’t run based on the file they reside in..

    Functions are called, either by another function or by a direct function call, ie. my_function(); ..

    Functions should be ideally placed into the functions.php file (although there are other use cases where they might be suited directly in the template file, this isn’t one of them).

    Can you explain what it is that you want to place into your header so someone is able to better advise you on how to proceed.

    That particular function is not suited for placing content into the header area (fine, no problem), but if you want help adapting the function to do something different then in order to advise you we need to be able to understand what is it you’re aiming to do..

    My best guess right now is that you want to print the content from a post into the header, would that be correct? And if so, is the content intended to be read from a particular post or are you aiming for the post content to be pulled dynamically?

    Please clarify what you want to do and i’ll do what i can to help .. 🙂

    Thread Starter Lou Sparx

    (@roulettered)

    Hi Mark,

    OK I’ll start a new query which will lead to the same result.. I have this code that calls the excerpt into a meta tag:

    <meta name=”description” content=”<?php $words = explode(‘ ‘, get_the_excerpt());
    echo implode(‘ ‘, array_slice($words, 0, 24)).’ …’; ?>” />

    The only problem is the excerpt that is called has quotes inside it that I don’t want (they break the meta tag).

    How can I str_replace ” (quotes) within the excerpt, with say a Z?

    Maybe:

    <meta name=”description” content=”<?php str_replace ‘”‘, ‘Z’; $words = explode(‘ ‘, get_the_excerpt());
    echo implode(‘ ‘, array_slice($words, 0, 24)).’ …’; ?>” />

    The excerpt that is being called contains quotes (“) that I don’t want. Please help me str_replace them 🙂

    Lou

    Replace both single and double quotes…

    $something = str_replace( array( '"', "'" ), '', $somevarwithquotes );

    Hope that helps.. 🙂

    Thread Starter Lou Sparx

    (@roulettered)

    So I’m assuming my variable is get_the_excerpt().

    Becomes..

    <meta name=”description” content=”<?php $something = str_replace( array( ‘”‘, “‘” ), ”, get_the_excerpt() ); $words = explode(‘ ‘, get_the_excerpt());
    echo implode(‘ ‘, array_slice($words, 0, 24)).’ …’; ?>” />

    What would replace the $something?

    Last piece of code was only intended as an example..

    Here’s a better version..

    <?php
    $excpt = str_replace( array( '"', "'" ), '', get_the_excerpt() );
    $words = explode( ' ', $excpt );
    $excpt = implode( ' ', array_slice( $words, 0, 24 ) );
    ?>
    <meta name="description" content="<?php echo $excpt; ?>" />

    🙂

    Thread Starter Lou Sparx

    (@roulettered)

    Thank you so much, Mark. Really appreciate you taking the time to write that code for me.

    Lou

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘Moving Function to Header..’ is closed to new replies.