[Resolved] Elements not showing up on archives pages

Home Forums Support [Resolved] Elements not showing up on archives pages

Home Forums Support Elements not showing up on archives pages

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1403411
    C

    Hi,
    I’m using a custom function to reorder posts on my category pages based on a custom field. However, this causes elements – both hooks and headers – to not show up on these pages. The green banner on the site is a header element set to be displayed on the entire site but does not show up on my category pages. I realize this isn’t exactly a GP issue but I’m wondering if there is any way for me to continue using the function and display elements on these pages:

    function order_by_custom_field( $query ) {
        if ( is_category()) {
    $query->set('meta_query', array(
        array(
        'meta_key' => 'custom_key',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
        )
    ));
    $query->set('meta_key', 'custom_key');
    $query->set('orderby', 'meta_value_num');
        }
    }
    add_action( 'pre_get_posts', 'order_by_custom_field' );
    #1403425
    Leo
    Staff
    Customer Support

    Hi there,

    Can’t see how the two are related.

    How are you adding the function?

    And what’s inside the page hero content?

    #1403436
    C

    The function was added to functions.php and the page hero content contains a styled div.

    #1403536
    Leo
    Staff
    Customer Support

    And the element shows up correctly as soon as you remove that function?

    What’s inside the styled <div>?

    #1403829
    C

    At the time, I had an ad dynamically inserted inside the styled div. It shows up perfectly on all pages except the pages affected by my function. I’m not a wordpress developer but from what I’ve read, it seems to be related to pre_get_posts modifying the page.

    The stack exchange question below was the closest thing I could find that best describes the problem from my basic understanding, but the solution is way over my head.

    https://wordpress.stackexchange.com/questions/214895/using-pre-get-posts-on-true-pages-and-static-front-pages

    #1403830
    C

    And yes, once the function is removed, everything shows up as it should.

    #1403915
    C

    Finally got it working. All I had to do was add “&& $query->is_main_query()” to the conditional.

    #1404113
    Leo
    Staff
    Customer Support

    Glad to hear!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.