2016-11-24 76 views
1

我有以下功能有条件地显示共享链接。所以当它是主页时,它会显示单个页面的不同列表。但它适用于家庭,而在单个帖子中则没有显示任何内容。 她是我的代码为什么这段代码无法正常工作?

function social_links(){ 
if(is_home()){ 
    $bitlink = $lnk = get_bloginfo('url'); 
    $nam = get_bloginfo('name'); 
} 
$current_post_id=get_the_ID(); 
if(is_single($post->ID)){ 
    $bitlink = $lnk = get_permalink($current_post_id); 
    $nam = get_the_title($current_post_id); 
} 
$them_uri = get_stylesheet_directory_uri(); 
$bitly = getBitly($bitlink); 
$url = wp_get_attachment_url(get_post_thumbnail_id($current_post_id)); 
echo '<div id="socialleft"> 
     <ul> 
      <li> 
       <img src="'.$them_uri.'/images/social/share-64.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-64.png" alt="شارك على فيسبوك" /> 
       </a> 
      </li> 
      <li> 
       <a href="http://twitter.com/home/?status='.$nam.' : '.$bitly.'" title="" target="_blank"> 
        <img src="'.$them_uri.'/images/social/twitter-64.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-64.png" alt="+شارك على جوجل" /> 
       </a> 
      </li> 
      <li> 
       <a href="http://pinterest.com/pin/create/button/?url='.$lnk.'&media='.esc_url($url).'" title="" target="_blank"> 
        <img src="'.$them_uri.'/images/social/pinterest-64.png" alt="شارك على بينترست" /> 
       </a> 
      </li> 
     </ul> 
    </div>';} 

我使用的功能到包含

<?php 
    /** 
    * The main template file. 
    * 
    * @package Brandon 
    * @author Muffin group 
    * @link http://muffingroup.com 
    */ 

    get_header(); 
    ?> 

    <!-- #Content --> 
    <div id="Content"> 
    <div class="content_wrapper clearfix"> 

    <!-- .sections_group --> 
    <div class="sections_group"> 


     <div class="section"> 
      <div class="section_wrapper clearfix"> 
       <?php 
        echo '<div class="posts_wrapper clearfix">';    
         while (have_posts()){ 
          the_post(); 
          get_template_part('includes/content', get_post_type()); 
         } 
        echo '</div>'; 

        // pagination 
        if(function_exists('rc_pagination')): 
         rc_pagination(); 
        else: 
         ?> 
          <div class="nav-previous"><?php next_posts_link(__('&larr; Older Entries', 'brandon')) ?></div> 
          <div class="nav-next"><?php previous_posts_link(__('Newer Entries &rarr;', 'brandon')) ?></div> 
         <?php 
        endif; 
       ?> 
      </div> 
     </div> 


    </div> 

    <!-- .four-columns - sidebar --> 
    <?php get_sidebar(); ?> 
    <?php render_social_links();?> 
</div> 

回答

1

有了您的新修改后的代码,仍然存在一些问题。

  1. 您还在使用$post->ID
  2. get_the_ID()不会在帖子页(首页)工作
  3. 的值不正确过滤之前呈现出的浏览器。

让我来帮你。

抓取的帖子页ID

的第一步是抢后的ID为主页,这是帖子页。下面是一个函数来做到这一点:

/** 
* Get the posts page ID. 
* 
* @since 1.0.0 
* 
* @return integer 
*/ 
function get_posts_page_id() { 
    $post_id = get_option('page_for_posts'); 
    if ($post_id) { 
     return (int) $post_id; 
    } 

    return (int) get_queried_object_id(); 
} 

第2步:修改你的代码

下一步是初始化永久链接,文章标题,以及针对单篇文章或帖子ID变量帖子页。如果没有帖子ID,那么无法将社交链接呈现出来。所以它救了出来。另外,为了将事情分开,最好将HTML移出业务逻辑并将其放入View文件中。这里我包含了新的视图文件。

您还会注意到,如果该帖子没有特色图片,则会将其设置为空字符串。

function render_social_links() { 
    $post_id = 0; 

    if (is_home()) { 
     $post_id = get_posts_page_id(); 
     $permalink = get_permalink($post_id); 
     $post_title = get_bloginfo('name'); 
    } 

    if (is_single()) { 
     $post_id = get_the_ID(); 
     $permalink = get_permalink($post_id); 
     $post_title = get_the_title($post_id); 
    } 

    if (! $post_id) { 
     return; 
    } 

    $theme_uri = get_stylesheet_directory_uri(); 
    $bitly  = getBitly($permalink); 

    $featured_image_url = ''; 
    if (has_post_thumbnail($post_id)) { 
     $featured_image_id = get_post_thumbnail_id($post_id); 
     $featured_image_url = $featured_image_id ? wp_get_attachment_url($featured_image_id) : ''; 
    } 

    $permalink   = esc_url($permalink); 
    $post_title_attribute = esc_attr($post_title); 
    $post_title   = esc_html($post_title); 

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

查看文件

接下来,你需要创建一个具有HTML和它嵌入PHP变量视图文件。我在上面的例子中显示位于views/social-links.php。以下是该文件:

请注意,该文件不以<?php开头。相反,我们使用本地HTML,然后嵌入您需要的变量来填充标记中的各个区域。

<div id="socialleft"> 
    <ul> 
     <li> 
      <img src="<?php echo $theme_uri; ?>/images/social/share-64.png" alt="مشاركة"/> 
     </li> 
     <li> 
      <a href="http://www.facebook.com/sharer.php?u=<?php echo $permalink; ?>&amp;t=<?php echo $post_title_attribute; ?>" title="" target="_blank"> 
       <img src="<?php echo $theme_uri; ?>/images/social/facebook-64.png" alt="شارك على فيسبوك"/> 
      </a> 
     </li> 
     <li> 
      <a href="http://twitter.com/home/?status=<?php echo $post_title_attribute; ?>:<?php echo esc_url($bitly); ?>" title="" target="_blank"> 
       <img src="<?php echo $theme_uri; ?>/images/social/twitter-64.png" alt="غرد"/> 
      </a> 
     </li> 
     <li> 
      <a href="https://plus.google.com/share?url=<?php echo $permalink; ?>" 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="<?php echo $theme_uri; ?>/images/social/Google-plus-64.png" alt="+شارك على جوجل"/> 
      </a> 
     </li> 
     <?php if ($featured_image_url) : ?> 
     <li> 
      <a href="http://pinterest.com/pin/create/button/?url=<?php echo $permalink; ?>&media=<?php echo esc_url($featured_image_url); ?>" title="" target="_blank"> 
       <img src="'<?php echo $theme_uri; ?>/images/social/pinterest-64.png" alt="شارك على بينترست"/> 
      </a> 
     </li> 
     <?php endif; ?> 
    </ul> 
</div> 
+0

你是华丽的,你的答案更有意义,但我已经做了所有的建议,但仍然没有显示。我会编辑我的代码到问题 –

+0

是的,请分享你现在有什么然后我们可以进一步帮助你和你的代码。 – hellofromTonya

+0

@MohamedOmar我修改了你的新修改代码的答案。试一试。 – hellofromTonya

1
function social_links(){ 
if(is_home()){ 
    $bitlink = get_bloginfo('url'); 
    $lnk = get_bloginfo('url'); 
    $nam = get_bloginfo('name'); 
    echo "You are at homepage"; 
} 
$current_post_id=get_the_ID(); 
if(is_single($current_post_id)){ 
    $bitlink = get_permalink($post->ID); 
    $lnk = the_permalink(); 
    $nam = the_title(); 
    echo "You are at single page"; 
} 
} 

上述功能,您将不显示单页在index.php因为单个页面的条件不满意。您将尝试使用此代码。

而不是单请,

如果(is_singular( '后')){

//your code here... 

}

请参考以下链接: https://wordpress.stackexchange.com/questions/59979/how-to-detect-single-php-but-not-single-portfolio-php

+0

谢谢,但它没有任何区别。仍然没有显示 –

+0

您可以在单页上打印帖子ID标题。 –

+0

@MohamedOmar你能分享一个网址吗?我必须再问一个问题,你的病情是否有效? –

相关问题