如何在发布内容后添加小部件

时间:2016-08-27 13:24:40

标签: wordpress widget

现在在我的主页主题中,我有一排1个帖子,一排3个帖子,3个帖子,然后又回到1个帖子的一行。在这个循环之后,我想添加一个新的小部件。我想要一些不同的小工具,如热门视频,热门帖子等。有没有人知道我该怎么做?

以下是我希望它的样子。我希望每14个帖子都有一个新的小部件。

enter image description here

以下是我要添加的不同小部件的一些示例(来源 - http://www.whowhatwear.com) -

enter image description here

enter image description here

这是我的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();

1 个答案:

答案 0 :(得分:1)

在主题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;
相关问题