繁体   English   中英

更改页面模板以在 Wordpress 中显示自定义帖子类型

[英]Change page template to show Custom Post Type in Wordpress

我有一个很久以前由其他人开发的页面模板,它根据他们所在的类别在网格中显示我的所有帖子。

现在,我想为我的自定义帖子类型创建一个这样的页面,但我无法让它工作。 我尝试更换

<?php if (have_posts()) : while (have_posts()) : the_post();?>

<?php $args = array('post_type'=>array('shopitems')); query_posts($args); if ( 
have_posts() ) : while ( have_posts() ) : the_post();?>`

这似乎是我所看到的任何地方的首选答案。 但对我来说,结果是空的。 在原始页面模板下方,我想对其进行更改,以便加载“shopitems”自定义帖子类型。

非常感谢任何帮助或朝着正确方向点头!

<?php get_header(); ?>
<div class="content-sidebar-wrap">
    <?php if (have_posts()) : while (have_posts()) : the_post();?>
  <?php $category_list= get_the_content(); ?>
<?php endwhile; endif; ?>
    <div id="content" class="post-category page">
    <?php $category_name=explode(',', $category_list);
     $catIDs="";
        foreach ($category_name as $cat_name) {
            $catIDs.= get_cat_ID(trim($cat_name)).",";
          } 
          $catIDs=trim($catIDs,","); 
          $i=0;
    ?>  
        <?php $thePosts= query_posts('cat='.$catIDs.'&post_status=publish&posts_per_page=-1'); ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
     <?php if (has_post_thumbnail( $post->ID ) ) { ?>
        <?php // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?>
       <?php if($i%4==0) { ?>
       <div class="clear"></div>
       <?php } $i++; ?>
      <div class="thumb">
        <a href="<?php the_permalink(); ?>">
         <img alt="" src="<?php echo $image[0]; ?>" class="thumb-img"> 
         <span class="thumb-title">
            <?php 
            $second_name = get_field('second_name');
            if( $second_name && !empty($second_name))
                echo $second_name;
            else
            the_title(); 
            ?>
         </span> 
        </a>
      </div>
        <?php } endwhile; else: endif; ?>
    </div>
</div>
<?php get_footer(); ?>

只是您需要像下面一样修改您的查询。

<?php 
$args = array(
           'post_type'=>'shopitems'
        );
 $query = new WP_Query($args);
if ($query->have_posts() ) : 
while ( $query->have_posts() ) : $query->the_post();
//do your code here

endwhile;

else:
  // No posts found
endif;


?>`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM