douzengjian1535 2016-11-28 11:24 采纳率: 100%
浏览 53
已采纳

为什么包含在这里不起作用?

The following code works fine if i explicitly add the HTML i want using echo, but i wanted to make the code more clean by including the markup file, which is in the same directory of the including script, but for some reason it won't include. Here is the code:

function render_social_links() {
$current_post_id=get_the_ID();
$them_uri = get_stylesheet_directory_uri();
$featured_image_url = '';
if ( has_post_thumbnail( $current_post_id ) ) {
    $featured_image_id  = get_post_thumbnail_id( $current_post_id );
    $featured_image_url = $featured_image_id ? wp_get_attachment_url( $featured_image_id ) : '';
}
if(is_home() || is_single()){
      if(is_home()){
        $bitlink = $lnk = get_bloginfo('url');
        $bitly = getBitly($bitlink);
        $nam = get_bloginfo('name');
        }
        elseif(is_single($current_post_id)){
            $bitlink = $lnk = get_permalink($current_post_id);
            $bitly = getBitly($bitlink);
            $nam = get_the_title($current_post_id);
        }

        include( __DIR__ . '/social-links.php' );
      }

  }

And this is the markup inside the social-links.php, and this file is in a child theme and is included in the child's functions.php

<div id="socialleft">
            <ul>
                <li>
                    <img src="'.$them_uri.'/images/social/share-38.png" alt=""/>
                </li>
                <li>
                    <a href="http://www.facebook.com/sharer.php?u='.$lnk.'&amp;t='.$nam.'" title="شارك على فيسبوك" target="_blank">
                </li>
                        <img src="'.$them_uri.'/images/social/facebook-38.png" alt="" />
                    </a>
                </li>
                <li>
                    <a href="http://twitter.com/home/?status='.$nam.' : '.$bitly.'" title="غرد" target="_blank">
                        <img src="'.$them_uri.'/images/social/twitter-38.png" alt="" />
                    </a>
                </li>
                <li>
                    <a href="https://plus.google.com/share?url='.$lnk.'" onclick="javascript:window.open(this.href,
                      \'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;" title="شارك على جوجل+" target="_blank">
                        <img src="'.$them_uri.'/images/social/Google-plus-38.png" alt="" />
                    </a>
                </li>
            </ul>
        </div>
  • 写回答

2条回答 默认 最新

  • dqajyxqem115006813 2016-11-28 12:35
    关注

    I don't know what is your social-links.php.

    Accoding to the manual http://php.net/manual/en/function.include.php

    When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

    If you social-links.php only contains HTML this will be parsed as HTML.

    I make a simple test and worked.

    teste.php

    <?php
    
    function teste_include()
    {
        include 'teste_include.php';
    }
    
    teste_include();
    

    and teste_included.php

    <div>teste 2</div><br><div>teste 3</div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?