【问题标题】:Wordpress only post 1 post from each categoryWordpress 仅发布每个类别的 1 个帖子
【发布时间】:2012-06-06 12:06:12
【问题描述】:

我有这个网站:http://n0rd.no/testing/,我尝试让每个播客在首页上发布一篇文章,而不是在该类别中发布一篇公关文章。

我在这个网站上找到了how do i get only 1 post from each category in wordpress,但不知道如何使用我的代码来完成这项工作。

谁能帮帮我?

<?php /* Template Name: category view */ ?>
<?php get_header(); ?>

<?php
    // Determine column/image size front page
    $cols = '3';
    $cols = get_option('woo_cols');
    $r1 = 3; $r2 = 2;
    if (get_option('woo_ratio1')) $r1 = get_option('woo_ratio1');
    if (get_option('woo_ratio2')) $r2 = get_option('woo_ratio2');
    if ( $cols == "1") {
        $class = "one"; $width = 940; $height = $width / $r1 * $r2;
    } elseif ( $cols == 2 ) {
        $class = "two"; $width = 460; 
    } elseif ( $cols == 3 ) {
        $class = "three"; $width = 300; 
    } elseif ( $cols == 4 ) {
        $class = "four"; $width = 220; 
    } elseif ( $cols == 5 ) {
        $class = "five"; $width = 172; 
    }
    $height = $width / $r1 * $r2;   
    $height = round($height);
?>

    <!-- Content Starts -->
    <div class="content">

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('cat=83'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); $count++; ?>

        <!-- Post Starts -->
        <div class="bg-photo-thumb <?php echo $class; ?>-col<?php if ($count == $cols) { echo ' last'; $count = 0; } ?>">
        <div class="photo-thumb"><?php ciii_category_images(); ?><h2 class="cattitle"><?php 
$category = get_the_category(); 
//print_r($category );
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'" class="catname">'.$category[0]->cat_name.'</a>';
}
?>
</h2><br clear="all" />
<h3 class="catdisc"><?php echo $category[0]->category_description;  ?></h3>

<?php
if (strpos($_SERVER[HTTP_REFERER], "twitter.com") == true) {
    echo "Hello Twitter User!";
}
?>
            <div class="fix"></div>

        </div>
        </div>
        <!-- Post Ends -->

    <?php endwhile; else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.', 'woothemes' ) ?></p>
    <?php endif; ?>  

        <div class="fix"></div>
        <div class="more_entries">
            <?php if (function_exists('wp_pagenavi')) wp_pagenavi(); else { ?>
            <div class="alignleft bg"><?php previous_posts_link(__('&laquo; Newer Entries ', 'woothemes' )) ?></div>
            <div class="alignright bg"><?php next_posts_link(__(' Older Entries &raquo;', 'woothemes' )) ?></div>
            <br class="fix" />
            <?php } ?> 
        </div>      
        <div class="fix"></div>

    </div><!-- Content Ends -->

<?php get_footer(); ?>

更新 - 我现在使用以下代码让它工作:

<?php /* Template Name: category view */ ?>
<?php get_header(); ?>

<?php
    // Determine column/image size front page
    $cols = '3';
    $cols = get_option('woo_cols');
    $r1 = 3; $r2 = 2;
    if (get_option('woo_ratio1')) $r1 = get_option('woo_ratio1');
    if (get_option('woo_ratio2')) $r2 = get_option('woo_ratio2');
    if ( $cols == "1") {
        $class = "one"; $width = 940; $height = $width / $r1 * $r2;
    } elseif ( $cols == 2 ) {
        $class = "two"; $width = 460; 
    } elseif ( $cols == 3 ) {
        $class = "three"; $width = 300; 
    } elseif ( $cols == 4 ) {
        $class = "four"; $width = 220; 
    } elseif ( $cols == 5 ) {
        $class = "five"; $width = 172; 
    }
    $height = $width / $r1 * $r2;   
    $height = round($height);
?>
    <div class="content">
<?php

$news_cat_ID = get_cat_ID('News'); 
$news_cats   = get_categories("parent=$news_cat_ID");
$news_query  = new WP_Query();

foreach ($news_cats as $news_cat):
?>

<?php $news_query->query('posts_per_page=1&cat=' . $news_cat->term_id); ?>
<?php if ($news_query->have_posts()): $news_query->the_post(); ?>

        <div class="post">
        <div class="bg-photo-thumb <?php echo $class; ?>-col<?php if ($count == $cols) { echo ' last'; $count = 0; } ?>">
        <div class="photo-thumb"><?php ciii_category_images(); ?><h2 class="cattitle"><?php 
$category = get_the_category(); 
//print_r($category );
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'" class="catname">'.$category[0]->cat_name.'</a>';
}
?>
</h2><br clear="all" />
<h3 class="catdisc"><?php echo $category[0]->category_description;  ?></h3>

<?php
if (strpos($_SERVER[HTTP_REFERER], "twitter.com") == true) {
    echo "Hello Twitter User!";
}
?>
            <div class="fix"></div>

        </div>
        </div>
            <!-- do whatever you else you want that you can do in a normal loop -->
        </div>  

