WordPress.org

Ready to get started?Download WordPress

Forums

Two categories on one page. (And sideblog question.) (16 posts)

  1. asfodel
    Member
    Posted 5 years ago #

    I'm trying to figure out how to display two categories on one page, the same way that clicking on a category will display all posts from that category. (I can't just change the categories of each set of posts because one of the categories is excluded from feeds and the other one is not.)

    Also, is there a way to display two categories in the sideblog in chronological order instead of having to display first the posts from one category and then the posts from the other one. (I'm using the sideblog plugin.)

    (Yes, I'm still using 2.1... The theme I'm using is not compatible with later versions so I have to do a major overhaul of the blog to update.)

  2. stvwlf
    Member
    Posted 5 years ago #

    http://codex.wordpress.org/Template_Tags/query_posts

    read the instructions

    if the name of the page you want to display these on is "two-cats" (create a static page for this purpose)

    <?php
    if (is_page('two-cats') {
       query_posts('cat=3,11');
    } ?>

    ====== WordPress loop starts here ==========

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

    You can use similar code in the sidebar to display the 2 cats in chrono order, rather than the plugin

  3. asfodel
    Member
    Posted 5 years ago #

    Thanks for your reply. I tried creating a page called Latest News, but when I enter this code, the whole site just disappears:

    <?php
    if (is_page('latest-news') {
       query_posts('cat=31,39');
    } ?>

    I entered in the main index template just before:

    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
  4. stvwlf
    Member
    Posted 5 years ago #

    hi

    Sorry, I left one thing out. The code should go in page.php not index.php because you are displaying it on a page not on your blog page.

  5. asfodel
    Member
    Posted 5 years ago #

    Thanks. However, when I enter the code in page.php then the pages disappear (it's just a white blank screen if I try to view a page).

    Is this because there's something wonky with my whole site? Whenever I enter a php tag that it doesn't recognize for whatever reason then everything disappears. (I corrected the validation errors a while back, but there are probably some new ones now.)

  6. stvwlf
    Member
    Posted 5 years ago #

    Well, as usual, I see an error in my PHP code - sorry

    <?php
    if (is_page('latest-news')) {
       query_posts('cat=31,39');
    } ?>

    that should work better

  7. roricka
    Member
    Posted 5 years ago #

    Hi,

    OK if I jump in on this thread. I'm a rank newbie to wp, but I was browsing the forums and you (stvwlf) just happened to mention something I'm scratching my head over. It was when you said:

    "The code should go in page.php not index.php"

    Could ya help me a second here? When I create a new "page" (Pages|Add New), it looks to me like no new files are created (so I assumed it's all happening in the database somewhere.) Is that right? IOW, I don't HAVE any files called "page.php," just index.php. I was hoping that new pages WOULD be embodied in a file like a "page.php" so that I could edit them "by hand" after they're created. How, exactly, do I make a "page.php" appear -- or am I totally confused? (obviously!)

    thx in advance,

    roricka

  8. stvwlf
    Member
    Posted 5 years ago #

    Hi Roricka

    page.php is one template file in a WP theme. Themes reside on /wp-content/themes/{themename}/ There is always one active theme in WordPress. If no theme is active it makes the default theme the active theme. http://codex.wordpress.org/Using_Themes

    There are no editable content pages in WP - that's the nature of a dynamic system, or content management system CMS.
    If you are used to editing pages it requires a big paradigm shift.

    All "static pages" created in the WP page editor are displayed using the page.php template file (you can create customized template files). You might picture the system as a giant form letter. The theme template files are the forms, containing static text that appears on every page, and the equivalent of variables for content blocks, that are filled in with data stored in WordPress' database. The variables include things like the post/page content, author, date created, categories or tags assigned to, images attached to the post, etc.

    If you haven't found the Codex (documentation) yet, take a look at
    http://codex.wordpress.org/Main_Page

    It is an adjustment learning to work with WordPress. In my opinion its well worth the effort.
    Hope this helps

  9. asfodel
    Member
    Posted 5 years ago #

    Thank you stvwlf! It's working now. I'd like the posts to have dates, but since my pages don't normally have dates, they don't here either. I'm not sure how to alter it to do that, but it's not very important.

    Re my other question: trying to add posts directly to the sidebar without using the sideblog plugin is a bit beyond my level of expertise, I'm afraid. :) I wouldn't know how to make post appear in the sidebar (I have a vague notion that I would have to paste the <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?> tag to the sidebar.php, and then use query tags to determine which posts would be included?). Plus the sideblog posts are styled differently.

    Thanks for your help.

    And roricka, if the theme you're using doesn't have a page.php and you'd like to have one just to display the pages differently from the single posts, you could create one.

  10. stvwlf
    Member
    Posted 5 years ago #

    hi

    your sidebar code would be like this, not like usual loop code - a custom loop so it doesn't collide with main page loop

    <h3>Recent Articles</h3>
    <ul>
    <?php
      $sidebar = new WP_Query();
      $sidebar->query('cat=31,39');
      while ($sidebar->have_posts()) : $sidebar->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
    </ul>
  11. roricka
    Member
    Posted 5 years ago #

    stvwlf,

    Ah, I see -- funny I didn't think to look in the template folder! Okay, fair enough. In fact, I am very MUCH used to using templates, and I think it's an extremely useful paradigm. But since I have your attention (and perhaps to send me to a more focused search into the codex) just tell me this -- if I want to create a complex page with a lot of table formatting and <select>s, would one normally simply create these as PHP code in the "page"? I see support for lists, but not tables or selects or divs, etc. etc.

    If this is getting too specific, just say so. Otherwise, continued thanks for your help.

    roricka

  12. asfodel
    Member
    Posted 5 years ago #

    Thanks. It works great, and I would love to do away with the sideblog plugin. Now I just have to figure out why the text is displaying in the wrong color... :)

  13. asfodel
    Member
    Posted 5 years ago #

    Perhaps I spoke too soon... the page only displays the last 8 posts, and because it's a static page, there is no link to "previous posts." (I tried putting one in but it didn't work, it just took me back to the same page)

    I've already gotten a lot of help, so I don't expect to have my hand held to figure out every little thing. This is what often happens when I try to change something in my wordpress blog: changing one thing then necessitates changing more things, and I end up not knowing how to go forward.

  14. asfodel
    Member
    Posted 5 years ago #

    Okay, I figured that one out... :)

    <?php
    if (is_page('latest-news')) {
       query_posts('cat=31,39&amp;showposts=20');
    } ?>

    (The forum code added the &amp; -- it doesn't work unless it just has an actual ampersand.)

  15. stvwlf
    Member
    Posted 5 years ago #

    @roricka
    Hi - in that case I take back a bit of what I said earlier. You CAN "hard code" PHP/HTML code in custom page templates. I do this at times to integrate scripts forms etc within the WP framework. Create an empty WP static page in the page editor, assign a custom page template to it, include the header sidebar and footer in the custom page template, and put your code in the page contents area.

    Look at this page
    http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

    Also look at the tinyMCE Advanced plugin which adds TinyMCE plugins to the visual editor. Table support is available there. You'll need to visit the plugin config page after you install it to add additional editor plugins to your toolbar.
    http://wordpress.org/extend/plugins/tinymce-advanced/
    You can add div's etc by editing the WP code on the HTML tab in the post editor.

    The editor tends to cause problems with complex forms as it has a habit of auto-reformatting code - adding break and p tags where it feels they are appropriate, sometimes not what you intend.

  16. roricka
    Member
    Posted 5 years ago #

    stvwlf--

    Thank you thank you thank you.

    I'm off to have fun with all of this!

    roricka

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.