Instructions to get post in wordpress and wp_Query in wordpress

Tutorials 0 lượt xem

Hi guys, to continue the topic of wordpress theme programming today we will learn how to get posts in wordpress as well as query post loop in wordpress.

This is considered the backbone of wordpress. We will use it throughout the process of building a wordpress theme.

When installing wordpress, we will see the posts section, which contains all the articles of the website. So how to display the content of those articles outside the interface? We’ll use the get post loop to get them out

Instructions for getting posts in wordpress

Loop get post in wordpress

Syntax:

<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
   // Thông tin cần lấy của 1 bài viết
<?php endwhile;?><?php endif; ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
</div>

Explain:

This is a while loop in php that checks if there are posts and then shows them.

Use:

Depending on where this loop is placed, it will return different results, specifically as follows:

  • Putting it on the homepage (index.php) will list the latest posts
  • Put in the category page (category.php) will display the list of articles of that category
  • Set in detail page (single.php) will display the content of that article
  • Put in the search results page (search.php) it will display the search results of the corresponding keyword

Post elements can be displayed in the query loop:

  • <?php the_title(); ?> Get the title of the article
  • <?php the_content(); ?> Get the content of the article
  • <?php the_excerpt(); ?> Get the description of the post
  • <?php the_category(); ?> Get the category of the article
  • <?php the_author(); ?> Get the author of the article
  • <?php the_post_thumbnail(); ?> Get the post’s avatar
  • <?php the_date(); ?> Get the publication date of the article
  • <?php the_permalink(); ?> Get the link of the article

There are some other ingredients that are rarely used, I can’t list them all here, you can search for more.

For example:

Displays a list of the latest posts, with an avatar and a short description of the article:

<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?><div class="post">
		<a href="&lt;?php%20the_permalink();%20?&gt;">
			<?php the_post_thumbnail(); ?></a>
		<h4><a href="&lt;?php%20the_permalink();%20?&gt;"><?php the_title(); ?></a></h4>
		<div class="desc">
			<?php the_excerpt(); ?>
</div>
	</div>		
<?php endwhile; else : ?><?php endif; ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span>
</div>

New WP_Query loop get posts with parameter

In the above we have learned how to query the default post of wordpress, but for higher requirements such as: Get articles with a certain number, get articles by 1 category or get articles by category. 1 author… then the above loop cannot respond.

So wordpress has provided us with a loop to get the article depending on the parameter that is new Wp_Query, specifically as follows:

New wp_Query syntax:

<?php $args = array(
    'posts_per_page' => -1,
    'post_type'      =&gt; 'post'
    'cat'            =&gt; 1
  );
  $the_query = new WP_Query( $args );
?&gt;
<?php if( $the_query->have_posts() ): ?&gt;
<?php while( $the_query->have_posts() ) : $the_query-&gt;the_post(); ?&gt;
  // Thông tin cần lấy của 1 bài viết
<?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span>
</div>

Explaining new wp_Query:

The syntax of this loop will have 2 parts:

  • Part 1 is array ($args) of parameter data
  • Part 2 is the normal get post part as passed the above parameter to give the desired result.

Input parameters:

  • posts_per_page => Number of posts
  • post_type => is post_type for example: post is post, page is page
  • cat => Id of the category to get
  • p => id of the article to get
  • post_status => Status of the post
  • author => id of the author

There are many input parameters that in the content of this article I cannot mention them all for you. I will summarize everything into one file, you can download this file below!

get post parameter in wordpress

File all parameters of get post in wordpress

Some examples:

Example 1: Get the latest 10 posts in wordpress 

<?php $args = array(
    'posts_per_page' => 10,
    'post_type'   =&gt; 'post',
    'post_status' =&gt; 'publish'
  );
  $the_query = new WP_Query( $args );
?&gt;
<?php if( $the_query->have_posts() ): ?&gt;
<?php while( $the_query->have_posts() ) : $the_query-&gt;the_post(); ?&gt;
  // Thông tin cần lấy của 1 bài viết
<?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span>
</div>

Example 2: Take 5 articles of news category, let’s say news category has id 1

<?php $args = array(
    'posts_per_page'  => 5,
    'post_type'       =&gt; 'post',
    'post_status'     =&gt; 'publish',
    'cat'             =&gt; 1
  );
  $the_query = new WP_Query( $args );
?&gt;
<?php if( $the_query->have_posts() ): ?&gt;
<?php while( $the_query->have_posts() ) : $the_query-&gt;the_post(); ?&gt;
  // Thông tin cần lấy của 1 bài viết
<?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span>
</div>

Example 3: Get 10 random posts in wordpress

<?php $args = array(
    'posts_per_page'  => 10,
    'post_type'       =&gt; 'post',
    'post_status'     =&gt; 'publish',
    'orderby'         =&gt; 'rand'
  );
  $the_query = new WP_Query( $args );
?&gt;
<?php if( $the_query->have_posts() ): ?&gt;
<?php while( $the_query->have_posts() ) : $the_query-&gt;the_post(); ?&gt;
  // Thông tin cần lấy của 1 bài viết
<?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span>
</div>

Summary:

Today I showed you how to get posts in wordpress. This is an important knowledge in wordpress theme programming

If you follow wordpress theme programming , this one cannot be ignored. Hope this article has helped you better understand the get posts in wordpress. Well, you can learn more about the new wp_query web here: => WordPress main document

Good luck with your wordpress learning!

Bài viết liên quan