Display recent post by tag

时间:2018-02-01 作者:ahmet

我有6个相关的职位标签在每个职位。但有些标签只有2到3个帖子。所以在这些帖子中,相关帖子部分只显示了2或3个帖子。

我想用新帖子完成相关帖子(如果不是6篇的话)。

我尝试了很多代码,但如果标签有2个帖子,那么相关帖子只会显示2个帖子。我可以将新帖子添加到6吗?

或者我可以按相同的标签显示最近的邮政订单吗?因此,将有6个帖子,相同的标签将首先显示。

提前感谢!

1 个回复
SO网友:Dharmishtha Patel

<div class="page-loop">

    <?php
      while (have_posts()) : the_post();
        $page_title = strtolower(get_the_title());
        the_title(\'<h1>\',\'</h1>\');
      ?>
        <p><?php the_content(); ?><p>
    <?php endwhile;?>

</div>

<!-- Get the most recent post that has been tagged with the page title -->
<div class="related-posts">

    <?php
      $args = array(
        \'tag\' => $page_title,
        \'posts_per_page\' => 1,
      );
      $query = new WP_Query($args);
      if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
          the_title(\'<h1>\',\'</h1>\');
        ?>
        <p><?php the_content(); ?><p>
    <?php endwhile; else: ?>
      <p>Sorry, no posts with this tag!</p>
    <?php endif; wp_reset_query(); ?>

</div>




/** register the meta box */
函数my\\u theme\\u add\\u meta\\u box(){global$post;add\\u meta\\u box(\'my theme meta\',\'Choose a tag\',\'my\\u theme\\u print\\u page\\u meta\',\'page\',\'normal\',\'high\');}add\\u action(\'add\\u meta\\u box\',\'my\\u theme\\u add\\u meta\\u box\');

/** Add extra meta to the page */
function my_theme_print_page_meta() {
    global $post;
    $page_tags = get_post_meta($post->ID, \'_page-tags\', true);
    ?>
    <label for="page-tags">Add a \'tag\'</label>
    <input type="text" class="page-tags" name="page-tags" value="<?php echo esc_attr($page_tags); ?>" />
<?php
}/**Save post meta*/函数my\\u theme\\u Save\\u custom\\u meta(){全局$post;

// Stops WP from clearing post meta when autosaving
if( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) {
  return $post->ID;
}
if (isset($_POST[\'page-tags\'])) {
    $clean = sanitize_text_field($_POST[\'page-tags\']);
    update_post_meta($post->ID, \'_page-tags\', $clean);
}
}add\\u action(\'save\\u post\',\'my\\u theme\\u save\\u custom\\u meta\');

<div class="page-loop">

<?php
  while (have_posts()) : the_post();
    $page_tags = get_post_meta($post->ID, \'_page-tags\', true);
    the_title(\'<h1>\',\'</h1>\');
  ?>
    <p><?php the_content(); ?><p>
<?php endwhile;?>

$page\\u标签,“posts\\u per\\u page”=>1,)$query=新的WP\\u查询($args);if($query->have\\u posts()):while($query->have\\u posts()):$query->the\\u post();\\u标题(“

”,“

”);?>抱歉,没有带有此标签的帖子!

结束

相关推荐