首页 文章

WordPress博客,只使用当前页面的存档?

提问于
浏览
4

我正在 Build 一个WP网站,它将有一个新闻和博客 .

它们将在单独的页面中,一个用于新闻,一个用于Blog,它们将按类别分开 .

例如,我在“新闻”上有这个代码,它会阻止循环获取帖子:

<?php 
    $uncat = get_cat_ID('uncategorised');
    $uncat2 = get_cat_ID('blog');

    $args = array(
      'posts_per_page' => 3,
      'category__not_in' => array($uncat, $uncat2)

      );
    $loop = new WP_Query( $args );
    while ($loop->have_posts() ) : $loop->the_post();
    ?>
    <div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
      <a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
        <div class="postsize">
          <div class="leftfloat" style="float: left; padding-right:20px;">
            <?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>

          </div>
          <div class="contentfaq">
            <h4><?php the_title(); ?></h3>
              <span class="entry-date-blue"><strong><?php echo get_the_date('d/m/y'); ?></strong></span>

              <?php $trimexcerpt = get_the_excerpt();

              $shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… 
<a href="">Read More ...</a>' ); echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>'; ?> </div> </div> </div> </a> <?php endwhile; ?> <?php wp_reset_query(); ?> </div>

这样可以正常工作,但我也在右侧有“档案”,按发布日期过滤 . 问题是,这将获得新闻和博客的帖子,这会破坏分裂它们的想法 .

有没有办法将它们分开,所以如果用户在存档上点击“2015年3月”,那么它只能从NEWS获得本月的帖子?

这是我目前的Archive.php代码

<?php if (have_posts()) : ?>
  <!-- First, the loop checks whether any posts were discovered with the have_posts() function. -->



  <!-- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating). -->
  <?php while (have_posts()) : the_post(); ?>
  <div class="col-md-12 col-sm-12 col-xs-12">
    <a href="<?php echo get_permalink(); ?>">
    <div class="postsize">
      <div style="float: left; padding-right:20px;">
        <?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>

      </div>
      <h5 class="captext"><?php the_title(); ?></h5>
      <span class="entry-date-orange"><?php echo get_the_date(); ?></span>
      <?php
      foreach((get_the_category()) as $category) { 
        echo ' | ' . $category->cat_name; 
      } 
      ?>

      <p style="margin-top:10px";><?php the_excerpt(); ?></p>
    </div>
  </a>
  </div>
  <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
  <?php endif; ?>

1 回答

  • 0

    试试这个,它会回显列表

    <h2>Archives by Month:</h2>
        <ul>
            <?php wp_get_archives('type=monthly'); ?>
        </ul>
    
        <h2>Archives by Subject:</h2>
        <ul>
             <?php wp_list_categories(); ?>
        </ul>
    

    完整的示例代码

    <?php
    /*
      Template Name: Archives
     */
    get_header(); ?>
    
    <div id="container">
    <div id="content" role="main">
    
     <?php the_post(); ?>
        <h1 class="entry-title"><?php the_title(); ?></h1>
    
        <?php get_search_form(); ?>
    
        <h2>Archives by Month:</h2>
        <ul>
            <?php wp_get_archives('type=monthly'); ?>
        </ul>
    
        <h2>Archives by Subject:</h2>
        <ul>
             <?php wp_list_categories(); ?>
        </ul>
    
       </div><!-- #content -->
     </div><!-- #container -->
    
     <?php get_sidebar(); ?>
     <?php get_footer(); ?>
    

相关问题