[英]WordPress dynamic change of posts category
今天是個好日子! 我想知道是否有一種方法可以為所有類別創建一個頁面,並根據單擊的導航菜單項動態更改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 /段,如何在我的代碼中使用它? 我應該在模板文件夾中僅創建category.php文件,還是應該使用其他文件結構來定制類別輸出?
single.php適用於所有類別。 每個帖子都可以使用該模板(除非您創建諸如single- $ template.php之類的模板),並且在代碼中,您可以使用以下https://developer.wordpress.org/reference/functions/get_the_category/從該帖子中獲取類別。
您無需為類別創建每個頁面,您可以創建可用於動態顯示內容的基本文件。 正如您可以在此處看到的wordpress模板層次結構中看到的類別一樣,該類別是默認類別以及默認的post
類型。
具有Wordpress Post
類型的默認類別。
呈現類別存檔索引頁面在WordPress中使用以下路徑:
具有自定義帖子類型的自定義分類
自定義分類法使用的模板文件路徑略有不同:
在您的情況下,可以使用category.php
進行動態類別顯示,除非您的類別不是自定義分類法,否則您將需要使用taxonomy.php
作為基本模板。
您可以使用get_query_var();
功能實現
get_query_var('cat'); >這將返回當前類別ID。
$category = get_category(get_query_var('cat'));
然后您可以按類別ID獲取類別對象,以便獲取cat id, name, slug etc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.