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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

本文标签: