简体   繁体   中英

get_permalink not working because of apostrophes?

I encountered a problem while implementing the Facebook comment box within my posts. The WordPress theme which i'm currently using on does not use single.php to build the post lay-outs, but some sort of module file. I've always managed to make the Facebook comment box work by implementing the code in single.php in the past.

The file that builds the post pages contains an example similar to what i'm referring to

...
} else {
            $buffy .= $this->get_content();
        }

        $buffy .= '<h4 class="block-title"><span>DEEL</span></h4>';
        $buffy .= do_shortcode('[ssba]');

        $buffy .= '<br><h4 class="block-title"><span>REAGEER</span></h4>';
        $buffy .= '<div class="fb-comments" data-href="<?php echo get_permalink(); ?>" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>';

        $buffy .= '<footer class="clearfix">';
        $buffy .= $this->get_post_pagination();
        $buffy .= $this->get_review();
...

As you can see I added the following code:

$buffy .= '<div class="fb-comments" data-href="<?php echo get_permalink(); ?>" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>';

I'm returned with the response "href URL is not properly formatted." I think it has something to do with the fact that there are apostrophes at the beginning and end of the code. Removing the apostrophes will break the whole page and get_permalink doesn't work as it should.

You have an error in your string. The problem is first of all that this is inside PHP -code which means you don't need the open/close-tags to use a function (even in a string).

Furthermore, you use single quotes ( ' ). When using single quotes, PHP is not executed. If you have used double quotes ( " ) you would not need to concatenate the strings like I've done (but you would have to escape all the double quotes from the markup).

This should work:

$buffy .= '<div class="fb-comments" data-href="' . get_permalink() . '" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>';

...or...

$buffy .= "<div class=\"fb-comments\" data-href=\"get_permalink()\" data-colorscheme=\"light\" data-numposts=\"5\" data-mobile=\"false\" data-width=\"700\"></div>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM