WordPress.org

Forums

Customizr
[resolved] Automatic generation of "featured pages"? (6 posts)

  1. fridde
    Member
    Posted 11 months ago #

    I have been working myself through the source code of Customizr and, yes, it's well written.

    I am looking for the simplest way of changing the generation of the featured pages on the frontpage WITHOUT changing the source of customizr.
    Right now the admin has to change these posts/pages manually. But I want the 6 newest posts that have the tag "frontpage"

    I have already read most of the codex, made a child theme and I have already "played" with the original code to let me have 6 featured pages instead of 3. (That worked)

    I am not a total newbie in php and could write a query that gets these 6 newest posts that are tagged with "frontpage", but HOW would I change the original function that is build into the class TC_featured_pages right now?
    I guess, some combination of remove_function and add_function will be the answer, but I am stuck...

    Anyone?

  2. nikeo
    Member
    Theme Author

    Posted 11 months ago #

    Hi @fridde,
    I understand that you need to replace your 6 featured pages dynamically by your 6 lasts featured posts.
    Here's what I would do :
    1) Create a function to store the last 6 featured posts ids in an array, either in a global var or a class property by looping on a get_posts() array of posts objects.
    2) Use the 'tc_fp_id' filter to override the default behaviour and assign the wanted post id to each single featured pages (with a switch...case on $fp_single_id var for example : case 'one', case 'two', ..., case 6)

    Hope this helps!

  3. fridde
    Member
    Posted 11 months ago #

    Yes, that helped me very much! Thank you! Also, I have understood more about how your code is written.

    Well, to be honest, I still have one minor problem. I wanted to implement your idea of the switch-case, but the value passed to the hook "tc_fp_id" is not "one", "two", "three" etc, but the actual id of the chosen post/page returned by tc__f( '__get_option' , 'tc_featured_page_'.$fp_single_id) (line 136 in class-content-featured_pages.php).

    My code in functions.php looks like this:

    remove_all_filters('tc_fp_id');
    remove_all_filters('tc_featured_pages_ids');
    
    function custom_fp_ids_array(){
        return array( 'one' , 'two' , 'three' , 'four', 'five', 'six');
    }
    
    function return_custom_ids($value){
        echo $value . "<br>";
    
        //insert switch-case here, but since the value is not the expected,
        // I simply return "1" to see that the rest works.
        // Post 1 does exist
    
        return 1;
    }
    
    add_filter('tc_featured_pages_ids', 'custom_fp_ids');

    The output created by echo $value is 24, 5, 0 ..., the id's corresponding to the posts I have chosen in the settings.
    Of course, a workaround would be to simply chose certain posts as the featured posts in customizrs settings-menu and build the switch-case around the id's of these posts. But the, of course, some unfortunate future admin could do changes in the customizr-settings not knowing that he/she just destroyed my workaround.

    Any idea what I have been missing or mixing up? I have followed the "road" of $fp_single_id, and yes, I expect it to be "one" or "two" etc, but I can't see why it has changed to the numerical correspondent BEFORE entering my custom filter. Maybe you can see it directly.

  4. nikeo
    Member
    Theme Author

    Posted 11 months ago #

    Hi @friddle,
    the filter is written as follow :

    $featured_page_id               = apply_filters( 'tc_fp_id', esc_attr( tc__f( '__get_option' , 'tc_featured_page_'.$fp_single_id) ), $fp_single_id );

    You are right, the filtered value is :

    esc_attr( tc__f( '__get_option' , 'tc_featured_page_'.$fp_single_id) )

    but you can access the second parameter of the filter, which is the one you need :

    $fp_single_id

    This type of code will do the trick (note that I set 2 parameters in the filter declaration) :

    add_filter( 'tc_fp_id' , 'my_forced_id' , 10 , 2 );
    function my_forced_id ( $fp_database_id, $fp_text_id ) {
    	//declare your array of featured POST that you get with a function
    	//in my example it should look like a simple array of numerical post ids $posts_to_feature = array( post_id_1, post_id_2, ...);
    	$posts_to_feature 		= get_my_posts_to_feature();//this function could use for example get_posts() for example to fetch your featured posts. you can also build a custom query with a class instanciation like $my_query = new WP_query;
    
    	//get your custom fp array
    	$custom_fp_ids_array 	= custom_fp_ids_array();
    
    	//loop on the fp id array
    	//check if tc_fp_id exists in fp custom array (safer) and returns the custom post id sharing the same key
    	while( $el = current($custom_fp_ids_array) ) {
    	    $fp_key =  key($custom_fp_ids_array);
    	    if ( isset($custom_fp_ids_array[$fp_key]) && $fp_text_id == $custom_fp_ids_array[$fp_key] )
    	     	return $posts_to_feature[$fp_key];
    	    //if no match found, then loop to the next item
    	    next($test);
    	}
    
    	//if no match was found, then return the original value.
    	return $fp_database_id;
    }

    I did not test it but this should hopefully not be too far from what you need.

  5. fridde
    Member
    Posted 11 months ago #

    That was it! Thanks!

    I had actually tested

    function return_custom_id($value, $secondvalue){
        echo $value . "<br>";
        echo $secondvalue . "<br>";
        return 1;
    }
    add_filter('tc_fp_id', 'return_custom_id');

    but had only gotten an error saying Warning: Missing argument 2 for return_custom_ids. So I made the assumption that there was no second argument created.

    Thanks to your hint that add_filter should actually declare how many arguments that are sent, I immediately could build a working function.

    You can't imagine my thankfulness! I'll convince the board of my club to grant a donation.

  6. nikeo
    Member
    Theme Author

    Posted 11 months ago #

    Cool.
    Donation, do what you want but hey, you don't have to!

Reply

You must log in to post.

About this Theme

About this Topic

Tags

No tags yet.