为什么这段代码不能正常工作?

时间:2016-11-24 16:59:59

标签: wordpress

我有以下功能可以有条件地显示共享链接。因此,当它是主页时,它显示与单个页面不同的列表。但它适用于家庭,而在单个帖子中它没有显示任何东西。 她是我的代码

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

我将该函数用于包含

的index.php
<?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>

2 个答案:

答案 0 :(得分: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";
}
}

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

请点击,而不是单身,

if(is_singular('post')){

//your code here...

}

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

答案 1 :(得分:1)

使用新修订的代码,仍然存在一些问题。

  1. 您仍在使用$post->ID
  2. get_the_ID()不适用于帖子页面(主页)
  3. 在渲染到浏览器之前,这些值未正确清理。
  4. 我来帮你。

    获取帖子页面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>
    
相关问题