WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom sidebar for each page of an archive or category? (14 posts)

  1. HopefulGJL
    Member
    Posted 1 year ago #

    Hello,

    I have my solution for custom sidebars on my pages and my individual posts. But now, I'm trying to find the solution for archives and categories.

    I have this in my page php file for pages where 2 is the page id# and sidebar2 is the custom sidebar I have for that one page:

    <?php if (is_page('2')) : ?>
      	<?php get_sidebar('sidebar2'); ?>
      <?php else : ?>
       	<?php get_sidebar(); ?>
      <?php endif; ?>'

    I have this in my single post php file for posts where 100 is the post id# and homepage is the custom sidebar I have for that one post:

    <?php if (is_single('100')) : ?>
      	<?php get_sidebar('homepage'); ?>
      <?php else : ?>
       	<?php get_sidebar(); ?>
      <?php endif; ?>

    But, I am now wondering what the code is to target specific pages for archives and categories?

    Please note, I do not want to specify the entire archive nor the entire category; I'm looking to specify each individual "page" of the archives and categories.

    For example, if I have website.com/category/communication/page/4 or website.com/category/communication/communication-series/page/3 as the url, how do I target each "page" specifically?

    Basically, no matter where/when Article A appears, I want one and only 1 specific sidebar for only Article A; and then no matter where/when Article B appears, I want one and only 1 specific sidebar for only Article B where the sidebar for Article A appears nowhere else but Article A, etc.

    I am aware of the is_archive() function, but according to this Codex article, "...is_archive() does not accept any parameters..."

    I am also aware of this conditional tags codex article, but the category slug isn't working for my purposes (probably because of the url structure as referenced above) and the archive information of this Codex article leads me back to the first codex article I linked to that states 'it doesn't accept parameters'.

    Please note that I do not want to use a plugin for this.

    I am also an incredible newbie to code, so I'm asking for specifics and code examples as much as possible.

    Thanks in advance for any information you can provide!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    I think you will find Conditional_Tags very useful.

  3. HopefulGJL
    Member
    Posted 1 year ago #

    Thank-you for the quick reply. I am aware of that Codex page, but perhaps you can let me know what on that Codex page might work?

    Please note what I've already tried from that Codex page from the above information.

    Thanks!

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Try is_category().

  5. HopefulGJL
    Member
    Posted 1 year ago #

    Maybe there's something I need to do with it then as I so far haven't been able to get that to work.

    I'm assuming this goes in to my archives php file?
    Or, should it go elsewhere?

    For this url: website.com/category/communication/page/4, I have so far tried this:

    <?php if (is_category('category/communication/page/4')) : ?>
      	<?php get_sidebar('custom_sidebar'); ?>
      <?php else : ?>
       	<?php get_sidebar(); ?>
      <?php endif; ?>

    And

    <?php if (is_category('article-slug-100')) : ?>
      	<?php get_sidebar('custom_sidebar'); ?>
      <?php else : ?>
       	<?php get_sidebar(); ?>
      <?php endif; ?>

    But, those aren't working for website.com/category/communication/page/4.

    Would you happen to know how it can be altered so that it does work?

    Thanks!

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Try <?php if (is_category('communication')) : ?>

  7. HopefulGJL
    Member
    Posted 1 year ago #

    Thank-you; yes, that does work for the entire category.

    But, how do I make it so that the 1 sidebar appears on only one and only one page of the communication archives and not all of the communication archive 'pages'?

    Basically, how I specifically target only one 'page'/'article'/'post' out of all the 'pages'/'articles'/'posts' of the communication archives?

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    See is_paged().

  9. HopefulGJL
    Member
    Posted 1 year ago #

    Ok, thanks, I tried that:

    <?php if (is_paged('category/communication/page/4')) : ?>
      	<?php get_sidebar('custom_sidebar'); ?>
      <?php else : ?>
       	<?php get_sidebar(); ?>
      <?php endif; ?>

    And

    <?php if (is_paged('article-slug-100')) : ?>
      	<?php get_sidebar('custom_sidebar'); ?>
      <?php else : ?>
       	<?php get_sidebar(); ?>
      <?php endif; ?>

    But, that did not work either.

    This article states is_paged() also does not accept any parameters.

    Is there something that can be altered so that the is_paged() can work, or will it not work since that Codex says it doesn't accept parameters?

    This is perplexing.

    Would you happen to have another idea for me to try?

    Thanks!

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    <?php if (is_category('communication') && !is_paged()) : ?> will result in the sidebar being displayed on the first page only.

  11. HopefulGJL
    Member
    Posted 1 year ago #

    Ok, thanks for the additional information.

    Is there a way to target each page instead?

    I do want one specific sidebar on that first page, but then I'm looking to have a different sidebar displayed on the second page, and so on.

    Thanks!

  12. esmi
    Forum Moderator
    Posted 1 year ago #

    That's not something I've tried before. Try adding

    <?php global $pagenow;
    echo $pagenow;>

    to see if you can test against $pagenow.

  13. HopefulGJL
    Member
    Posted 1 year ago #

    I don't know enough about code to understand what you are referring to. I don't know what you mean by test against..?

    Do you mean add that piece of code in some way to my above code?

  14. HopefulGJL
    Member
    Posted 1 year ago #

    Well, I don't know how to do the above suggestion, but through a lot of other searching around, I found a completely different idea (by accident) that I will utilize instead of trying to track down this specific answer.

    I went in to my WordPress reading settings and changed the number of posts to show per page at a very high number. Initially, I had it set to view one blog post per page.

    Next, I learned about the <?php the_excerpt(); ?> code from this codex article.
    I replaced the the_content with the_excerpt in my archive php file (which I think is category php file for some of you). I also replaced the get_content with the_excerpt in my index php file as well. Having the_excerpt in my archive php file is making all of my blog posts in my categories/archives appear as excerpts. Having the_excerpt in my index php file is making all of my search results appear as excerpts as well. Plus, it made it so that the page I have all of my blog posts appear on show all of the posts as excerpts instead of the full articles.

    Then, in order to have a 'read more' link at the end of the excerpt, I learned about this function from this article that needs to be placed in to your functions php file. I put it at the bottom of the functions php file, and changed the text it displays.

    The other good thing about stumbling upon this solution instead of finding what I was originally looking for above is that it eliminates the potential possibility of Google viewing me as maybe having 'duplicate content'. With the full articles previously appearing under all of those different url's, I read on some forums that there's a debate out there as to whether or not Google would consider that duplicate content even though it's all on the same site.
    So, this solution as I've described it should hopefully eliminate that possibility since now the full article should be appearing in only one place on my site.

    And, since my categories/archives will now appear on only one page, I should be able to make my above code structure work with the is_category( '9' ) conditional tag as described here.

    I will now mark this topic as resolved since this is the solution that I have instead come up with.

    If anyone wants to learn more about how I made my custom sidebars, feel free to read this information.

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic