[Resolved] Use page-template to create a page?

Home Forums Support [Resolved] Use page-template to create a page?

Home Forums Support Use page-template to create a page?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #697315
    Edin

    I want to create a page which lists all articles in a specific way. Exactly like here: https://jamesclear.com/articles.

    How do I list all articles on the site (and the new ones coming) in that way?

    I thought about taking the page.php of GP and doing it with some WordPress tags, this proved to be difficult.

    Any ideas on how to proceed here?

    Thanks in advance,
    Edin

    #697578
    Tom
    Lead Developer
    Lead Developer

    Creating a page template and then adding a post loop using get_posts() is definitely one solution.

    Another option is using a plugin like WP Show Posts to query posts by category. Then styling them so only the title appears.

    #697667
    Edin

    Thanks Tom.

    WP Show Posts definitely looks interesting, I look into it.

    Stupid question, if I use the page.php as the template, where do I insert the content?

    <?php
    
    /*
    Template Name: Articles_New
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_content_class();?>>
    		<main id="main" <?php generate_main_class(); ?>>
    			<?php
    			/**
    			 * generate_before_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_before_main_content' );
    
    			while ( have_posts() ) : the_post();
    
    				get_template_part( 'content', 'page' );
    
    				// If comments are open or we have at least one comment, load up the comment template.
    				if ( comments_open() || '0' != get_comments_number() ) : ?>
    
    					<div class="comments-area">
    						<?php comments_template(); ?>
    					</div>
    
    				<?php endif;
    
    			endwhile;
    
    			/**
    			 * generate_after_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_after_main_content' );
    			?>
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    get_footer();
    #697670
    Edin

    So using WP Show Posts I would be able to create the exact same articles page as here?

    Is the plugin light-weight? I don’t want to add more strain to the server (I’m fighting with site pseed issues).

    Thank you in advance for the help!

    #698107
    Tom
    Lead Developer
    Lead Developer

    You would replace this line with your custom query:

    get_template_part( 'content', 'page' );

    WP Show Posts would add an extra resource to the page (CSS), but other than that it wouldn’t affect performance.

    #698152
    Edin

    Thank you Tom, very helpful.

    Just one more thing, I was hoping that I could replace:

    get_template_part( 'content', 'page' );

    and add a page title, a few paragraphs of text, then use get_posts() to output articles.

    But it doesn’t work, it gives me an error.

    I understand that this is kind of outside the scope of GP, maybe you can point me to a tutorial or something (couldn’t find anything).

    #698354
    Tom
    Lead Developer
    Lead Developer

    What’s the code you’re using that’s generating the error?

    #698574
    Edin

    What I was trying to do is make the template output a “normal” WordPress page, so that I can just fill in the HTML code and the PHP calls for the archive.

    So I’ve put in the_title( '<h2>', '</h2>' ); to output the page’s title. But it doesn’t format it the way I want to, margins, etc.

    I think that I have to put the CSS class for the page wrapper in there. So I tried wrapping it in <div class="inside-article"></div> and I received the error.

    Maybe you can point me into the right direction?

    Thanks,
    Edin

    #698874
    Tom
    Lead Developer
    Lead Developer

    You’d need to do something like this:

    $args = array (
        'post_type' => 'nav_menu_item',
        'fields' => 'ids',
        'no_found_rows' => true,
    );
    
    $posts = get_posts( $args );
    
    echo '<ul>';
    
    foreach ( $posts as $post_id ) {
        printf( 
            '<li><a href="%1$s">%2$s</a></li>',
            get_permalink( $post_id ),
            get_the_title( $post_id )
        );
    }
    
    echo '</ul>';

    Of course, it will need a little styling to look good ๐Ÿ™‚

    #699230
    Edin

    That works Tom, thank you!

    #699620
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.