WordPress.org

Ready to get started?Download WordPress

Forums

Need help getting around the "variables can be passed by reference" error (2 posts)

  1. Stephiew
    Member
    Posted 1 year ago #

    I have finally found a way to generating a slug name that will call a specific post to show on another post's sidebar!

    $aside = $post; echo $post->post_name;
    do_action('after_aside', $aside);
    $aside = preg_replace('/(\-profile)/','-aside$0',"-aside");
    
    echo $aside

    So now I can have a post with a person's name as the slug, and a post with the slug name-aside, that I can hopefully call up on the new sidebar.
    While this is really great (although messy, the -profile bit works but I'm not sure why), now my problem is calling the new slug name to get THAT post's contents!
    When I do

    get_post('aside')
    or
    get_post($aside)

    I get the dreaded "Only variables can be passed by reference" message, which I don't even understand, not being fluent in PHP.
    From what I can gather, I have to do something to my little working bit to get it to work within the post call. I've tried making it a function (didn't work)
    what else can I try? Does anyone know?
    Thank you,
    Stephanie
    (more info later if required)

  2. Stephiew
    Member
    Posted 1 year ago #

    Figured it out! I'm going to post the code that worked for if anyone else wants to do what I am doing.

    <?php
    ob_start();
    $aside = $post; echo $post->post_name;
    do_action('after_aside', $aside);
    $aside = preg_replace('/(\-profile)/','-aside$0',"-aside");
    echo $aside;
    $aside = ob_get_contents();
    ob_end_clean();
    
    $page = get_posts( array( 'name' => $aside ) );
    
    if ( $page )
    {
        echo $page[0]->post_content;
    }
    
    ?>

    So basically, I put all the code that turns the current post name from name to name-aside, and put it inside the object, then fetch it with my variable name ('$aside = ob_get_contents();') so when I put it into get_posts, it goes to find the constructed slug.
    This is going to be helpful because I am working on a company website, where we want profiles to be a post, and then have a sidebar with additional information about the person in question. The people doing the posting from now on do not know code, so I am making it simple for them. All they need to do is make their posts, and name the slugs accordingly (name and name-aside) for it to be constructed for them automatically!
    Best part: no plugins or widgets used!

Topic Closed

This topic has been closed to new replies.

About this Topic