WordPress sidebar to retrieve articles in different categories

 There are some WordPress theme widgets that support side access to different categories of articles. Just drag and drop the widget that adds category articles to the sidebar to add them, then select the corresponding category and set the number of articles displayed. . But some themes don’t have this gadget, what should I do? Here we use the shortcode plug-in function to achieve.


For beginners, or friends who don’t have a code foundation, don’t panic when you hear the code you need or see the more code below. The following is very simple, you can implement the wordpress sidebar to retrieve articles of different categories.


First of all, you install a shortcode plugin Code Widget,


Code Widget

https://wordpress.org/plugins/code-widget/




After installation, start the plug-in. Then there will be an option for this shortcode in the gadget.


WordPress sidebar to retrieve articles in different categories



After adding this gadget to the sidebar, you will find that there are multiple choices in its Widget Type: we choose php code.





Then copy the code into the widget according to your own needs.




The first one is to display the specified article and number. You can find out the IDs of the posts you want to display, and then replace the numbers in'post__in' => array(1,67,87,57). Remember to select the number of IDs and the following'posts_per_page' => 4, the same value. When copying and pasting the code, remember to delete the // and the characters behind //. (The places that need to be modified are highlighted in the text, and the parts that need to be deleted have been marked in gray.)


 <ul>


<?php


    $args=array(


        'post__in' => array(1,67,87,57),//post ID


        'posts_per_page' => 4, // show the number of posts


    );


    query_posts($args);


    if(have_posts()): while (have_posts()): the_post(); ?> //Judgment, loop start


    <li>


        <a title="<?php the_title();?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?></a> //title Link


    </li>


<?php endwhile; endif; wp_reset_query(); ?> //End the loop


</ul>




The second one is to display the specified article and quantity, with thumbnails. <img width="720" height="480" here is to adjust the size of the thumbnail.


<ul>


<?php


    $args=array(


        'post__in' => array(1,67,87,57),//post ID


        'posts_per_page' => 4, // show the number of posts


    );


    query_posts($args);


    if(have_posts()): while (have_posts()): the_post(); ?> //Judgment, loop start


    <li>


        <a title="<?php the_title();?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?></a> //title Link


    </li>


     <div class="thumbnail">//The thumbnail call starts


<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>//End of thumbnail call


<?php endwhile; endif; wp_reset_query(); ?> //End the loop


</ul>






The third is to retrieve the number and number of articles in a certain category, and retrieve the abstract in the article. If you don’t need the abstract part, just delete the orange part.


<ul>


<?php


    $args=array(


        'cat' => 1, // category ID


        'posts_per_page' => 10, // show the number of posts


    );


    query_posts($args);


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


?>


    <li>


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


        <span>


        <?php if (has_excerpt()) {


                echo $description = get_the_excerpt(); //Abstract in article editing


            }else {


                echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); //If there is no abstract in the article editing, customize the number of words in the article content as a summary


            } ?>


        </span>


    </li>




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




Fourth, retrieve the classified articles and number, retrieve thumbnails, abstracts, set the size of the title, and the interval between two.




<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>




Okay, so you can freely retrieve articles in each category.

Post a Comment

0 Comments