WordPress.org

Forums

Twenty Fourteen
[resolved] Override twentyfourteen_get_featured_posts (7 posts)

  1. mepmepmep
    Member
    Posted 1 year ago #

    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 );
  2. mepmepmep
    Member
    Posted 1 year ago #

    This piece of code is located in the Functions.php file of my Twenty fourteen child theme.

  3. mepmepmep
    Member
    Posted 1 year ago #

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

  4. mepmepmep
    Member
    Posted 11 months ago #

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

  5. mepmepmep
    Member
    Posted 11 months ago #

    dbl post

  6. mepmepmep
    Member
    Posted 11 months ago #

    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 );
  7. mepmepmep
    Member
    Posted 11 months ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic