如何显示两个随机发布的部分,每个部分位于各自的类别下

时间:2015-06-11 作者:barb
 <?php get_header(); ?>

 <div id="containerHOME">
<div id="left-div">
<!--Begin Sidebar-->
<div id="left-inside">
  <?php if (get_option(\'earthlytouch_featured\') == \'on\') get_template_part(\'includes/featured\'); ?>
  <div class="clear">&nbsp;</div>

  <div class="home-squares">
    <div class="home-headings">
      <?php esc_html_e(\'Whats New?\',\'EarthlyTouch\'); ?>
    </div>
    <?php $args = array(
      \'category_name\' => \'whats\', 
      \'orderby\' => \'DESC\',
      \'ignore_sticky_posts\' => true,
      \'posts_per_page\' => 3 
    );
    $qry = new WP_Query($args);
    if ($qry->have_posts()) {
      while ($qry->have_posts() ) { 
        get_template_part(\'content\',\'blocks\');
      }
      }?>
    <!-- end .home-squares -->
    <div class="home-squares">
      <div class="home-headings">
        <?php esc_html_e(\'Recipes\',\'EarthlyTouch\'); ?>
      </div>
      <?php $args = array(
      \'category_name\' => \'recipes\', 
      \'orderby\' => \'DESC\',
      \'ignore_sticky_posts\' => true, 
      \'posts_per_page\' => 3 
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
      while ($query->have_posts() ) { 
        get_template_part(\'content\',\'blocks\');
      }
    } ?>
      <!-- end .home-squares -->
      <?php }; ?>  // if I take this out I get an error down at the bottom of the page
      <!-- movie -->
      <div class="movie"> <a href="http://www.youtube.com/embed/tojlpK9zFPs"> <img src="images/video-front.jpg" width="530" height="299" alt="video" class="popmake-2673" /></a> </div>
    </div>
    <!-- end movie --> 
    <!-- end #left-inside --> 

    <!--Begin Sidebar-->
    <?php get_sidebar(); ?>
    <!--End Sidebar-->
    <div id="sup-hold">
      <div id="supplementary" style="background-color:#FFFFFF;">
        <?php if (!function_exists(\'dynamic_sidebar\') || !dynamic_sidebar(\'Header Widget Area\')) :
endif; ?>
      </div>
      <div id="mc-mobilecause-widget"> <a href="//www.mobilecause.com/feature/subscription-widget" id="powered-by-mobilecause">Mobile messaging powered by Mobilecause</a><script>!function(d,s){var s=d.createElement("script"),h=(document.head||d.getElementsByTagName(\'head\')[0]);s.src="https://app.mobilecause.com/public/messaging_widgets/opvd3d/source";h.appendChild(s);}(document);</script> 
      </div>
      <div id="footer"> Copyright &copy;
        <?php
echo date("Y");
?>
        JustOneTree.org All Rights Reserved | <a href="/contact">Contact Us</a> | <a href="/terms">Policies and Terms of Use</a> | <a target="_blank" href="http://www.boplinger.com">webmaster: boplinger.com</a></div>
      <!-- end #footer --> 
  </div>
    <!-- end #footer -->
    <?php get_template_part(\'includes/scripts\'); ?>
    <?php wp_footer(); ?>
  </div> // error here
  <!-- end #sup-hold --> 
</div>
<!-- end #container -->
1 个回复
SO网友:s_ha_dum

相关代码如下:

query_posts("cat=25&orderby=DESCd&showposts=3&$randomArticlesNum&caller_get_posts=1");
这就是拉动帖子的查询,这就是您想要更改的内容。

您以前的主题生成器也做了一些不应该做的事情--使用query_posts(). 而且both showposts and caller_get_posts have been replaced. 这是非常旧的代码,或者您以前的编程人员没有跟上对内核的更改。让我们清理一下:

$args = array(
  \'cat\' => 25, 
  \'orderby\' => \'DESC\',
  \'ignore_sticky_posts\' => true, // used to be caller_get_posts
  \'posts_per_page\' => 3 // used to be showposts
);
$qry = new WP_Query($args);
if ($qry->have_posts()) {
  while ($qry->have_posts() ) { 
    $width = 40;
    $height = 40;
    $classtext = \'no-border\';
    $titletext = get_the_title();
    $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext);
    $thumb = $thumbnail["thumb"]; ?>
    <div class="random"><?php 
      $width = 40;
      if($thumb != \'\') { ?>
        <div class="random-image"> "><?php 
          print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
        </div><?php 
      } 
      // something is broken here. the start of the following tag is missing
      ?>" rel="bookmark" title="<?php printf(esc_attr__(\'Permanent Link to %s\',\'EarthlyTouch\'), the_title()) ?>"><?php 
        truncate_title(40);
        truncate_post(50); ?>
    </div>
    <!-- end .random --><?php
  }
}
我交换了query_posts() 对于新的WP_Query 对象,我重新排序以提高可读性,一次快速消除尽可能多的PHP标记垃圾邮件。

我不知道是什么$randomArticlesNum 应该这样做,但查看参数列表query_posts() 我看不出它到底能做什么。

您希望在下一个循环中再次执行相同的操作,但更改cat 论点照原样,两个循环都有cat=25 因此,这也不可能奏效。

你的标签也坏了。请参见代码注释。

另一个技巧是:两个循环的代码看起来是相同的,但您在两个地方维护相同的代码。这是一个令人头痛的问题,而且容易出错。将大部分循环代码移动到另一个文件并通过get_template_part():

$args = array(
  \'cat\' => 25, 
  \'orderby\' => \'DESC\',
  \'ignore_sticky_posts\' => true, // used to be caller_get_posts
  \'posts_per_page\' => 3 // used to be showposts
);
$qry = new WP_Query($args);
if ($qry->have_posts()) {
  while ($qry->have_posts() ) { 
    get_template_part(\'content\',\'blocks\');
  }
}
将加载名为content-blocks.php 从主题根目录——与主题的样式表相同的目录和functions.php.

我看了一下你所做的编辑,你没有完全理解。下面是一些代码和注释:

// $randomArticlesNum does nothing, as noted previously. I am not even sure what it was supposed to do.    
// You can remove it
$randomArticlesNum = (int) get_option(\'earthlytouch_randomposts_num\'); 
$args = array(
  // use one or the other, but not both, of the following and delete the one you don\'t use
  // Your original code use `cat` and the ID
  \'cat\' => 123, // \'cat\' needs to be an ID-- a number-- not a word, 
  \'category_name\' => \'whats\', // for a word, use \'category_name\' and quote the value
  \'orderby\' => \'DESC\',
  \'ignore_sticky_posts\' => true, // used to be caller_get_posts
  \'posts_per_page\' => 3 // used to be showposts
);
$query = new WP_Query($args);
if ($query->have_posts()) {
  while ($query->have_posts() ) { 
    get_template_part(\'content\',\'blocks\');
  }
} // you were missing the PHP closing tag --> ?> 
两个循环中都存在相同的错误。此外,在第二个括号之后,还有一个假结束括号:

<!-- end .home-squares -->

      <?php }; ?>
删除它:

<!-- end .home-squares -->
另外,给自己找一个像样的代码编辑器——比如Textwrangler(在Mac电脑上,我猜你是:)或Notepad++。

我编辑并重新格式化了您的模板:

<?php
get_header(); ?>

<div id="containerHOME">
  <div id="left-div">
  <!--Begin Sidebar-->
    <div id="left-inside"><?php 
      if (get_option(\'earthlytouch_featured\') == \'on\') {
        get_template_part(\'includes/featured\'); 
      } ?>
      <div class="clear">&nbsp;</div>
      <div class="home-squares">
        <div class="home-headings"><?php 
          esc_html_e(\'Whats New?\',\'EarthlyTouch\'); ?>
        </div><?php 
        $args = array(
          \'category_name\' => \'whats\', 
          \'orderby\' => \'DESC\',
          \'ignore_sticky_posts\' => true,
          \'posts_per_page\' => 3 
        );
        $qry = new WP_Query($args);
        if ($qry->have_posts()) {
          while ($qry->have_posts() ) { 
            get_template_part(\'content\',\'blocks\');
          }
        } ?>
        <!-- end .home-squares -->
        <div class="home-squares">
          <div class="home-headings">
            <?php esc_html_e(\'Recipes\',\'EarthlyTouch\'); ?>
          </div>
          <?php 
          $args = array(
            \'category_name\' => \'recipes\', 
            \'orderby\' => \'DESC\',
            \'ignore_sticky_posts\' => true, 
            \'posts_per_page\' => 3 
          );
          $query = new WP_Query($args);
          if ($query->have_posts()) {
            while ($query->have_posts() ) { 
              get_template_part(\'content\',\'blocks\');
            }
          } ?>
          <!-- end .home-squares -->
          <!-- movie -->
          <div class="movie"> <a href="http://www.youtube.com/embed/tojlpK9zFPs"> <img src="images/video-front.jpg" width="530" height="299" alt="video" class="popmake-2673" /></a> </div>
        </div>
        <!-- end movie --> 
        <!-- end #left-inside --> 

        <!--Begin Sidebar--><?php 
        get_sidebar(); ?>
        <!--End Sidebar-->
        <div id="sup-hold">
          <div id="supplementary" style="background-color:#FFFFFF;"><?php 
            if (!function_exists(\'dynamic_sidebar\') || !dynamic_sidebar(\'Header Widget Area\')) : endif; ?>
          </div>
          <div id="mc-mobilecause-widget"> <a href="//www.mobilecause.com/feature/subscription-widget" id="powered-by-mobilecause">Mobile messaging powered by Mobilecause</a><script>!function(d,s){var s=d.createElement("script"),h=(document.head||d.getElementsByTagName(\'head\')[0]);s.src="https://app.mobilecause.com/public/messaging_widgets/opvd3d/source";h.appendChild(s);}(document);</script> 
          </div>
          <div id="footer"> Copyright &copy; <?php echo date("Y"); ?> JustOneTree.org All Rights Reserved | <a href="/contact">Contact Us</a> | <a href="/terms">Policies and Terms of Use</a> | <a target="_blank" href="http://www.boplinger.com">webmaster: boplinger.com</a></div>
          <!-- end #footer --> 
      </div>
    <!-- end #footer --> <?php 
      get_template_part(\'includes/scripts\'); 
      wp_footer(); ?>
  </div> 
  <!-- end #sup-hold --> 
</div>
<!-- end #container -->
我不会出错,但如果不建立完整的并行环境,很难测试这样的东西。我注意到您的模板没有使用get_footer(). 为什么不呢?

结束

相关推荐

expire wordpress user posts

我正在尝试制作分类WordPress主题,我需要让用户的帖子过期日期加上在帖子中显示该日期。用户只允许以帖子类型发布自己的帖子,而不允许发布普通WordPress帖子。有人知道我怎么做吗?Update:根据对这个问题的回答,到目前为止,我不得不说,通过meta post为帖子类型指定过期日期并不是我想要的,因为用户可能会更改我想要的30天日期(意味着发布帖子后的30天)永久删除帖子,并在后端自动将日期添加到帖子中,而不是手动与管理员或作者联系。希望我的意思现在更清楚。特别感谢:@sakibmoon。谢谢