我在我的类别中使用以下代码。php文件。我想让它显示我的自定义贴子类型“产品”的缩略图和摘录。
目前,它正在显示帖子标题,所有帖子的缩略图。然而,它只显示了第一篇文章的摘录。
<?php
/*
Template Name: Category
*/
?>
<?php get_header(\'\'); ?>
<?php /* Products sidebar */
if ( !function_exists(\'dynamic_sidebar\') || !dynamic_sidebar(\'product_sidebar\') ) : ?><?php endif; ?>
<div id="main">
<?php if ( function_exists(\'yoast_breadcrumb\') ) {
yoast_breadcrumb(\'<p id="breadcrumbs">\',\'</p>\');
} ?>
<div id="post">
<div id="productlist">
<?php
if (!category_has_children()) {?>
<?php foreach(get_the_category() as $category) {
$cat = $category->cat_ID; }?>
<?php query_posts(\'post_type=Product&cat=\' .$cat .\'&order=ASC\'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><?php the_post_thumbnail(array(100,100)); ?>
<?php the_excerpt(); ?></p>
<?php endwhile;?>
<?php endif; ?>
<?php get_template_part(\'loop\', \'index\');
} else {?></div><!-- /productlist -->
<div id="productcategories">
<?php
$cat_id = get_query_var(\'cat\');
$catlist = get_categories(\'hide_empty=0&child_of=\' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo \'<h1><a href="\' . get_category_link( $categories_item->term_id ) . \'" title="\' . sprintf( __( "View all products in %s" ), $categories_item->name ) . \'" \' . \'>\' . $categories_item->name.\'</a> </h1> \';
echo \'<p>\'. $categories_item->description . \'</p>\';
}
echo "</ul>";
}
?>
</div>
</div><!-- /post -->
</div><!-- /main -->
<?php get_footer(); ?>
这是我的职责。还有php//Display product categories
function category_has_children() {
global $wpdb;
$term = get_queried_object();
$category_children_check = $wpdb->get_results(" SELECT * FROM wp_term_taxonomy WHERE parent = \'$term->term_id\' ");
if ($category_children_check) {
return true;
} else {
return false;
}
}
有人知道为什么吗?What i\'m trying to achieve:我网站上的主要分类页面是“产品”。在此页面上,它显示父类别的子类别。
转到子类别页面将显示更多子类别,如果没有子类别,则显示我的自定义帖子类型“Product”。
我使用的代码是这样的。如果类别有子项,则显示它们,否则显示产品。
Update:我一直在试验,发现了一些东西。它使用以下代码和不同的自定义帖子类型“News”,显示所有帖子的摘录。
<?php query_posts(array(\'post_type\'=>\'News\')); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<p><?php the_time(\'j M. Y\'); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt();?></p>
<?php endwhile;?>
<?php endif; ?>
如果我将“新闻”更改为“产品”,则只会显示创建的第一个产品的摘录。我能想到的这两种自定义帖子类型之间的唯一区别是“新闻”是帖子类型,“产品”是页面类型。
我不知道为什么这不起作用。此外,我还没有为他们中的任何一个写过摘录,因此该摘录是Wordpress自动提供的。