• Resolved ivanisevic82

    (@ivanisevic82)


    Hello everyone, I hope you can help me find a solution for the problem described below.

    I wanted to create a page where to display the posts grouped by each single category.

    So I created a “category.php” file and using the following code, I can group all the posts of the category related to the “slug” that I insert (in my case, for example, I will use the slug “my-category“).

    To change the category, just act on the line ‘category_name’ => ‘my-category’, inserting the slug of the category you want to view.

    <?php
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => 'my-category',
        'posts_per_page' => 5,
    );
    $arr_posts = new WP_Query( $args );
    
    if ( $arr_posts->have_posts() ) :
    
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php
                if ( has_post_thumbnail() ) :
                    the_post_thumbnail();
                endif;
                ?>
    
    
                        <h2><a href="<?php the_permalink() ?>">
        <?php the_title(); ?></a></h2>
                    <div class="Time_Category"><?php the_time('j M Y') ?>&nbsp | &nbsp<?php the_category(', '); ?></div>
                    <div class="riassunto_content"><?php the_excerpt(); ?></div>
                    <a href="<?php the_permalink() ?>" class="leggi_articoli">
        Leggi di più...</a>
            </article>
            <?php
        endwhile;
    endif;
    ?>  

    Now I would like to replace the “my-category” value with something that calls the slug of the <span style=”text-decoration: underline;”>current</span> category page.

    In this way, depending on the category that has been selected, the related posts will be displayed on the related page.

    Can you help me achieve this result?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • $category = get_queried_object();
    $slug = $category->slug;

    then replace ‘my-category’ with $slug

    Thread Starter ivanisevic82

    (@ivanisevic82)

    Thank you, it works perfectly!

    Thread Starter ivanisevic82

    (@ivanisevic82)

    Hello, I am writing here again because I have another similar need.

    I would like all posts of a certain category to be displayed NOT according to the current category, BUT according to the current PAGE.

    I specify that, if it were useful for my purpose, I can name the slug of the page equally to that of the respective category.

    I gave it a try by making a change to the code you recommended above:

    $category = get_queried_object();
    $slug = $page->slug;

    Unfortunately it doesn’t work.

    Can you still support me?

    Many thanks!

    EDIT: Solved using this code!

    global $post;
    $post_slug = $post->post_name;
    • This reply was modified 1 year, 10 months ago by ivanisevic82.
    Thread Starter ivanisevic82

    (@ivanisevic82)

    Here I am again for one last need: I would like to add a piece of code to the page to show in case there are no posts.

    For example:

    <?php else: ?>
    <p>No posts found. :(</p>
    <?php endif; ?>

    Can you tell me what I can write or where to add this piece of code in my code reported in the opening post?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Call a slug category by current page’ is closed to new replies.