• How to display only 3 posts and certain caregory?

    Swift Basic: mag.php (display/mag.php)

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    <?php
    /**
     * The default template for displaying content
     *
     * @package Swift
     * @subpackage template
     * @since 6.0
     */
    ?>
    <?php
    GLOBAL $count,$swift_thumbnail_sizes,$swift_design_options;
    if( $count % $swift_design_options['cn_ws'] ==  ($swift_design_options['cn_ws'] -1) ){
    	$post_class = 'clearfix mag1 omega';
    }else{
    	$post_class ="clearfix mag1";
    }
    $count++;
    ?>
    
    	<article id="post-<?php the_ID(); ?>" <?php post_class( $post_class.' temp' ); ?>>
    	<div class="div-content">
    
    <?php
    		the_post_thumbnail( $swift_thumbnail_sizes['mag1'], array( 'class'	=> 'mag-thumbnail') );
    ?>
    		<div class="entry-summary">
    			<h2 class="entry-title"><a>" title="<?php printf( esc_attr__( '%s', 'swift' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    			<?php
    			if( isset( $swift_design_options['mag_excerpts_enable'] ) && $swift_design_options['mag_excerpts_enable'] ):
    			?>
    			<div class="excerpt">
    			<?php the_excerpt();
    			?>
    			</div>
    			<?php endif;?>
    						<?php edit_post_link( __( 'Edit', 'swift' ), '<span class="edit-link">', '</span>' ); ?>
    
    		</div><!-- .entry-summary -->
    
    		<footer class="entry-meta clearfix">
    
    			<?php echo get_the_time( get_option( 'date_format' ), get_option( 'time_format' ) ) ?>
    			<?php if ( comments_open() ) : ?>
    			<span class="comments-link alignright"><?php comments_popup_link( _x( '0', '0 comments', 'swift' ), _x( '1', '1 comment', 'swift' ), sprintf( _x( '%s', 'n comments', 'swift' ), '%' ) ); ?></span>
    			<?php endif; // End if comments_open() ?>
    
    			<div class="clear"></div>
    
    		</footer><!-- #entry-meta -->
    	</div>
    	</article><!-- #post-<?php the_ID(); ?> -->
Viewing 3 replies - 1 through 3 (of 3 total)
  • How to display only 3 posts and certain caregory?

    where?
    on the front page?

    your posted mag.php template is only for showing the content; there must be another template with the actual loop …

    to start with the number, check your setting under:
    dashboard – settings – reading – blog pages show …

    Thread Starter superoreh

    (@superoreh)

    Yes on front page.

    Thank you for that dashboard – settings – reading – blog pages show …
    I would never guessed.

    https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    to restrict the posts on the front page (posts page), try adding some code to functions.php;

    this will use the category slug and will also show posts of a sub category of that chosen category:

    function swift_basic_restrict_category_posts_page( $query ) {
       if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'category_name', 'your-cat-slug' );
        }
    
    }
    add_action('pre_get_posts','swift_basic_restrict_category_posts_page');

    alternatively, this will use the category ID and will only show posts directly in that chosen category:

    function swift_basic_restrict_category_posts_page( $query ) {
       if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'category__in', array(123) );
        }
    
    }
    add_action('pre_get_posts','swift_basic_restrict_category_posts_page');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display only 3 posts’ is closed to new replies.