如何创建包含模板的新函数

时间:2016-12-09 作者:user6738171

我正在为我的所有帖子添加社交分享按钮。有没有一种方法可以将代码放入函数中,这样当我将社交共享按钮放在某个地方时,我就可以调用该函数?

这是社交共享按钮代码。。。

                <a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-twitter.png"></a>

                <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" target="_blank" title="Share on Facebook."><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-facebook.png"></a>

                <a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>" target="_blank"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-pinterest.png"></a>

                <a href="http://www.bloglovin.com/e/?b=\' .get_bloginfo(\'wpurl\') . \'&#038;p=\' .get_permalink().\'&#038;t=\'.get_the_title().\'"onclick=\\\'window.open(this.href,&quot;bloglovin_like&quot;,&quot;height=320,width=480,toolbar=0,location=0,menubar=0,scrollbars=0,status=0&quot;); return false;\\\'--><img style="margin: 10px 0 10px 0; border: 0; padding: 0;" src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-bloglovin-1.png" alt="" />

                <a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/12/social-shopping.png"></a>
这是我的正面。php我想把社交分享按钮放在每篇文章的阅读更多按钮之前。然而,如果我把社会共享代码放在每一个之前,我将不得不在代码中粘贴很多次。所以我想知道是否有更有效的方法来做到这一点?(我知道我在这段代码中已经有了一些函数-这是因为我在创建这段代码时得到了一些帮助-我对这段代码还是新手,我自己也不太清楚如何做到这一点)

<?php
/*
 * Template Name:
 */

get_header();
get_template_part (\'inc/carousel\');

$the_query = new WP_Query( [
    \'posts_per_page\' => 14,
    \'paged\' => 1
] );
<div id="ajax">
if ( $the_query->have_posts() ) {
    $i = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();

        if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
            <article <?php post_class( \'col-md-12\' ); ?>>
                <?php the_post_thumbnail(\'large-thumbnail\'); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( \'share-buttons\' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link (\'No Comments\', \'1 Comment\', \'% Comments\', \'comment-count\', \'none\'); ?>
            </article><?php

        } else { // Small posts ?>

            <article <?php post_class( \'col-md-4\' ); ?>>
                <?php the_post_thumbnail( \'medium-thumbnail\' ); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( \'share-buttons\' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link (\'No Comments\', \'1 Comment\', \'% Comments\', \'comment-count\', \'none\'); ?>
    </article>
<?php 
        }
        $i++;
    }
    wp_reset_postdata();
    </div>
}
else {
    echo \'<p>Sorry, no posts matched your criteria.</p>\';
}
<?php load_more_button(); ?>
get_footer();
这是头版的布局enter image description here

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

将共享按钮html添加到主题中的模板文件:

your-theme/share-buttons.php

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-twitter.png"></a>

<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" target="_blank" title="Share on Facebook."><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-facebook.png"></a>

<a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>" target="_blank"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-pinterest.png"></a>

<a href="http://www.bloglovin.com/e/?b=\' .get_bloginfo(\'wpurl\') . \'&#038;p=\' .get_permalink().\'&#038;t=\'.get_the_title().\'"onclick=\\\'window.open(this.href,&quot;bloglovin_like&quot;,&quot;height=320,width=480,toolbar=0,location=0,menubar=0,scrollbars=0,status=0&quot;); return false;\\\'--><img style="margin: 10px 0 10px 0; border: 0; padding: 0;" src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-bloglovin-1.png" alt="" />

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/12/social-shopping.png"></a>
使用get_template_part() 将模板包含在适当的位置。

get_template_part( \'share-buttons\' );
Edit: 这里是模板代码的更新版本,应该解决原始版本中的一些问题。它还包括共享按钮模板部分:

<?php
/*
 * Template Name:
 */

get_header();
get_template_part (\'inc/carousel\');

$the_query = new WP_Query( [
    \'posts_per_page\' => 14,
    \'paged\' => 1
] );

if ( $the_query->have_posts() ) {
    $i = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();

        if ( $i % 7 === 0 ) { // Large post: on the first iteration and after every block of 6 posts... ?>

            <article <?php post_class( \'col-md-12\' ); ?>>
                <?php the_post_thumbnail(\'large-thumbnail\'); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( \'share-buttons\' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link (\'No Comments\', \'1 Comment\', \'% Comments\', \'comment-count\', \'none\'); ?>
            </article><?php

        } else { // Small posts ?>

            <article <?php post_class( \'col-md-4\' ); ?>>
                <?php the_post_thumbnail( \'medium-thumbnail\' ); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( \'share-buttons\' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link (\'No Comments\', \'1 Comment\', \'% Comments\', \'comment-count\', \'none\'); ?>
            </article><?php 
        }
        $i++;
    }
    wp_reset_postdata();
}
else {
    echo \'<p>Sorry, no posts matched your criteria.</p>\';
}

get_footer();

相关推荐

Add code to Functions.php

我想知道我是否在函数中添加了一些短代码。php在我的主题和解决我的问题,会发生什么,如果我更新我的主题??更新后是否删除我的短代码??在将代码添加到函数后,更新主题是否会有任何问题。php?