admin管理员组文章数量:1294305
I'm new to php and wordpress. I am building an archive template that has to make a masonry gallery with all the posts of the current category. I have to replace the 'edito' with a dynamic piece of code that give me the actual category name. Can you help me to figure out how to do it? Thanks !
<?php
$args = array(
'category_name' => 'edito',
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'DESC',
);
$child_query = new WP_Query( $args );
$first = true;
$same_date = 'dd';
while ( $child_query->have_posts() ) : $child_query->the_post();
$divclass = '';
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
$thumb_w = $thumb_url_array[1];
$thumb_h = $thumb_url_array[2];
$excerpt = get_the_excerpt();
$divstyle = 'background-image:url(' . $thumb_url .');height:' . $thumb_h . 'px;';
?>
I'm new to php and wordpress. I am building an archive template that has to make a masonry gallery with all the posts of the current category. I have to replace the 'edito' with a dynamic piece of code that give me the actual category name. Can you help me to figure out how to do it? Thanks !
<?php
$args = array(
'category_name' => 'edito',
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'DESC',
);
$child_query = new WP_Query( $args );
$first = true;
$same_date = 'dd';
while ( $child_query->have_posts() ) : $child_query->the_post();
$divclass = '';
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
$thumb_w = $thumb_url_array[1];
$thumb_h = $thumb_url_array[2];
$excerpt = get_the_excerpt();
$divstyle = 'background-image:url(' . $thumb_url .');height:' . $thumb_h . 'px;';
?>
Share
Improve this question
asked Apr 23, 2021 at 15:11
sandrosandro
1
1
- You are building a term archive? You know WP does this out of the box, right? developer.wordpress/themes/template-files-section/… – vancoder Commented Apr 23, 2021 at 15:38
1 Answer
Reset to default 0You can try something like:
if ( is_category() ) {
$cat = get_category( get_query_var( 'cat' ) );
// $cat_id = $cat->cat_ID;
$cat_name = $cat->name;
// $cat_slug = $cat->slug;
$args = array(
'category_name' => $cat_name,
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'DESC',
);
...
The function you are looking for is get_category, as well as is_category to check that the query is for an existing category archive page.
本文标签:
版权声明:本文标题:categories - How do I make an array to get the category name, dynamically, in an archive template? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741598134a2387534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论