How can I specify a category post on my home page

You really should be using custom posts to do what you’re trying to do, but to answer your question here’s a solution (put this in your functions.php file):

add_shortcode( 'job-posts', 'rt_list_posts_by_category' );
function rt_list_posts_by_category($atts) {

  $a = shortcode_atts( array(
        'link1' => '#',
        'link2' => '#',
        'link3' => '#',
    ), $atts );

  // arguments for function rt_list_posts_by_category

  $taxonomy = 'jobs';

  // WP Query
  $args = array(
    'category_name' => $taxonomy,
    'post_type' => 'post',
    'posts_per_page' => 3,
  );
  $query = new WP_Query( $args );

  $i=1;
  if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 
      if ($i==1) {
           $link= $a['link1'];
         }elseif($i==2) {
           $link= $a['link2'];
         }else{
           $link= $a['link3'];      
         };?>
      <div class="info_Job"><a href="https://wordpress.stackexchange.com/questions/319125/<?php echo $link; ?>">
        <h2><?php the_title();?></h2>
        <?php the_post_thumbnail();?></a>
      </div>
    <?php $i++;
    endwhile;   
    wp_reset_postdata();
  ?>
  <?php else: ?>
    <h2>No posts found</h2>
  <?php endif;
}

Now on the page that you want to list the most recent 3 posts from the jobs category simply put this shortcode:

[job-posts link1="1" link2="2" link3="3"]

There are a few caveats.

This is a start for you, not a finished product.
There is no security on the outputs at all.

For each link above replace what’s in the quote with the link that you want to provide for each of the 3 jobs. ie. If you want the first job to go to www.example.com replace the 1 with http://www.example.com…like so (notice the addition of http:// which is required in this case or it will link to a page on your site):

[job-posts link1="http://www.example.com" link2="2" link3="3"]

It will directly take the user to that link for the 1st post, (no _blank was added)

I didn’t do any checks in case there isn’t a thumbnail either. You can add that if you need to.

Leave a Comment