- This topic has 10 replies, 2 voices, and was last updated 5 years, 6 months ago by Tom.
-
AuthorPosts
-
October 9, 2018 at 10:26 am #697315Edin
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,
EdinOctober 9, 2018 at 7:32 pm #697578TomLead DeveloperLead DeveloperCreating 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.
October 10, 2018 at 1:02 am #697667EdinThanks 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();
October 10, 2018 at 1:10 am #697670EdinSo 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!
October 10, 2018 at 9:54 am #698107TomLead DeveloperLead DeveloperYou 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.
October 10, 2018 at 10:25 am #698152EdinThank 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).
October 10, 2018 at 5:28 pm #698354TomLead DeveloperLead DeveloperWhat’s the code you’re using that’s generating the error?
October 11, 2018 at 3:20 am #698574EdinWhat 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,
EdinOctober 11, 2018 at 9:30 am #698874TomLead DeveloperLead DeveloperYou’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 ๐
October 12, 2018 at 1:19 am #699230EdinThat works Tom, thank you!
October 12, 2018 at 10:06 am #699620TomLead DeveloperLead DeveloperYou’re welcome ๐
-
AuthorPosts
- You must be logged in to reply to this topic.