WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] show sticky post in cat but not on front page (9 posts)

  1. creed3000
    Member
    Posted 1 year ago #

    Exclude sticky posts from front page but show in category as a sticky post ( top of cat page ) also do not want the sticky post showing up in cats rss feeds

    website > http://www.psychic3000.com/
    Blog > http://psychic3000.com/wordpress/

  2. bcworkz
    Member
    Posted 1 year ago #

    You can control whether sticky posts are used or not by altering the query with the 'ignore_sticky_posts' argument. You alter the query by hooking the 'pre_get_posts' filter. Your filter function would determine which page is requested, then alter the query or not.

    Some useful references:
    https://codex.wordpress.org/Class_Reference/WP_Query#Sticky_Post_Parameters
    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  3. creed3000
    Member
    Posted 1 year ago #

    thank you so much for the help
    but i must admit that i dont know where i would place the code
    i dont know what templete to place it ..

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-1,-1347' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    also im not sure if i need to include

    <?php add_action( 'pre_get_posts', 'exclude_category' ); ?>

    again thanks for the help !!!

  4. bcworkz
    Member
    Posted 1 year ago #

    The easiest place to put the code is in your theme's functions.php file, but your really should create a child theme, even if that's the only content, as your change is then protected from theme upgrades. You could also create a very basic plugin to contain your custom code instead of a child theme. Since you're altering how content is displayed, a child theme makes more sense to me.

    Yes, you must have the add_action() call, or your function will never be executed when the query runs. I also see you are restricting which categories are displayed rather than controlling the use of stickies. This may work for your particular situation, perhaps only stickies have those categories. So it may be fine, it's just different from your original question. As long as you're clear what's going on, it doesn't matter if I'm not :)

  5. creed3000
    Member
    Posted 1 year ago #

    thank you for your reply
    & yes your are correct just different from your original question.
    i think i basic gave up, becuase i could not ( have not been able to get it to work ) so it looks like i jumped to hide category.

    which in all reality is the bigger issue for me. hide post from front page & hide sub categories from main menu side bar. anyway i took your advice & did create a child theme

    i have been able to install child theme
    did test to change background color of blog ..( it works )
    here is the code that im using in that child theme

    /*
    Theme Name:     twenty child 1
    Theme URI:      http://www.psychic3000.com/
    Description:    Child theme for the Twenty Twelve theme
    Author:         Creed masters
    Author URI:     http://www.psychic3000.com/
    Template:       twentyeleven
    Version:        3.4.2
    */
    /* Import layout */
    @import url(../twentyeleven/style.css);
    
    a{ color: #254655; }
    body{
    background: #727D82;
    }  
    
    header#branding{
    background: #29305C;
    color: #B3BDC1;
    }  
    
    header#branding h1, header#branding h2, header#branding a{ color: #B3BDC1; }  
    
    #respond{ background: #E7DFD6; }
    
    <?php add_action( 'pre_get_posts', 'exclude_category' ); ?>
    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-1,-1347' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    ive added the code from above with no reults ..
    im new tho this so i dont really know what im doing here
    creating the child theme & getting that to work was a huge step for me, so i need some big time direction & help

    do i put it here in the child theme css
    do i make another file in my cild theme folder ??
    heck will that code work to remove post from front page
    & what code do i need to hide sub categories from mian menu

    thankful for any advice & all help ..

    im trying to hide sub categories from main menu

  6. bcworkz
    Member
    Posted 1 year ago #

    OK, let's back up a little. For what your are trying to do here, you need two files in your child theme folder, the style.css, which you have implemented correctly, as far as the stying portion goes. Good work getting that portion figured out, this child theme will serve you well for any future modifications as well.

    The other file you need is functions.php. Remove all of your posted code from the first <?php and down from style.css and place it in functions.php. Also either erase the ?> or move it to the very end of the file. You also have two add_action() calls, you only need one, erase the other. Doesn't matter which one, it works equally well above or below the function definition.

    All PHP code must be inside blocks delimited by <?php ?> and in a file with a .php extension. Any text outside these delimiters in a .php file is treated as HTML and is sent to the user's browser. If the final text in a file is PHP code, you can omit the final ?> and it will all work properly still. In fact dropping the final ?> is considered good practice by many.

    You should be seeing results this time! Either way, let us know how it goes. Good luck.

  7. creed3000
    Member
    Posted 1 year ago #

    hello
    i did create the function.php
    & with the help of "alchymyth" i
    added in this code to hide post & sub categories

    <?php
    
    add_filter('widget_categories_args','custom_category_widget_args');
    function custom_category_widget_args($args) {
    $args['depth'] = '1';
    return $args;
    } 
    
    function exclude_homepage_categories( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-15' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_homepage_categories' );

    hide post of category from front page ( sucsess )
    hide post of sub categories from side bar main menu ( fail )
    any help would be useful & great !!

  8. bcworkz
    Member
    Posted 1 year ago #

    If this code is applied to your live site you linked in your first post, it appears to be working, but I'm not privy to your actual category structure, I don't know what's a child category, and it is possible to get categories listed as flat, without indents for each level.

    I'm wondering if we're talking about the same thing. Do you wish to restrict categories listed under 'Categories' to the main categories only? Or do you want to restrict posts shown under 'Recent Posts' to only those in the main categories? Or both? Something else? The widget code should work for the first condition, but will not work for the second.

    For the second condition, no surprise by now, there's another filter for that. It should look something like this (untested, if it fails the problem is probably how I defined the argument):

    add_filter('widget_posts_args','custom_posts_widget_args');
    function custom_posts_widget_args($args) {
       $args['tax_query'] = array(
    	array(
    		'taxonomy' => 'category',
    		'include_children' => false,
    	)
       );
       return $args;
    }

    If your goal is something else, my apologies for misunderstanding, try explaining again and hopefully I will understand this time.

  9. creed3000
    Member
    Posted 1 year ago #

    yep thanks for the info
    also want to thank "alchymyth"
    the support here is really great

Topic Closed

This topic has been closed to new replies.

About this Topic