繁体   English   中英

在首页上的第n个帖子之后添加小部件

[英]Add widget after every nth post on home page

我试图弄清楚出现一定数量的帖子后如何向首页添加不同的自定义小部件(如instagram,pinterest,新闻通讯,视频)。 例如http://margoandme.com ,但是与本博客不同的是,我希望小部件在第n个帖子之后仍然出现,即使您转到下一页。

我将如何完成? 我会在自己的php文件中编写小部件(例如instagram的小部件),然后将php文件添加到我的首页循环中吗? 如果我这样做,那我将如何让每个人在第n个帖子后出现。 我想我想要大约5个小部件,并且希望每5个帖子后出现其中一个。

我的front-page.php

 <?php get_header(); get_template_part ('post-template/trendingg'); ?> <script> var now=2; // when click start in page 2 jQuery(document).on('click', '#load_more_btn', function () { jQuery.ajax({ type: "POST", url: "<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php", data: { action: 'my_load_more_function', // the name of the function in functions.php paged: now, // set the page to get the ajax request posts_per_page: 15 //number of post to get (use 1 for testing) }, success: function (data) { if(data!=0){ jQuery("#ajax").append(data); // put the content into ajax container now=now+1; // add 1 to next page }else{ jQuery("#load_more_btn").hide(); } }, error: function (errorThrown) { alert(errorThrown); // only for debuggin } }); }); </script> <section id="ajax"><!-- i have to change div to section, maybe a extra div declare --> <?php $the_query = new WP_Query( [ 'posts_per_page' => 15, // i use 1 for testing 'orderby' => 'post_date', 'order' => 'DESC', 'paged' => get_query_var('paged', 1) //page number 1 on load ] ); if ($the_query->have_posts()) { $i = 0; $j = 0; while ($the_query->have_posts()) { $the_query->the_post(); if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... ?> <div class="row"> <article <?php post_class( 'col-sm-12 col-md-12' ); ?>> <div class="large-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?></a> </div> <div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> </div> <?php } else { // Small posts ?> <?php if($j % 2 === 0){ echo '<div class="row">';} ?> <article <?php post_class( 'col-sm-6 col-md-6' ); ?>> <div class="two-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></a> <div> <div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> <?php $j++; if($j % 2 === 0){ echo '</div>';}?> <?php } $i++; }?> <?php }?> </section> <button id="load_more_btn">Load More Posts</button> <!-- button out of ajax container for load content and button displayed at the bottom --> <?php get_footer(); 

我的functions.php

 //FRONT PAGE add_action('wp_ajax_my_load_more_function', 'my_load_more_function'); add_action('wp_ajax_nopriv_my_load_more_function', 'my_load_more_function'); function my_load_more_function() { $query = new WP_Query( [ 'posts_per_page' => $_POST["posts_per_page"], 'orderby' => 'post_date', 'order' => 'DESC', 'paged' => get_query_var('paged', $_POST["paged"]) ] ); if ($query->have_posts()) { $i = 0; $j = 0; while ($query->have_posts()) { $query->the_post(); if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... ?> <div class="row"> <article <?php post_class( 'col-sm-12 col-md-12' ); ?>> <div class="large-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?></a> </div> <div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> </div> <?php } else { // Small posts ?> <?php if($j % 2 === 0) echo '<div class="row">'; ?> <article <?php post_class( 'col-sm-6 col-md-6' ); ?>> <div class="two-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></a> <div> <div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> <?php $j++; if($j % 2 === 0) echo '</div>'; ?> <?php } $i++; } wp_reset_query(); }else{ return 0; } exit; } 

***我根据建议更新了front-page.php

 <?php get_header(); get_template_part ('post-template/trendingg'); ?> <script> var now=2; // when click start in page 2 jQuery(document).on('click', '#load_more_btn', function () { jQuery.ajax({ type: "POST", url: "<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php", data: { action: 'my_load_more_function', // the name of the function in functions.php paged: now, // set the page to get the ajax request posts_per_page: 15 //number of post to get (use 1 for testing) }, success: function (data) { if(data!=0){ jQuery("#ajax").append(data); // put the content into ajax container now=now+1; // add 1 to next page }else{ jQuery("#load_more_btn").hide(); jQuery("#content-load-more-btn").html("<h4 class='no-more-load'>No more items to load.</h4>"); jQuery('.no-more-load').delay(2000).fadeOut('slow'); } }, error: function (errorThrown) { alert(errorThrown); // only for debuggin } }); }); $(document).ready(function () { setTimeout(function() { $('.no-more-show').slideUp("slow"); }, 5000); }); </script> <section id="ajax"><!-- i have to change div to section, maybe a extra div declare --> <?php $the_query = new WP_Query( [ 'posts_per_page' => 15, // i use 1 for testing 'orderby' => 'post_date', 'order' => 'DESC', 'paged' => get_query_var('paged', 1) //page number 1 on load ] ); if ($the_query->have_posts()) { $i = 0; $j = 0; while ($the_query->have_posts()) { $widgetCount = 0; $widgets = ['widgets/shop-widget.php','widgets/insta-widget.php','widgets/video-widget.php','widgets/pinterest-widget.php']; $numWidgets = count($widgets); $the_query->the_post(); if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... if ($widgetCount < $numWidgets) { //if we haven't used all the widgets include($widgets[$widgetCount]); //include next widget $widgetCount++; //increment the count } ?> <div class="row"> <article <?php post_class( 'col-sm-12 col-md-12' ); ?>> <div class="large-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?></a> </div> <div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> </div> <?php } else { // Small posts ?> <?php if($j % 2 === 0){ echo '<div class="row">';} ?> <article <?php post_class( 'col-sm-6 col-md-6' ); ?>> <div class="two-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></a> </div> <div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> <?php $j++; if($j % 2 === 0){ echo '</div>';}?> <?php } $i++; }?> <?php }?> </section> <div id="content-load-more-btn"> <button id="load_more_btn">Load More Posts</button> <!-- button out of ajax container for load content and button displayed at the bottom --> </div> <?php get_footer(); 

