Support » Theme: Twenty Fourteen » Override twentyfourteen_get_featured_posts

  • Resolved mepmepmep

    (@mepmepmep)


    Hello friends! I’ve created a child theme for twenty fourteen where I display the slider on every page. The site is divided into categories and so I need to display different featured content depending on which category the visitor sees (I’ve used a plugin to be able to add categories to pages as well as posts). The below code almost works.. it picks the correct featured content by category. BUT… it no longer respects the ‘Featured’ tag… I get ALL new posts in the slider and I still want to use the ‘Featured’ tag. What am I missing here? How do I change the code to have the normal behaviour of filtering content by the ‘Featured’ tag?

    remove_filter( 'twentyfourteen_get_featured_posts' );
    
    function twentyfourteen_child_get_featured_posts( $posts ){
    	global $post;
    	$current_post_id = $post->ID;
    	$show_posts = 5; //max number of posts returned to slider
    
    	if( is_category() || is_archive() ){
    		$current_cat_id = get_query_var('cat');
    		$args = array('category' => $current_cat_id, 'post_type' => 'post', 'posts_per_page' => $show_posts);
    	}elseif( is_page() || is_single() ){
    		$category = get_the_category($current_post_id);			$current_cat_id = $category[0]->cat_ID;
    
    		$args = array('category' => $current_cat_id, 'post_type' => 'post', 'posts_per_page' => $show_posts);
    	}else{
    		$args = array( 'post_type' => 'post', 'posts_per_page' => $show_posts);
    	}
    
    	$new_post_array = get_posts( $args );
    
    	if ( count($new_post_array) > 0 ) {
    		return $new_post_array;
    	} else {
    		return $posts;
    	}
    }
    add_filter( 'twentyfourteen_get_featured_posts', 'twentyfourteen_child_get_featured_posts', 999, 1 );
Viewing 6 replies - 1 through 6 (of 6 total)
  • This piece of code is located in the Functions.php file of my Twenty fourteen child theme.

    … and here is the development version of the site:
    http://oikoscommunity.org.92-33-32-245.goclouded.com/wp/

    I have solved this.. Topic can be closed..

    dbl post

    My current solution… use it or suggest better solutions! 🙂

    /*
     *	Overide featured posts to get posts by category
    */
    remove_filter( 'twentyfourteen_get_featured_posts', 'twentyfourteen_get_featured_posts' );
    
    function twentyfourteen_child_get_featured_posts( $posts ){
    	global $post;
    	$current_post_id = $post->ID;
    	$show_posts = 5; //max number of posts returned to slider
    	//$featured_tag = 'Bildspel';
    	//$featured_tag = Featured_Content::get_setting('tag-name');
    	$featured_tag_id = Featured_Content::get_setting('tag-id');
    
    	if( is_category() || is_archive() ){
    		$current_cat_id = get_query_var('cat');
    		//$args = array('category' => $current_cat_id, 'post_type' => 'post', 'posts_per_page' => $show_posts, 'tag_id' => get_tag_ID($featured_tag) );
    		$args = array('category' => $current_cat_id, 'post_type' => 'post', 'posts_per_page' => $show_posts, 'tag_id' => $featured_tag_id );
    	}elseif( is_page() || is_single() ){
    		$category = get_the_category($current_post_id);
    		$current_cat_id = $category[0]->cat_ID;
    		//$args = array('category' => $current_cat_id, 'post_type' => 'post', 'posts_per_page' => $show_posts, 'tag_id' => get_tag_ID($featured_tag) );
    		$args = array('category' => $current_cat_id, 'post_type' => 'post', 'posts_per_page' => $show_posts, 'tag_id' => $featured_tag_id );
    	}else{
    		//$args = array( 'post_type' => 'post', 'posts_per_page' => $show_posts, 'tag_id' => get_tag_ID($featured_tag) );
    		$args = array( 'post_type' => 'post', 'posts_per_page' => $show_posts, 'tag_id' => $featured_tag_id );
    	}
    
    	$new_post_array = get_posts( $args );
    
    	if ( count($new_post_array) > 0 ) {
    		//return $new_post_array;
    		return custom_shuffle($new_post_array);
    	} else {
    		//return $posts;
    		return;
    	}
    }
    add_filter( 'twentyfourteen_get_featured_posts', 'twentyfourteen_child_get_featured_posts', 999, 1 );

    Custom shuffle is just a function that takes the featured posts that should be displayed and randomizes the order they are displayed in. To get some variety on the page. Here:

    /*
     * return an array whose elements are shuffled in random order.
     */
    function custom_shuffle($my_array = array()) {
    	$copy = array();
    	while (count($my_array)) {
    		// takes a rand array elements by its key
    		$element = array_rand($my_array);
    		// assign the array and its value to an another array
    		$copy[$element] = $my_array[$element];
    		//delete the element from source array
    		unset($my_array[$element]);
    	}
    	return $copy;
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Override twentyfourteen_get_featured_posts’ is closed to new replies.