使用打开的最新帖子创建自定义帖子类型帖子

时间:2014-04-09 13:38:23

标签: php wordpress

我在faq页面制作手风琴,当我打开faq页面时,我希望它打开最近的帖子,我为它制作了一个自定义的帖子类型,我没有使用手风琴的任何插件我刚刚复制了代码来自 dreamweaver 。 我使用下面的代码: -

<?php $recentPosts = new WP_Query(array('showposts' => 1, 'post_type' => 'FAQ'));
while( $recentPosts->have_posts() ) : 
    $recentPosts->the_post(); ?>

    <div id="Accordion1" class="Accordion" tabindex="0">
          <div class="AccordionPanel">
             <div class="AccordionPanelTab">
             <?php the_title(); ?>
             </div>
             <div class="AccordionPanelContent"> <?php the_content(); ?> </div> 
          </div>
<?php endwhile; ?>   

     </div>

Function.php

add_action('init', 'cptui_register_my_cpt_faq');
function cptui_register_my_cpt_faq() {
register_post_type('faq', array(
'label' => 'FAQ',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'faq', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
'labels' => array (
  'name' => 'FAQ',
  'singular_name' => '',
  'menu_name' => 'FAQ',
  'add_new' => 'Add FAQ',
  'add_new_item' => 'Add New FAQ',
  'edit' => 'Edit',
  'edit_item' => 'Edit FAQ',
  'new_item' => 'New FAQ',
  'view' => 'View FAQ',
  'view_item' => 'View FAQ',
  'search_items' => 'Search FAQ',
  'not_found' => 'No FAQ Found',
  'not_found_in_trash' => 'No FAQ Found in Trash',
  'parent' => 'Parent FAQ',
)
) ); }

当我使用硬编码时它正常工作: -

<div id="Accordion1" class="Accordion" tabindex="0">
          <div class="AccordionPanel">
             <div class="AccordionPanelTab">
             title
             </div>
             <div class="AccordionPanelContent">content  </div> 
         </div>



          <div class="AccordionPanel">
             <div class="AccordionPanelTab">
             title
             </div>
             <div class="AccordionPanelContent">content </div> 
         </div>
   </div>           

我该如何更改?

2 个答案:

答案 0 :(得分:0)

只需单独替换此代码,

<div id="Accordion1" class="Accordion" tabindex="0">
<?php $recentPosts = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'faq'));
while( $recentPosts->have_posts() ) : 
    $recentPosts->the_post(); ?>
      <div class="AccordionPanel">
         <div class="AccordionPanelTab">
            <?php the_title(); ?>
         </div>
         <div class="AccordionPanelContent"> <?php the_content(); ?> </div> 
      </div>
<?php endwhile; ?> 
</div>

将其放入模板文件中。这会奏效。 showposts是较旧的..所以尝试使用posts_per_page ..

答案 1 :(得分:0)

我刚从accordion推出divloop并且轻松完成这件事并不是什么大不了的事

<div id="Accordion1" class="Accordion" tabindex="0">   
      <?php $args = array( 'post_type' => 'FAQ' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>    
          <div class="AccordionPanel">
             <div class="AccordionPanelTab">
             <?php the_title(); ?>
             </div>
             <div class="AccordionPanelContent"> <?php the_content(); ?> </div> 
         </div>
    <?php endwhile; ?>            
   </div>  <!--Accordion1-->