2016-11-28 125 views
0

下面的代码工作正常,如果我明确地加我想用echo的HTML,但我想使代码更干净由包括标记文件,这是在包括脚本的同一目录中,但由于某种原因它赢得不包括。下面是代码:为什么包含在这里不起作用?

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'); 
     } 

    } 

这是社会links.php内的标记,该文件是一个孩子的主题和包含在孩子的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> 
+0

你能分享“social-links.php”或类似内容的内容吗?它有一些警告或错误? –

+0

如果我直接使用'social-links.php'的内容而不包含它,那么效果很好。但是当包含任何输出时 –

回答

1

我不知道你的social-links.php是什么。

Accoding到手动http://php.net/manual/en/function.include.php

当包含一个文件,解析滴出PHP模式并进入HTML模式在目标文件的开头,并在最后再次重新开始。因此,目标文件中应该作为PHP代码执行的任何代码都必须包含在有效的PHP开始和结束标记中。

如果您只有social-links.php包含HTML,则会将其解析为HTML。

我做了一个简单的测试并且工作。

teste.php

<?php 

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

teste_include(); 

和teste_included.php

<div>teste 2</div><br><div>teste 3</div> 
+0

我的'social-links.php'包含php和html。我编辑了我的问题并添加了其内容 –

+0

感谢人解决了这个问题。我把它放在PHP标签中,它工作 –

0

我不知道哪里是你的社会links.php

假设你的社会links.php在您的自定义主题目录,然后使用下面给出的代码来包含您的社交链接.php

include(get_template_directory_uri(). '/social-links.php'); 
相关问题