Support » Theme: Hueman » Directing posts to different columns

  • Resolved adieu

    (@adieu)


    Would it be possible to have some posts appear in one of the sidebars without them first appearing in the main content area?

    I would like to have small news items appear in a sidebar on my site while reserving the main post area to longer destination features.

    My understanding is that some premium themes allow this by placing posts in different columns based on their category.

    Thanks

    • This topic was modified 7 years, 6 months ago by adieu.
Viewing 11 replies - 16 through 26 (of 26 total)
  • Check the “Display the featured posts also in the list of posts” option in Customize > Content > Blog Design and Content > Featured Posts.

    Thread Starter adieu

    (@adieu)

    I checked and unchecked the option, but nothing seems to change. I would rather not display the featured post in the list below, but I guess it’s not the end of the world if it does.

    I wonder if I might have removed important code when I edited featured.php.

    This is how it read:

    $featured = new WP_Query(
    	array(
    		'no_found_rows'				=> false,
    		'update_post_meta_cache'	=> false,
    		'update_post_term_cache'	=> false,
    		'ignore_sticky_posts'		=> 1,
    		'posts_per_page'			=> hu_get_option('featured-posts-count'),
    		'cat'						=> hu_get_option('featured-category')
    	)
    );

    I changed it to:

    $featured = new WP_Query(
    	array(
    		'no_found_rows'				=> false,
    		'update_post_meta_cache'	=> false,
    		'update_post_term_cache'	=> false,
    		'ignore_sticky_posts'		=> 1,
    		'posts_per_page'			=> hu_get_option('featured-posts-count'),
    		'cat'						=> ('-16')
    	)
    );

    Thinking I might have deleted important information in the editing, I tried a couple variations of:

    'cat' => ('-16') hu_get_option('featured-category')

    But my home page was wiped blank.

    • This reply was modified 7 years, 6 months ago by adieu.
    Thread Starter adieu

    (@adieu)

    I just tried this:

    'cat' => hu_get_option('featured-category', '-16')

    But it showed the page as if nothing was changed. 16, or News, appeared in the most recent post.

    • This reply was modified 7 years, 6 months ago by adieu.

    Your edit to featured.php only changed the criteria for the category. If you unchecked the “display in list” option and they are still in the content area then there’s another disconnect somewhere. Let me poke around and see what I can find.

    But my home page was wiped blank.

    That was an invalid PHP statement and when PHP chokes, it stops wherever the error is. The result is what you saw. In other cases a PHP error can take down the entire site.

    But it showed the page as if nothing was changed

    Probably because the hu_get_option() function only accepts the first parameter and ignores the rest.

    I think I found it. The theme implements its own version of the pre_get_posts() function in the init-front.php functions file. That version of pre_get_posts() sets the blog post ids to exclude the featured post ids using another function, hu_get_featured_post_ids(). That function is still using this argument:

    'category' => hu_get_option('featured-category'),

    So, we need to copy function hu_get_featured_post_ids() to your child theme functions.php file and modify the category like we did in featured.php, so it would look like this:

    function hu_get_featured_post_ids() {
        $args = array(
          'category'    => '-16',
          'numberposts' => hu_get_option('featured-posts-count')
        );
        $posts = get_posts($args);
        if ( !$posts ) return false;
        foreach ( $posts as $post )
          $ids[] = $post->ID;
        return $ids;
    }

    Now you’ll just need to remember that if you decide to exclude different posts then you’ll need to change the category id in two places.

    Thread Starter adieu

    (@adieu)

    Thanks for that.

    I have been playing around with the Ultimate Category Excluder plugin and it seems to do what I think I need.

    Edited to say that I was thanking you before your last post. I will try what you last posted.

    • This reply was modified 7 years, 6 months ago by adieu.
    Thread Starter adieu

    (@adieu)

    Little confused… So this will appear in my child theme functions.php:

    function my_exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-16' );
        }
    }
    add_action( 'pre_get_posts', 'my_exclude_category' );
    
    function hu_get_featured_post_ids() {
        $args = array(
          'category'    => '-16',
          'numberposts' => hu_get_option('featured-posts-count')
        );
        $posts = get_posts($args);
        if ( !$posts ) return false;
        foreach ( $posts as $post )
          $ids[] = $post->ID;
        return $ids;
    }

    It doesn’t work. 16 appears in the featured posts, and the previous featured posts disappears, though it remains in the dashboard list.

    I’m probably missing a big piece of the puzzle.

    I just went back through the topic here to make sure I had all the right functions. I ran some tests and, based on the pieces we have used, it should work.

    In featured.php the query should look like this:

    // Query featured entries
    $featured = new WP_Query(
    	array(
    		'no_found_rows'			=> false,
    		'update_post_meta_cache'	=> false,
    		'update_post_term_cache'	=> false,
    		'ignore_sticky_posts'		=> 1,
    		'posts_per_page'		=> hu_get_option('featured-posts-count'),
    		'cat'				=> '-16'  // remove news category posts
    	)
    );
    

    In your functions.php file you should have two functions:

    // Remove news category posts on home page
    function my_exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-16' );
        }
    }
    add_action( 'pre_get_posts', 'my_exclude_category' );
    
    // Remove news category posts from list of featured posts so the list matches the posts
    // used in featured.php. This list is used in function hu_pre_get_posts() to remove the
    // featured posts from the content posts so they're not duplicated
    function hu_get_featured_post_ids() {
        $args = array(
          'category'    => '-16',
          'numberposts' => hu_get_option('featured-posts-count')
        );
        $posts = get_posts($args);
        if ( !$posts ) return false;
        foreach ( $posts as $post )
          $ids[] = $post->ID;
        return $ids;
    }

    The end result should be:
    1. No news category posts in the featured posts
    2. No news category posts in the main content
    3. No posts in the featured posts displayed in the main content

    • This reply was modified 7 years, 6 months ago by bdbrown.
    • This reply was modified 7 years, 6 months ago by bdbrown.
    Thread Starter adieu

    (@adieu)

    I messed it up a few times! For some reason it wasn’t saving in the dashboard editor, so I edited the files in the cpanel file manager, and…

    Presto, you are a genius, bdbrown. I am in awe.

    And very thankful. Cheers bud, the next pirate lasagna is on me.

    Glad to hear it. Your site is looking good. If I’m ever up your way I’ll take you up on your offer.

    Thread Starter adieu

    (@adieu)

    It is my pleasure to mark this RESOLVED.

    Thanks, bdbrown

Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘Directing posts to different columns’ is closed to new replies.