我怎么才能在模板文件中只显示几个帖子

时间:2013-01-01 作者:Batman

伙计们,我的存档主题板我有三个主题:-海报复杂简单

如何显示特定数量的arhive主题帖子:

例如:对于我的海报,我想显示24篇帖子(只有海报和标题),对于综合体,我想显示12篇帖子(全部是摘录…),用于显示30(仅标题)的简单信息

我有存档的树主题。php

档案文件php

<div class="breadcrumbs">
    <?php if(function_exists(\'bcn_display\'))
    {
        bcn_display();
    }?>
</div>

<div class="art-content-film">
<div class="box-lay"> 
                <a href="?afisare=complex" class="grid" title="Grid"><span></span></a>  
                <a href="?afisare=poster" class="poster" title="Poster"><span></span></a> 
                <a href="?afisare=simple" class="list" title="List"><span></span></a> 
</div> 
<?php
$view = \'poster\';
$mode = stripslashes( $_GET["afisare"] );
$modes = array(\'complex\', \'simple\', \'poster\');
if(in_array($mode, $modes)) $view = $mode;
get_template_part(\'persoane\', $view);
?>
</div>

<div class="art-sidebar1"> 
<?php include (TEMPLATEPATH . \'/sidebar/actors.php\'); ?>
<?php include (TEMPLATEPATH . \'/publicitate/ad300.php\'); ?>
</div>
persoane海报。php

<div class="art-Post">
    <div class="art-Post-tl"></div>
    <div class="art-Post-tr"></div>
    <div class="art-Post-bl"></div>
    <div class="art-Post-br"></div>
    <div class="art-Post-tc"></div>
    <div class="art-Post-bc"></div>
    <div class="art-Post-cl"></div>
    <div class="art-Post-cr"></div>
    <div class="art-Post-cc"></div>
    <div class="art-Post-body">
<?php is_tag(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<div class="bposter">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__(\'Film %s\'), the_title_attribute(\'echo=0\')); ?>">
<img src="/scripts/timthumb.php?src=<?php the_field(\'img_actor\'); ?>&h=120&w=90&zc=1" alt="" title="<?php the_title(); ?>" /></a>
<div class="bpt"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__(\'Permanent Link to %s\', \'kubrick\'), the_title_attribute(\'echo=0\')); ?>">
<?php the_title(); ?>
</a></div>
</div>
<?php endwhile; ?>
<div class="cleared"></div>

</div>
</div>

<div class="nav">
<?php if(function_exists(\'wp_paginate\')) {wp_paginate();} ?>
</div>
<?php if ($prev_link || $next_link): ?>

<?php endif; ?>

<?php else : ?>
<div class="art-Post">
    <div class="art-Post-tl"></div>
    <div class="art-Post-tr"></div>
    <div class="art-Post-bl"></div>
    <div class="art-Post-br"></div>
    <div class="art-Post-tc"></div>
    <div class="art-Post-bc"></div>
    <div class="art-Post-cl"></div>
    <div class="art-Post-cr"></div>
    <div class="art-Post-cc"></div>
    <div class="art-Post-body">
<div class="art-Post-inner art-article">

<div class="art-PostContent">

<?php
    if ( is_category() ) { // If this is a category archive
        printf("<h2 class=\'center\'>".__("Sorry, but there aren\'t any posts in the %s category yet.", "kubrick").\'</h2>\', single_cat_title(\'\',false));
    } else if ( is_date() ) { // If this is a date archive
        echo(\'<h2>\'.__("Sorry, but there aren\'t any posts with this date.", "kubrick").\'</h2>\');
    } else if ( is_author() ) { // If this is a category archive
        $userdata = get_userdatabylogin(get_query_var(\'author_name\'));
        printf("<h2 class=\'center\'>".__("Sorry, but there aren\'t any posts by %s yet.", "kubrick")."</h2>", $userdata->display_name);
    } else {
        echo("<h2 class=\'center\'>".__(\'No posts found.\', \'kubrick\').\'</h2>\');
    }
    if(function_exists(\'get_search_form\')) get_search_form();
?>

</div>
<div class="cleared"></div>


</div>

        <div class="cleared"></div>
    </div>
</div>

<?php endif; ?>

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

好啊您正在根据$_GET 已添加到URL的参数archive.php

$view = \'poster\';
$mode = stripslashes( $_GET["afisare"] );
$modes = array(\'complex\', \'simple\', \'poster\');
if(in_array($mode, $modes)) $view = $mode;
get_template_part(\'persoane\', $view);
您可以更改将显示的帖子数量,但这样做太晚了。

未经测试,但我会尝试pre_get_posts.

function toggle_modes($query) {
  global $_GET;
  if (isset($_GET["afisare"])) {
    $modes = array(
      \'complex\' => 12, 
      \'simple\' => 30, 
      \'poster\' => 24
    );
    if (isset($modes[$_GET["afisare"]])) {
      $query->set(\'posts_per_page\',$modes[$_GET["afisare"]]);
    }
  }
}
add_action(\'pre_get_posts\',\'toggle_modes\');
您的URL参数必须位于发送到此页面的每个请求中,否则分页将不同步。

这是从你现在的位置到你想要的位置的最简单的方式。这可能不是最好的方式。你可以这样想:Passing and retrieving query vars in wordpress

结束

相关推荐

显示Archives.php中的所有自定义帖子类型

我该怎么做?archive.php 只有以下内容:wp_get_archives(\'type=monthly\'); 以及wp_get_archives() 没有显示所有帖子类型的参数。我也认为archive-[post_type].php 不是我要找的,因为我希望所有帖子类型都显示在一个归档页面中。谢谢W