***根据建议,我更新了functions.php

 //FRONT PAGE add_action('wp_ajax_my_load_more_function', 'my_load_more_function'); add_action('wp_ajax_nopriv_my_load_more_function', 'my_load_more_function'); function my_load_more_function() { $query = new WP_Query( [ 'posts_per_page' => $_POST["posts_per_page"], 'orderby' => 'post_date', 'order' => 'DESC', 'paged' => get_query_var('paged', $_POST["paged"]) ] ); if ($query->have_posts()) { $i = 0; $j = 0; while ($query->have_posts()) { $widgetCount = 0; $widgets = ['widgets/shop-widget.php','widgets/insta-widget.php','widgets/video-widget.php','widgets/pinterest-widget.php']; $numWidgets = count($widgets); $query->the_post(); if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... if ($widgetCount < $numWidgets) { //if we haven't used all the widgets include($widgets[$widgetCount]); //include next widget $widgetCount++; //increment the count } ?> <div class="row"> <article <?php post_class( 'col-sm-12 col-md-12' ); ?>> <div class="large-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?></a> </div> <div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> </div> <?php } else { // Small posts ?> <?php if($j % 2 === 0) echo '<div class="row">'; ?> <article <?php post_class( 'col-sm-6 col-md-6' ); ?>> <div class="two-front-container"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></a> <div> <div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div> <div class="front-page-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'includes/front-shop-the-post' ); ?> <?php get_template_part( 'includes/share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> <?php $j++; if($j % 2 === 0) echo '</div>'; ?> <?php } $i++; } wp_reset_query(); }else{ return 0; } exit; } 

您已经有一个条件语句,使用PHP的模运算符比较循环索引与被5整除的索引。 将您的逻辑放在该if语句中。

至于如何包括它们,有许多方法可以实现,但是您要确保它们不重复。

这是一个粗略的布局,假设您只是将小部件放在包含文件中:

// CREATE A NEW .php FILE FOR EACH OF YOUR WIDGETS AND SAVE
// SAVE THEM TO YOUR SERVER. THEN ADD THE FOLLOWING 3 LINES BEFORE YOUR
// while LOOP. MAKE SURE THE NAMES OF YOUR NEW FILES ARE WHATS IN THE
// 'widgets' ARRAY AND THAT THE PATHS TO THE FILES ARE CORRECT.
$widgetCount = 0;
$widgets = ['twitter.php','insta.php','facebook.php','reddit.php'];
$numWidgets = count($widgets);

// FOLLOWING IS BASED ON YOUR EXISTING WHILE LOOP:
while ($the_query->have_posts()) {  //loop thru posts
    $the_query->the_post();
    if ($i % 5 === 0) { //if post divisible by 5 LOOK FOR THIS IN YOUR CODE!

        // ADD THE FOLLOWING.  THIS WILL ADD ONE OF YOUR WIDGETS FROM
        // FROM THE ARRAY ABOVE.
        if ($widgetCount < $numWidgets) { //if we haven't used all the widgets
            include($widgets[$widgetCount]); //include next widget
            $widgetCount++; //increment the count
        }

        // all your other existing post rendering here.


    } // end of if statement
} // end while loop

您已经实现了Logic,例如: if( $i % 5 === 0 ) {} ,您不需要在functions.php文件中调用相同的条件。 只需在此if ( $i % 5 === 0 ) { //Your Widget Code }您的窗口if ( $i % 5 === 0 ) { //Your Widget Code }下添加您的窗口if ( $i % 5 === 0 ) { //Your Widget Code }并调用它。

病情一旦发作,您就可以实现真正需要的东西。

有关Loop和Widget的更多信息,请阅读WordPress支持,其中已经定义了这些内容。 (i): https : //codex.wordpress.org/Widgets_API和(ii): https : //codex.wordpress.org/Function_Reference/dynamic_sidebar

上述方法不可扩展。 如果您正在寻找社交网络以供参考,那么您正在寻找一种更好的方法。

推荐方法的一个示例是拥有2个数据集,一个拥有帖子/提要,另一个拥有小部件数据。

例:

Posts: [
 [post 1],
 [post 2],
  ...
]

小部件:

[
  [
    position: 5,
    widget: 'video',
    src: 'file, path or something',
    template: 'if to pick a specific template',
    ...(width, height)
  ],
  [
    position: 11,
    widget: 'ad',
    src: 'file, path or something',
    template: 'if to pick a specific template',
    ...(width, height)
  ],
]

现在,在渲染帖子/提要时,您只需要在渲染之前将2合并即可。

这种方式允许保留任何模板视图或数据的各种类型的卡/小部件。

通常,我会合并卡片并将其称为提要,并为其添加“类型”属性。 这样我也可以保持动态位置,而不是所有位置的第5位或第10位。

输出:

[
  [ type: 'post', title: '', excerpt: '',...]
  [ type: 'video', ...]
  [ type: 'ad', ...]
]

在for循环中渲染它们时,您只需检查一下

if(type = post)
  echo template('post', $data)
if(type = video) 
  echo template('video', $data)

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM