WordPress.org

Ready to get started?Download WordPress

Forums

Need to make a page display posts... (16 posts)

  1. CaptainSEO
    Member
    Posted 3 years ago #

    I want my page /uploaded-documents/ to only show posts from the category "shared" and nothing else.

    How do I do that?

  2. jessn
    Member
    Posted 3 years ago #

    Do you know how to make a custom page template? You can make one just for that page to use and then use a custom query to pull up posts only for that category like so:

    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('category_name=shared'.'&paged='.$paged);
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
  3. CaptainSEO
    Member
    Posted 3 years ago #

    Aha - I see.

    Thanks. I'll go away and try that right now.

  4. CaptainSEO
    Member
    Posted 3 years ago #

    And where exactly does it go. I placed the code where I thought logical but it stops the page loading.

    [Large code excerpt removed by moderator per forum rules. Please use the pastebin for all large code excerpts. It works better anyway.]

  5. CaptainSEO
    Member
    Posted 3 years ago #

    First I've heard of the pastebin. I'll go do that and come back here with the link or whatever.

  6. CaptainSEO
    Member
    Posted 3 years ago #

  7. jessn
    Member
    Posted 3 years ago #

    Ok so what you would do is make a copy of your page.php file and rename it to something else, like page_shared.php. Then at the very top of the page paste:

    <?php/*Template Name: Shared Page */?>

    Upload that to the server and then when editing that page from the admin pick it as a page template.

    Then replace the loop with the code that I gave you. Note that it starts with a "while" statement, no "if" so at the bottom of the loop make sure it ends only with <?php endwhile; ?>. If there's an endif; in there it will throw an error.

  8. CaptainSEO
    Member
    Posted 3 years ago #

    I'm sorry - I'm a real PHP newbe.

    I done the template name bit and selected it. What I can't work out is what a loop is?

  9. CaptainSEO
    Member
    Posted 3 years ago #

    Can you show me in the pastebin link? I would really appreciate it.

  10. CaptainSEO
    Member
    Posted 3 years ago #

    This is the code I have in the page template, but I get no posts shown on the page:

    <?php /* Template Name: share template
    */ ?>
    <html>
    <?php get_header(); ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('category_name=share'.'&paged='.$paged);
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <?php endwhile; ?>
    <?php get_footer(); ?>
  11. FYI, CaptainSEO, your posts are being caught as spam by Akismet. I keep cleaning them out of the queue, but it'll be a while before it learns :/ That means there's a weird delay in your posts showing up for everyone else until that happens, alas. Wish I knew why it got that way...

  12. CaptainSEO
    Member
    Posted 3 years ago #

    I also notice that I am shown as anonymous instead of my username.

  13. Yeah, that happens when we haven't un-spammed your reply quite yet *sigh*

  14. CaptainSEO
    Member
    Posted 3 years ago #

    I am using a plug-in for now, but any further help will be appreciated.

  15. jessn
    Member
    Posted 3 years ago #

    Ok so starting at line 87:

    <?php
    if ( have_posts() )
    while ( have_posts() ) : the_post();  ?>

    That's called "the loop". It's looking to see if there are any posts, and if so loop through each one and display it. So you'd replace that bit with the code I showed you. It's starts with the "if (have_posts)" part, but I don't actually see where it ends that if statement...let us know if, after you replace the code, it throws an error. Something like "unexpected endif".

    I'll see if I can modify the pastebin code for you...

    Oh and you don't need that <html> tag towards the top of your page template. The get_header call should pull in your header.php file which should already have that.

  16. CaptainSEO
    Member
    Posted 3 years ago #

    Got it thanks - works a treat.

    Very much appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.