Ticker

6/recent/ticker-posts

wordpress 側邊欄調取不同分類的文章



有一些wordpress的主題小工具裏面支持側邊調取不同分類的文章,只需要將添加分類文章的小工具拖拽到側邊欄就可以添加上了,然後選擇對應的分類,設置顯示的文章篇數。但是有的主題沒有這個小工具,那怎麽辦呢?這裏我們利用短代碼的插件功能來實現。

對於初學者,或者沒有代碼基礎的朋友來説,大家不要聽到需要代碼或者看到下面比較多的代碼就恐慌。下面這個很簡單,就能實現wordpress 側邊欄調取不同分類的文章。

首先大家安裝一個短代碼的插件 Code Widget

Code Widget



安裝好后,啓動這個插件。那麽在小工具裏面就會有這個短代碼的選項。

wordpress 側邊欄調取不同分類的文章

將此小工具添加到側邊欄后,你會發現在它的Widget Type:那裏有多個選擇,我們選擇php code。



之後根據你自己的需要把一下代碼複製到小工具裏面的就可以了。


第一個,顯示指定的文章和數量。你可以找出想要顯示的那幾篇文章的ID,然後替換     'post__in'   => array(1,67,87,57),裏面的數字。切記選擇的ID數目要和下面的   'posts_per_page' => 4, 數值相同。在拷貝粘貼代碼的時候,切記要吧//和//後面的中文字一起去掉。(需要修改的地方 文中以高亮標識出來,需要刪除的部分已經標成灰色。)

 <ul>  

<?php  

    $args=array(  

        'post__in'   => array(1,67,87,57),//文章的ID

        'posts_per_page' => 4, // 显示篇数  

    );  

    query_posts($args);  

    if(have_posts()) : while (have_posts()) : the_post(); ?>  //判断、循环开始

    <li>  

        <a title="<?php the_title();?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?></a> //标题链接

    </li>  

<?php  endwhile; endif; wp_reset_query(); ?>  //结束循环

</ul>


第二個,顯示指定的文章和數量,並帶有縮略圖。<img width="720" height="480"此處為調整縮略圖的大小。

<ul>  

<?php  

    $args=array(  

        'post__in'   => array(1,67,87,57),//文章的ID

        'posts_per_page' => 4, // 显示篇数  

    );  

    query_posts($args);  

    if(have_posts()) : while (have_posts()) : the_post(); ?>  //判断、循环开始

    <li>  

        <a title="<?php the_title();?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?></a> //标题链接

    </li>  

     <div class="thumbnail">//缩略图调用开始

<a title="<?php the_title();?>" href="<?php the_permalink(); ?>"><?php if((function_exists('has_post_thumbnail')) && (has_post_thumbnail())){$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()) );?><img width="720" height="480" src="<?php echo $thumbnail_src[0];?>"/><?php }else {?><img alt="<?php the_title();?>" src="<?php echo catch_that_image(); ?>"/><?php } ?></a>

</div>//缩略图调用结束

<?php  endwhile; endif; wp_reset_query(); ?>  //结束循环

</ul>



第三個,調取某分類的文章和數目,並調取文章中的摘要。如果不需要摘要部分,只需要把橘紅色的部分刪除掉即可。

<ul>  

<?php  

    $args=array(  

        'cat' => 1,   // 分类ID  

        'posts_per_page' => 10, // 显示篇数  

    );  

    query_posts($args);  

    if(have_posts()) : while (have_posts()) : the_post();  

?>  

    <li>  

        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> //标题  

        <span>  

        <?php if (has_excerpt()) {  

                echo $description = get_the_excerpt(); //文章编辑中的摘要  

            }else {  

                echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要  

            } ?>  

        </span>  

    </li>  


<?php  endwhile; endif; wp_reset_query(); ?>  


第四個,調取分類的文章和數目,並調取縮略圖,摘要,設置標題的大小,以及兩兩之間的間隔。


<ul>  

<?php  

    $args=array(  

        'cat' => 4, 

        'posts_per_page' => 10,

    );  

    query_posts($args);  

    if(have_posts()) : while (have_posts()) : the_post();  

?>  

    <li>  

       <h4> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>  </h4> <br>

        <span>  

        <?php if (has_excerpt()) {  

                echo $description = get_the_excerpt(); 

            }else {  

                echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); 

            } ?>  

        </span>  

    </li>  


 <div class="thumbnail">

<a title="<?php the_title();?>" href="<?php the_permalink(); ?>"><?php if((function_exists('has_post_thumbnail')) && (has_post_thumbnail())){$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()) );?><img width="720" height="480" src="<?php echo $thumbnail_src[0];?>"/><?php }else {?><img alt="<?php the_title();?>" src="<?php echo catch_that_image(); ?>"/><?php } ?></a>

</div>

<br>

<?php  endwhile; endif; wp_reset_query(); ?>  

</ul>


好啦,這樣就可以自如的調取各分類的文章啦。

Reactions

發佈留言

0 留言