【发布时间】:2017-10-02 13:44:10
【问题描述】:
大家好! 我想知道有没有办法为所有类别创建一个页面并根据单击的导航菜单项在 WP_query 中动态更改类别名称,或者我必须为每个类别创建一个单独的页面(在我的情况下有 23 个) ?
菜单:
<?php
$args = array(
'menu' => 'category_nav',
'container' => 'ul',
'container_class' => 'accordion-content',
'container_id' => '',
'menu_class' => 'accordion-content',
'menu_id' => '',
'echo' => true,
'fallback_cb' => false,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'item_spacing' => 'preserve',
'depth' => 0,
'walker' => '',
'theme_location' => ''
);
wp_nav_menu($args); ?>
帖子:
<?php
$args = array(
'nopaging' => true,
'orderby' => 'name',
'category_name' => 'HERE GOES A CATEGORY NAME',
);
$q = new WP_Query($args);
if($q->have_posts()) {
while($q->have_posts()){ $q->next_post();
$post_id = $q->post->ID;
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $post_id ), 'full' );
$title = get_the_title($post_id);
$date = get_the_date('d.m.Y', $post_id);
$content = get_post_field('post_content', $post_id);
$discount = get_post_field('discount', $post_id);
$discount_exists = get_post_meta( $post_id, 'discount', true );
$full_description = get_post_field('full_description', $post_id);
$full_description_exists = get_post_meta( $post_id, 'full_description', true ); ?>
<div class="gallery-item">
<?php echo '<div class="item-img" style="background-image:url(\'' . $thumbnail[0] . '\')"></div>'; ?>
<div class="item-content">
<div class="item-header"> <?php echo $title; ?> </div>
<div class="item-desc"> <?php echo $content; ?> </div>
</div>
<?php
if ( $full_description_exists ) {
?><div class="btn">More</div><?php
} ?>
</div>
<?php
}
}
wp_reset_postdata();
?>
我应该从哪里获得该类别名称/ID/slug,如何在我的代码中使用它?我应该在模板文件夹中只创建 category.php 文件还是应该使用其他文件结构来自定义类别输出?
【问题讨论】:
标签: php wordpress navigation