Forums

[resolved] need to find what type of page i'm on (8 posts)

  1. snackd
    Member
    Posted 6 months ago #

    basically right now i just want to load different content for these categories:

      - pages (mostly i'm using pages),
      - home page (which is a page), and
      - multi-page/post (ie, blog or blog post)

    the code i've got almost works:

    function page_typer() {
    global $post;
    
    	if ( is_front_page() ) {
    		$content = 'front page';
    	} elseif ( is_page() ) {
    		$content = 'a page';
    	} elseif ( is_post() ) {
    		$content = 'a post';
    	} else {
    		$content = 'multipage';
    	}
    
    	echo $content;
    }

    except that when i use it on multipost-pages it gives me an

    undefined function is_post()

    error. so my primary question is how do i fix this for multipost. but then i'm also wondering if there's a better (or more inclusive, less likely to break later) way of doing this in the first place.

    i really don't understand why it breaks on is_post() -- it's right in the middle of the function..... any thoughts?

    thanks.

  2. snackd
    Member
    Posted 6 months ago #

    update: figured out there's no is_post(). not sure where i got that. i'm not getting an error, but i'm still wondering if this is even the right way to be doing this?

  3. MichaelH
    moderator
    Posted 6 months ago #

    Guessing you are interested in is_single().

  4. riledhel
    Member
    Posted 6 months ago #

    You can define a specific template (in your theme directory) for each different page and then set, in the WP administration, each page with it's corresponding template. This way, in each template you can load the specific content for it.
    You can define:
    * a template for your home page. let's call it "home_page.php".
    * another template "pages.php" for all your other pages. You don't need to change anything in the WP Administration.
    * and the "index.php" for your blog posts. Same as last one, no need to change the WP Administration.
    Then WP calls each template when suitable. More info here.

  5. snackd
    Member
    Posted 6 months ago #

    @riledhel -- thanks for your input. i wrangled with this idea, but since what i really want is large categories (ie, pages, posts, multipost, home) it seemed it would be easier to write it into my templates. i agree that templates are very easy to use in the admin internface -- but anything i can do to simplify things for the end users.....

    @MichaelH -- yes, exactly. found it. thanks. but i'm still interested in the larger question, that is "is there a better way of doing this"

  6. MichaelH
    moderator
    Posted 6 months ago #

    Template Hierarchy describes what riledhel is saying...

  7. snackd
    Member
    Posted 6 months ago #

    guess i'm just approaching it from a DRY perspective.... why have a bunch of templates that are almost identical. ...of course, DRY can certainly become the enemy of fast.... thanks you both for your help and info.

  8. snackd
    Member
    Posted 6 months ago #

    ps -- look how serious i sound above:

    "i really don't understand why it breaks on is_post()"

    even after quoting the parser itself saying

    "undefined function is_post()"

    someday i'll learn. nite y'all.

Reply

You must log in to post.

About this Topic