WordPress.org

Ready to get started?Download WordPress

Forums

Categories and sidebar (16 posts)

  1. hj
    Member
    Posted 4 years ago #

    In a recent thread ( http://wordpress.org/support/topic/329520?replies=18 ) Michael very helpfully showed me some code to call category content which was equal to a page name slug. For instance, if a page was called 'Expeditions' then the php code would call the page title links for the category called 'expeditions' into the sidebar. Code was entered into the sidebar using Otto's php widget.

    Michaels code also included an exception to the general rule, whereby the Home Page (using a page called My Projects) could call a category called 'general news' into the sidebar (as there was no category called 'my_projects'). Here is the code:

    <?php
    // on a page, get category  that has slug equal
    // to the page slug, display posts in that category
    if ( is_page() )
    global $posts; {
      $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
    if ($page_name == 'my-projects') {
    $page_name = 'general';
    }
      $category = get_term_by('slug',$page_name, 'category');
      if ($category) {
        $args=array(
          'category__in' => array($category->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Basically this worked great, however it is still a little limiting. For instance I have a page called Background and would like to display in the sidebar the content from the 'general news' category again (there is no category called 'background'). Similarly on the Photos page I might want to show the content (in sidebar) of the 'friends' category.

    This means I want more than one single exception rule, which is currently provided by the lines..

    if ($page_name == 'my-projects') {
    $page_name = 'general';

    I'm not sure how to code this up for different or multiple exceptions (or inclusions! I'm not sure what the right term is!)

    ------------

    The second point is, that if I go to a page with nice sidebar content links provided by the above code....if I click on a link I then get the story but the sidebar content has disappeared...although the title I entered for the sidebar (eg. 'Stories') still shows and there is obviously a sidebar column there. It just has no content.

    I guess once I click on a link to a category posting, WordPress is using a different template?...which is somehow disconnected from the widget code?

    What I want is for the widget PHP code to continue displaying content from a category even when I am no longer on a page but rather in a post?

    Does that make sense?

    Hope someone can shed light on this...and thanks in the first place to Michael for the very useful starter.

    Hugh

  2. MichaelH
    Member
    Posted 4 years ago #

    Had to rearrange things. Another way to not have to test page slugs would be to assign a custom field to each page that represents the category you want displayed in the sidebar.

    <?php
    // on a page, get category that has slug equal
    // to the page slug, display posts in that category
    // on a category archive, display posts in that category
    // on a single post, display posts from the 1st category on that single post
    global $posts;
    if ( is_page() ) {
      $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
      echo $page_name;
      if ($page_name == 'my-projects' || $page_name == 'my-projects') {
        $page_name = 'general';
      } elseif ($page_name == 'something-projects') {
        $page_name == 'whowhatwhere';
      } elseif ($page_name == 'forever') {
        $page_name == 'projectx';
      }
      $category = get_term_by('slug',$page_name, 'category');
      $catid = $category->term_id;
    }
    
    if ( is_category() ) {
      $catid = intval( get_query_var('cat') );
      echo $catid;
    }
    
    if ( is_single( ) ) {
      $ID = $posts[0]->ID;
      $postcat = get_the_category($ID);
      $catid = $postcat[0]->cat_ID;
    }
    
      if ($catid) {
        $args=array(
          'category__in' => array($catid),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><small><?php the_time('F jS, Y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. sosafait
    Member
    Posted 4 years ago #

    hi.

    i'm trying to do the same thing but with pages, having the sidebar navigation show only the parent page and its child pages of the parent or child page you're on. i installed otto's php widget and i've read both this topic and the one linked above, but i don't understand how to change the php to accommodate pages only.

    i have searched the forums and support pages also, and these 2 posts are the closest i've come to a solution.

    can anyone help with how to adjust this for pages only?

    i really appreciate it.

  4. MichaelH
    Member
    Posted 4 years ago #

    sosfait - I recommend the same advice you got at http://wordpress.org/support/topic/331177

  5. hj
    Member
    Posted 4 years ago #

    Hi Michael,

    Thanks for that (we're in different time zones it seems!)

    OK, so what you're saying is..use a series of elseif statements? And there could be no limit to the number of elseif statements you use? Because I may end up with rather a lot...!

    OK, I gave that a try. Here's the elseif section of the code for my test:

    <?php
    // on a page, get category that has slug equal
    // to the page slug, display posts in that category
    // on a category archive, display posts in that category
    // on a single post, display posts from the 1st category on that single post
    global $posts;
    if ( is_page() ) {
      $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
      echo $page_name;
      if ($page_name == 'the-mulu-caves-project' || $page_name == 'the-mulu-caves-project') {
        $page_name = 'general';
      } elseif ($page_name == 'background') {
        $page_name == 'general';
      } elseif ($page_name == 'photos') {
        $page_name == 'general';
      }
      $category = get_term_by('slug',$page_name, 'category');
      $catid = $category->term_id;
    }
    etc. etc....
    ?>

    As you can see, I want the general category links to be displayed on the mulu-caves-project page, the background page and the photos page. At least that's what I think I'm doing...am I right?

    Well, this code is pulling the category general into the mulu-caves-project page only, not into the Background or Photos pages.

    Also, all page sidebars now display the slug name at the top, regardless of whether any category content is being called...which is a bit ugly!

    Also the links are now preceeded by the date stamp which I don't want.

    The first code you gave me worked beautifully in display terms. I'm looking for exactly the same display functionality (title link only), but with the extended elseif options....which I don't think I understand yet!

  6. MichaelH
    Member
    Posted 4 years ago #

    For your display, replace:

    <p><small><?php the_time('F jS, Y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

    with

    <p><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
  7. hj
    Member
    Posted 4 years ago #

    Hi, thanks Michael....unfortunately it's still not going right.

    That last bit of code doesn't seem to work right at all. But neither that nor the original longer full code from yesterday is pulling content into other pages like Bakcground or Photos....and all sidebars are displaying the nasty category slug.

    see screendumps here:

    http://www.dowlass.plus.com/wordpress/wordpresstest.html

  8. MichaelH
    Member
    Posted 4 years ago #

    Sorry, should have confirmed you just want the title displayed.

    So use something like this

    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
  9. hj
    Member
    Posted 4 years ago #

    yeah, thanks.

    But Michael, this is not the main issue. The main issue is that your multiple elseif statements are not pulling in their respective content.

    It could be my coding substitution that's wrong?....I don't know. That's why I repeated it on the page for you. You'll see the categories and pages I'm using if you look at that part of the code....but it's only pulling stuff into the main mulu-caves-project page.

    Hugh

  10. MichaelH
    Member
    Posted 4 years ago #

    Also, all page sidebars now display the slug name at the top, regardless of whether any category content is being called...which is a bit ugly!

    Need to delete the echo $page_name;

    There's also an echo $catid; that should be deleted but before doing that do you see the category id being echoed?

    I guess a link to see the problem would make it easier.

  11. hj
    Member
    Posted 4 years ago #

    I guess a link to see the problem would make it easier.

    Yeah, I wish I could show you, but I'm doing it on a MAMP install on my Mac so not live on the web. I guess I might try and find some webspace somewhere and replicate the site live.

    Stay tuned....

  12. hj
    Member
    Posted 4 years ago #

    Hi Michael,

    Got it running on a live server now, still just test content at the moment:

    http://www.mulucaves.org/wordpress

    'Home' uses the 'Introduction' static page (not sure how to get rid of Introduction from the menu bar?). Your early PHP code chunk (not the later one) has been applied via Otto's widget, with the single exception rule to show category 'news' under the 'introduction' page (so it shows on Home and Introduction pages). The relevant links show nicely according to requirements when these pages are displayed. Good.

    ( ps. One thing to note is that I have had to leave out the Title field in the php widget or else it shows regardless of whether any category links are displayed or not. But I think I managed that bit by changing the words 'List of posts' to 'Relevant Articles'...it will do for the timebeing. )

    In the case of things under the Expeditions page, a category has been set up called RGS Expedition 1977-78, just like the page name. Again, this brings in the relevant article links. (although would it be preferable to use the slug name in the php code rather than the full category name? It just seems long-winded??...But then I suppose it has to match the page name?)

    If I click on the Discovery of Clearwater Cave article link under the RGS Expedition 1977-78 page (and thus category) I get taken to the article....but now my articles list has disappeared from the sidebar. I need to retain the list because my reader is still 'browsing' and reading in the RGS Expedition 1977-78 pages - he/she needs to be able to see other relevant articles related to this expedition.

    Is that clear?

    As mentioned before, I also need one or two additional 'exceptions' to this same pagename/category naming thing. Like the Introduction page calling the News category, I have a Background page which might need to call a Team Members category, or similar. So I need more than one exception rule.

    ....if it's possible, that is!!

    Many thanks for your help on this!

    regards
    Hugh

  13. MichaelH
    Member
    Posted 4 years ago #

    If I click on the Discovery of Clearwater Cave article link under the RGS Expedition 1977-78 page (and thus category) I get taken to the article....but now my articles list has disappeared from the sidebar. I need to retain the list because my reader is still 'browsing' and reading in the RGS Expedition 1977-78 pages - he/she needs to be able to see other relevant articles related to this expedition.

    But Discovery of Clearwater Cave is an article, not a page and the code above does have if ( is_page() ) {.

    I'm wondering if it isn't better for you to use a custom field on your Pages that is the Category of posts you want displayed in your related posts section?

  14. hj
    Member
    Posted 4 years ago #

    Yes you're right, Discovery of Clearwater is an article I suppose that messes it up, although to the viewer it's just a page of information. I guess WP doesn't see it like that, though..

    OK, how would you use a 'custom field'....? sounds like the right idea, but no idea how to apply it?

  15. MichaelH
    Member
    Posted 4 years ago #

    Could this be simplified even more -- if you are just trying to show, in a sidebar.php or via a Widget, posts related to the current page/post, then maybe a related posts plugin might be more flexible.

    Such as:
    http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/

  16. hj
    Member
    Posted 4 years ago #

    That's a possibility, although configuring that plugin itself sounds a bit of a job ;)

    I'd still be interested to learn how you would have implemented a custom fields solution?

    thx

Topic Closed

This topic has been closed to new replies.

About this Topic