WordPress.org

Forums

How to change featured content to a different tag (3 posts)

  1. lwdesign
    Member
    Posted 1 year ago #

    In twenty fourteen theme, we’d like to know how to change the features content tag. Currently it’s set as featured.

    Now, we would like to know how to code this for different page templates.

    We notice in page.php there is a piece of code calling the template part featured-content.

    <?php
    	if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
    		// Include the featured content template.
    		get_template_part( 'featured-content' );
    	}
    ?>

    From our understanding this calls the file featured-content.php. In this file, is there a way to decide the tag. We imagine you can do it using an if statement and the $tag or $term variable inside an array. But we’re not completely sure.

    Our idea is to create three pages on our site, each using a different tag to show different posts in the featured content section.

    Can this be done and can anyone provide us with the code or logic to do it?

    Thanks in advance!
    the LWdesign team

  2. Ashok
    WPMU DEV Support Staff
    Posted 1 year ago #

    Let me set a scenario for you.

    Suppose you have three tags:
    logo
    graphic
    design

    For those you have three pages
    page 1 - (ID = 202) which will show posts from logo tags
    page 2 - (ID = 210) which will show posts from graphic tags
    page 3 - (ID = 233) which will show posts from design tags
    The IDs are assumed too. You need to find out correct IDs for the pages.

    Now add the following code in your functions.php:

    function changed_featured_posts( $posts ){
    	global $post;
    	switch($post->ID) {
    
    		case 202:
    			$tag_name = 'logo';
    			break;
    
    		case 210:
    			$tag_name = 'graphic';
    			break;
    
    		case 233:
    			$tag_name = 'design';
    			break;
    
    		default:
    			$tag_name = 'featured';
    
    	}
    
    	$layout = get_theme_mod( 'featured_content_layout' );
    	$max_posts = get_theme_mod( 'num_posts_' . $layout, 2 );
    
    	$args = array(
    		'tag' => $tag_name,
    		'posts_per_page' => $max_posts,
    		'order_by' => 'post_date',
    		'order' => 'DESC',
    		'post_status' => 'publish',
    	);
    
    	$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', 'changed_featured_posts', 999, 1 );

    Then go to page.php and change

    if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
    		// Include the featured content template.
    		get_template_part( 'featured-content' );
    	}

    to

    if ( twentyfourteen_has_featured_posts() ) {
    		// Include the featured content template.
    		get_template_part( 'featured-content' );
    	}

    You will need to edit the code to match the ID and tag names, also you may need to edit some css.

    If you want to select layout, in that case, you can remove this code:

    $layout = get_theme_mod( 'featured_content_layout' );

    And add within the switch case block:

    case 210:
    			$tag_name = 'graphic';
                            $layout = 'grid';
    			break;

    or

    $layout = 'slider';

    Hope it works :)

    Cheers
    Ash

  3. Ashok
    WPMU DEV Support Staff
    Posted 1 year ago #

    Okay, this is the final code:

    function changed_featured_posts( $posts ){
    	global $post;
    	switch($post->ID) {
    
    		case 2:
    			$tag_name = 'hello';
    			break;
    
    		case 210:
    			$tag_name = 'graphic';
    			break;
    
    		case 233:
    			$tag_name = 'design';
    			break;
    
    		default:
    			$tag_name = 'featured';
    
    	}
    
    	$layout = get_theme_mod( 'featured_content_layout' );
    	$max_posts = get_theme_mod( 'num_posts_' . $layout, 2 );
    
    	if($layout == 'grid') {
    		add_action('wp_footer', 'add_grid_class');
    	}elseif($layout == 'slider') {
    		add_action('wp_footer', 'add_slider_class');
    		wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
    		wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
    			'prevText' => __( 'Previous', 'twentyfourteen' ),
    			'nextText' => __( 'Next', 'twentyfourteen' )
    		) );
    	}
    
    	$args = array(
    		'tag' => $tag_name,
    		'posts_per_page' => $max_posts,
    		'order_by' => 'post_date',
    		'order' => 'DESC',
    		'post_status' => 'publish',
    	);
    
    	$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', 'changed_featured_posts', 999, 1 );
    
    function add_grid_class() {
    	?>
    	<script type="text/javascript">
    		jQuery(function($) {
    			if(!$('body').hasClass('grid')) $('body').addClass('grid');
    		});
    	</script>
    	<?php
    }
    
    function add_slider_class() {
    	?>
    	<script type="text/javascript">
    		jQuery(function($) {
    			if(!$('body').hasClass('slider')) $('body').addClass('slider');
    		});
    	</script>
    	<?php
    }

    Note that, with the code the control of view (grid or slider) is maintained by Dashboard > Themes > Customize > Featured Content > Layout.

    If you want to different layout in different pages, then you have to edit the code like:

    case 210:
    			$tag_name = 'graphic';
                            $layout = 'grid'; //or slider
    			break;

    Cheers
    Ash

Topic Closed

This topic has been closed to new replies.

About this Topic