簡體   English   中英

發布內容后如何添加小部件

[英]How to add a widget after post content

現在,在我的主頁主題中,我有1行,3行,3行,然后又回到1行。 在此循環之后,我想添加一個新的小部件。 我希望有一些不同的小部件,例如熱門視頻,熱門帖子等。有人知道我該怎么做嗎?

這是我希望它看起來像的一個示例。 我希望每14個帖子有一個新的小部件。

在此處輸入圖片說明

這是一些我想添加的不同小部件的示例(來源-http: //www.whowhatwear.com )-

在此處輸入圖片說明

在此處輸入圖片說明

這是我的front-page.php

 <?php /* * Template Name: learningwordpress */ get_header(); get_template_part ('inc/carousel'); $i = 0; $args = array( 'posts_per_page' => 14, 'paged' => 1 ); $the_query = new WP_Query($args); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { if( $i %2 == 1 ) { $the_query->the_post(); ?> <article class="post col-md-4"> <?php the_post_thumbnail('medium-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php $the_query->the_post(); ?> <article class="post col-md-4"> <?php the_post_thumbnail('medium-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php $the_query->the_post(); ?> <article class="post col-md-4"> <?php the_post_thumbnail('medium-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php $the_query->the_post(); ?> <article class="post col-md-4"> <?php the_post_thumbnail('medium-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php $the_query->the_post(); ?> <article class="post col-md-4"> <?php the_post_thumbnail('medium-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php $the_query->the_post(); ?> <article class="post col-md-4"> <?php the_post_thumbnail('medium-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php } else { $the_query->the_post(); ?> <article class="post col-md-12"> <?php the_post_thumbnail('large-thumbnail'); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p> <?php echo get_the_excerpt(); ?> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> </p> </article> <?php } ?> <?php $i++; } } else { echo '<p>Sorry, no posts matched your criteria.</p>'; } get_footer(); 

在主題的functions.php文件中添加以下代碼以創建小部件

register_sidebar( array(
        'name'          => 'After content',
        'id'            => 'after-content',
        'description'   => '',
        'before_widget' => '',
        'after_widget'  => '',
        'before_title'  => '<h2>',
        'after_title'   => '</h2>',
    ) );

在要顯示小部件區域的位置添加以下代碼

if ( is_active_sidebar( 'after-content' ) ) : 
    dynamic_sidebar( 'after-content' );
endif;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM