0

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;';
    ?>
1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.