2016-11-24 73 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你能分享一個網址嗎?我必須再問一個問題,你的病情是否有效? –

相關問題