通过短代码查询存档

时间:2014-04-06 04:16:12

标签: php wordpress function

WordPress档案的查询是什么,我可以将其放入函数中并通过短代码调用...

<?php do_shortcode('[archives]'); ?>

在archive.php里面

找到get_queried_object();,我不知道是不是正确的功能。

而不是

<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
/* Custom Archives Functions Go Below this line */

/* Custom Archives Functions Go Above this line */
</div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?>

我想通过

来称呼它
<?php do_shortcode('[archives]'); ?>

我想制作功能,比如

function archives( $atts, $content = null ) {

if ( have_posts() ) : while ( have_posts() ) : the_post();

$return .= '<div class="archives_posts">';
$return .= '<div class="archives_posts_title">';
$return .= '<a href="'.get_permalink( $id ).'">'.get_the_title($ID).'</a>';
$return .= '</div>';
$return .= '</div>';
endwhile;
endif;
return $return;
}
add_shortcode( 'archives', 'archives' );

2 个答案:

答案 0 :(得分:1)

在主题的functions.php中添加以下代码。

function my_archives($params, $content = null) {

    extract(shortcode_atts(array(
        'type' => 'style1'
    ), $params));

    ob_start();
    ?>
   <div class="archives_posts">       
       <?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 10 ) ); ?>     
   </div>

<?php return ob_get_clean();
}
add_shortcode('archives','my_archives');

您可以根据需要更改参数以列出存档帖子。

使用以下短代码获取存档帖子列表

  1. 在模板中使用<?php echo do_shortcode('[archives]'); ?>
  2. 在帖子/页面中使用[archives]

答案 1 :(得分:0)

将以下行添加到/wp-content/themes/themeName/functions.php

function sc_archive(){  
    return wp_get_archives(); 
} 
add_shortcode('archives','sc_archive');

然后在您的网页中使用它:[archives]

相关问题