<?php endif; ?>

<?php endforeach; ?>
</div>

【问题讨论】:

    标签: wordpress posts wp-list-categories


    【解决方案1】:

    我用这个来修复它:

    <?php /* Template Name: category view */ ?>
    <?php get_header(); ?>
    
    <?php
        // Determine column/image size front page
        $cols = '3';
        $cols = get_option('woo_cols');
        $r1 = 3; $r2 = 2;
        if (get_option('woo_ratio1')) $r1 = get_option('woo_ratio1');
        if (get_option('woo_ratio2')) $r2 = get_option('woo_ratio2');
        if ( $cols == "1") {
            $class = "one"; $width = 940; $height = $width / $r1 * $r2;
        } elseif ( $cols == 2 ) {
            $class = "two"; $width = 460; 
        } elseif ( $cols == 3 ) {
            $class = "three"; $width = 300; 
        } elseif ( $cols == 4 ) {
            $class = "four"; $width = 220; 
        } elseif ( $cols == 5 ) {
            $class = "five"; $width = 172; 
        }
        $height = $width / $r1 * $r2;   
        $height = round($height);
    ?>
        <div class="content">
    <?php
    
    $news_cat_ID = get_cat_ID('News'); 
    $news_cats   = get_categories("parent=$news_cat_ID");
    $news_query  = new WP_Query();
    
    foreach ($news_cats as $news_cat):
    ?>
    
    <?php $news_query->query('posts_per_page=1&cat=' . $news_cat->term_id); ?>
    <?php if ($news_query->have_posts()): $news_query->the_post(); ?>
    
            <div class="post">
            <div class="bg-photo-thumb <?php echo $class; ?>-col<?php if ($count == $cols) { echo ' last'; $count = 0; } ?>">
            <div class="photo-thumb"><?php ciii_category_images(); ?><h2 class="cattitle"><?php 
    $category = get_the_category(); 
    //print_r($category );
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'" class="catname">'.$category[0]->cat_name.'</a>';
    }
    ?>
    </h2><br clear="all" />
    <h3 class="catdisc"><?php echo $category[0]->category_description;  ?></h3>
    
    <?php
    if (strpos($_SERVER[HTTP_REFERER], "twitter.com") == true) {
        echo "Hello Twitter User!";
    }
    ?>
                <div class="fix"></div>
    
            </div>
            </div>
                <!-- do whatever you else you want that you can do in a normal loop -->
            </div>  
    
    <?php endif; ?>
    
    <?php endforeach; ?>
    </div>
    

    【讨论】:

      【解决方案2】:

      也许我错了,但你尝试使用 insted your:

      <div class="content">
      
      <?php
      
      $cat_ID = get_cat_ID('News'); 
      $cats   = get_categories("parent=$news_cat_ID");
      $main_query  = new WP_Query();
      $count = 0;
      
      foreach ($cats as $cat):
         $news_query->query('posts_per_page=1&cat=' . $cat->term_id);
         if ($main_query->have_posts()): 
          $count++; 
          $main_query->the_post(); ?>
              <!-- Post Starts -->
              <div class="bg-photo-thumb <?php echo $class; ?>-col<?php if ($count == $cols) { echo ' last'; $count = 0; } ?>">
              <div class="photo-thumb"><?php ciii_category_images(); ?>
              <h2 class="cattitle">
              <?php $category = get_the_category(); 
              if($category[0]){
                  echo '<a href="'.get_category_link($category[0]->term_id ).'" class="catname">'.$category[0]->cat_name.'</a>';
              }?>
              </h2>
          <br clear="all" />
          <h3 class="catdisc"><?php echo $category[0]->category_description;  ?></h3>
          <?php
          if (strpos($_SERVER[HTTP_REFERER], "twitter.com") == true) {
              echo "Hello Twitter User!";
          }
          ?>
              <div class="fix"></div>
              </div>
              </div>
              <!-- Post Ends -->
         <?php endif; ?>
      
      <?php endforeach; ?>
      <div class="fix"></div>
              <div class="more_entries">
                  <?php if (function_exists('wp_pagenavi')) wp_pagenavi(); else { ?>
                  <div class="alignleft bg"><?php previous_posts_link(__('&laquo; Newer Entries ', 'woothemes' )) ?></div>
                  <div class="alignright bg"><?php next_posts_link(__(' Older Entries &raquo;', 'woothemes' )) ?></div>
                  <br class="fix" />
                  <?php } ?> 
              </div>      
              <div class="fix"></div>
      
          </div>
      

      【讨论】:

      • 感谢您的回复,但似乎也无法正常工作。我只是得到标题,没有帖子。如果我想让它只在类别 83 上工作,我该怎么办?
      • 您只想要 83 类的 1 个帖子还是想要 83 的所有子类?
      • 我想要所有子类别中的一个。但现在开始工作了。更新了第一篇文章。
      猜你喜欢
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2011-12-03
      • 2016-02-21
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多