• Hello,

    I have a twenty twelve theme, and am trying to have the order of posts in the category archives in the reverse-chronological order (i.e., earliest-written posts will appear first or on the top)

    I only want to have this happen when reader access the category archives and not globally.

    What code should i tack on or modify? and where do i add it to? .css file?

    thank you in advance!!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi wpnoob2014

    Try and create a child theme or download a pre-made Twenty twelve child theme from here.

    Then put this in your child theme’s functions.php to have categories go in ascending order:

    add_action( 'pre_get_posts', 'category_asc_query' );
    function category_asc_query( $query ) {
    
    	// if it's not a wp-admin page and it's the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    
    		// if it's a category archive page
    		if ( is_category() ) {
    
    			// set the order to ascending
    			$query->set( 'order', 'ASC' );
    		}
    	}
    }

    Thread Starter wpnoob2014

    (@wpnoob2014)

    Thank you, but i admit I just heard abt this whole child theme existence abt 1 days ago πŸ™

    But I was working on my websit since 1 week ago
    And the problem is I hv already edited some of my .CSS and other file codes
    Not a lot, but I did enough edits such that i like how my website looks now and am very afraid o starting all over again

    To prevent further damage and avoid me starting my website on blank state again (ie uninstall and reinstall wordpress) , what do you suggest I do now? I just learned creating child theme is essential otherwise future WP updates can mess things up…….

    So, what i would like to know is! how can I create child theme(what special steps?) such that I don’t mess up whatever I hv now on the website and it’s appearance ?

    My domain is travelmolecule.com

    Moderator keesiemeijer

    (@keesiemeijer)

    See:
    http://codex.wordpress.org/Child_Themes

    If you know what files you edited you can just copy them over to your child theme directory, except for the functions.php file. see http://codex.wordpress.org/Child_Themes#Using_functions.php

    I prefer to also start over with the style.css file, but if you want you can copy it and add the child theme header to it without the @import url("../twentytwelve/style.css"); rule in it. But be aware that an upgrade with style changes will not show up if you do it this way.

    Thread Starter wpnoob2014

    (@wpnoob2014)

    hi, sorry,

    i dont think i follow everything you say.
    so are you suggesting that i should start everythng from scratch again – ie. uninstall and reinstall wordpress?

    i believe i only edited .css files and maybe 2-3 other files, including functions.php file.

    I prefer to also start over with the style.css file, but if you want you can copy it and add the child theme header to it without the @import url(“../twentytwelve/style.css”); rule in it. But be aware that an upgrade with style changes will not show up if you do it this way.
    i am not sure what you mean by “starting over with .css file” do you mean uninstall and reinstall wordpress.
    not sure what @ import url means…

    are you suggesting that i should start everythng from scratch again – ie. uninstall and reinstall wordpress?

    No. Just a fresh child theme. Did you try reading http://codex.wordpress.org/Child_Themes ?

    not sure what @ import url means

    Do you know how to use CSS?

    Thread Starter wpnoob2014

    (@wpnoob2014)

    No. Just a fresh child theme. Did you try reading http://codex.wordpress.org/Child_Themes ?
    yes, i did. it seems that only .css file is required for child theme
    but as you are aware, my existing .css file is already not original. it’s already being edited a little here and there. so are you saying that if i were to do all thats’ said on http://codex.wordpress.org/Child_Themes and also put the line “@import url(“../twentyfourteen/style.css”)”, the content of my child theme will be exactly identical to how the original twenty-twelve theme will look like, had i not done any edits?

    secondly, how do i make my current .css file (which will technically become the parent .css file) back to the original state? should i copy over content of .css file (which will be designated parent now) over to the newly-created child theme, then somehow revert the .css parent file back to original state?

    i guess i want to make sure i get this right now and for all ie undo all my mistakes for now since i am at early stage. i want to make sure whatever i do now, wont get mess up sometime in future when WP update/uupgrade happen

    i am also a pure newbie to WP. just started a week ago, so please kindly be patient to me :(, let me know step-by-step on what i need to do so that i dont mess things up – i already had to uninstall and reinstall WP once because i didnt know how to undo another previous unrelated mistakes to this issue πŸ™

    Moderator keesiemeijer

    (@keesiemeijer)

    To start new:
    Backup your parent theme Twenty Twelve (with the edits).

    Replace the parent Twenty Twelve theme with a fresh download of the theme.
    http://wordpress.org/themes/twentytwelve

    Create the child theme. outlined here:
    http://codex.wordpress.org/Child_Themes

    Copy (from the backup) all the files you edited over to your child theme, except the functions.php and style.css files.

    Copy (from the backup) the edits you made in the functions.php and style.css file over to your child theme’s functions.php file and style.css.

    http://codex.wordpress.org/Child_Themes#Using_functions.php

    Thread Starter wpnoob2014

    (@wpnoob2014)

    thank you so much keesiemeijer!!!
    i think i manage to successfully do it (meaning, i have replaced all the parent twentytwelve files with a fresh download of theme, as you instructed.

    can i confirm the following

    1) so from now on, if i want to make any edits, i should just ADD the code lines to .css file, functions.php file, under the child theme folder, correct? if there’s any other file i want to modify (e.g., custom header), then i should make a copy from the parent folder into the child theme folder before modifying?

    2) second, i am a little confused with how functions.php work. in the link you sent, i understood it as child theme functions.php get loaded BEFORE parent theme functions.php is loaded. However, i googled online on how to adjust the header dimensions for my child theme, and found the below code lines. are these code lines only specific to loading AFTER parent functions.php? what’s the difference between having child theme functions.php load first or second?

    thank you

    [ Moderator note: Code fixed, please wrap code in backticks or use the code button. ]

    <?php
    
    /* This function will run after the parents functions.php */
    add_action( 'after_setup_theme', 'post_theme_setup' );
    
    if ( !function_exists( 'post_theme_setup' ) ):
    
    function post_theme_setup() {
       /* Code goes here! */
    
       add_filter( 'twentytwelve_header_image_width', 'my_header_width' );
       add_filter( 'twentytwelve_header_image_height', 'my_header_height' );
       function my_header_width($width) {
       	$width = 1200;
    	return $width;
       }
       function my_header_height($height) {
    	$height = 400;
    	return $height;
       }
    }
    
    endif;
    Moderator keesiemeijer

    (@keesiemeijer)

    1)
    Yes, that’s the way to do it πŸ™‚

    2)
    The child functions.php file will always load before the parents functions.php file.

    If you want to edit a part of a function copy the whole function over to your child theme and edit it there. Because the function in your child theme is loaded earlier the function with the same name in the parent is not loaded at all because of the if ( !function_exists( ... ) ) conditional, which means “if function does not exist”. The exclamation mark means “not”.

    With add_action() (in the code sample above) you can set a $priority parameter to have the function load after the parent function that’s also hooked to the ‘after_setup_theme’ hook.

    http://codex.wordpress.org/Function_Reference/add_action
    http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme

    I hope this makes sense.

    Thread Starter wpnoob2014

    (@wpnoob2014)

    still quite confusing to me…. but am i correct to summarize the following??

    1) if i want to edit any function.php file, and i know which relevant code lines, then i should copy the code lines from parent function.php file over to child function.php file, and edit in the child file

    and these copied code lines should go after the line function post_theme_setup() { /* Code goes here! */ but before the line endif;?

    and because child file loads first, this means the modified function in child’s file reigns supreme over any original function that exist, because the functions will only run one time?

    2)

    Moderator keesiemeijer

    (@keesiemeijer)

    For example, you want to make an edit to some_function() in the parent theme and the function is inside a function_exists():

    // parent functions.php
    
    if ( !function_exists( 'some_function' ) ) :
    	function some_function() {
    
    		/* function code! */
    
    	}
    endif;

    You copy the whole function to the child functions.php. the child some_function() will override the parent some_function()

    // child functions.php
    
    function some_function() {
    
    	/* make edits to the function code here! */
    
    }

    Example with filters and actions.
    For example, the some_function() function in the parent is called by add_filter().
    http://codex.wordpress.org/Function_Reference/add_filter

    And for example it’s using “the_content” filter hook to add “Hello” after post content.
    http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

    // parent functions.php
    
    add_filter( 'the_content', 'some_function' );
    
    function some_function( $content ) {
    
    	/* adds "Hello" after post content */
    	return $content . 'Hello';
    
    }

    If you instead want it to add ‘Goodbye’ you’ll have to remove the parent filter first and use your own filter and function in the child functions.php.

    // child functions.php
    
    // removing filter from parent
    remove_filter( 'the_content', 'some_function' );
    
    // adding your own filter to the_content
    add_filter( 'the_content', 'child_some_function' );
    
    function child_some_function( $content ) {
    
    	/* adds "Goodbye" after post content */
    	return $content . 'Goodbye';
    
    }

    Or let’s say, you want to add ” World” after the “Hello” added by the parent “the_content” filter. You’ll have to use the $priority parameter of the add_filter() function.
    http://codex.wordpress.org/Function_Reference/add_filter

    // child functions.php
    
    // $priority 11
    add_filter( 'the_content', 'child_some_function', 11 );
    
    function child_some_function( $content ) {
    
    	/* adds "World" after post content */
    	return $content . ' World';
    
    }

    If the priority is lower, or not set the result would be ” WorldHello” because the child filter is added before the parent filter (child functions.php is loaded before parent functions.php).

    Side note
    If the $priority parameter is not used it defaults to 10.

    // parent functions.php
    
    // $priority not used, default priority 10
    add_filter( 'the_content', 'some_function' );

    This is all very technical but these are the ways you can edit or override functions in the child functions.php

    Hiii to everyone ,
    I am new wordpress Developer πŸ˜‰ . i am try to solve out this problem if any mistake let me know plz.
    @wpnoob2014 try it
    <?php

    $args = array(
    ‘showposts’ => 2,//for nos of post u want to show
    ‘category_name’ => ‘xyz’, // name of category
    ‘orderby’=>’meta_value’,
    ‘meta_key’=>’view_order’, //view_order is custum field to that post
    ‘order’=>’ASC’ //Order of post ascending or decending
    );
    query_posts( $args );

    if (have_posts()) : while (have_posts()) : the_post();

    // here place code
    ?>

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘category archives – making earliest posts appear first’ is closed to new replies.