• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Displaying posts from single category using custom query

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Displaying posts from single category using custom query

This topic is: not resolved

Tagged: category.php, WP_query

  • This topic has 1 reply, 2 voices, and was last updated 3 years, 11 months ago by Brad Dalton.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • July 4, 2019 at 7:11 am #492125
    sbrody
    Member

    Hi - I've created a custom query to display category-specific blog posts. It needs to work on every category page, so I need to be able to pull through the category archive page title as a variable to use in the query. But I just can't get it to work - it just pulls all my posts. Here is my code so far - can anyone help? Thanks!

    function this_cat() {
        $categorypage = single_cat_title();
        
        return $categorypage;
    }
    echo this_cat();
    
    remove_action('genesis_loop', 'genesis_do_loop');
    add_action('genesis_loop', 'srl_category_loop');
        function srl_category_loop() {
            
            $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
                'post_count' => '-1',
                'category_name' => this_cat()
            );
            $the_query = new WP_Query( $args );
            
            // The Loop
            if ( $the_query->have_posts() ) {
                echo '<div id="macy-container">';
                while ( $the_query->have_posts() ) {
                    echo this_cat();
                    $the_query->the_post();
                    $tags = get_the_tags();
                    if ((!empty ($tags)) && ($tags[0]->name == 'Photo')):
                        echo '<div class="grid-item photo">';
                    else: 
                    echo '<div class="grid-item">';
                    endif;
                    echo the_post_thumbnail();
                    echo '<a href="' . get_permalink() . '">' . '<h2>' . get_the_title() . '</h2>' . '</a>';
                        $category = get_the_category();
                        if ( !empty($category) ): 
                        echo '<div class="post-cat">' . esc_html($category[0]->name) . '</div>';
                        endif; ?>
                    <div class="overlay"><a href="<?php echo get_permalink(); ?>">Read</a></div>
                <?php echo '</div>';
                }
                echo '</div>';
            } else {
                // no posts found
            }
            /* Restore original Post Data */
            wp_reset_postdata();
       }
    
     genesis(); 
    July 5, 2019 at 11:11 pm #492149
    Brad Dalton
    Participant

    What did you name the file?

    Also, what are you trying to do?


    Tutorials for StudioPress Themes & WooCommerce.

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble