WordPress.org

Ready to get started?Download WordPress

Forums

Trouble with If/Else using interwoven PHP calls (4 posts)

  1. RUBYHAUS
    Member
    Posted 1 year ago #

    Can anyone help me understand how to do an if/else with interwoven PHP items starting and stopping?

    I have a Page Title on a Page that is inserting the Category Title on top of the Page Title even though it's a standard page not a post.

    I'd like it to be:
    If cat post this title tag and determine which cat title to post.
    Else remove this tag from standard pages.

    The way it's written works for detecting specific categories, however I wrote with lack of understanding on how to make it NOT show up on normal Pages.

    Help much appreciated.

    Here is what works for detecting cat page vs normal page.

    //ADD CATEGORY TITLE AT TOP OF CAT PAGES
    add_action('pagelines_inside_top_postloop','cat_title');
    function cat_title(){
    if (is_category('news') || ('inspiration') ) {
    ?>
    <h1 class="cat-title"><?php single_cat_title(); ?></h1>
    <?php
    }
    }

    Here is where I'm trying to go with it.

    add_action('pagelines_inside_top_postloop','cat_title');
    function cat_title(){
    if (is_category('news') || ('inspiration') ) {
    	?>
    
    	<?php print "<h1 class='cat-title'> " ?> <?php single_cat_title(); ?> <?php "</h1>";
    
    }
    else {
    	?>
    
      <?php print "<h1 class='cat-title' style='display: hide; visibility: hidden;'> " ?> <?php single_cat_title(); ?> <?php "</h1>";
    }
    }
    ?>
  2. bcworkz
    Member
    Posted 1 year ago #

    I'm not sure what you're really trying to do, but here's some tips. Use get_post_type() to determine if it's a page or post.

    The only reason to exit a php block is to output a bunch of html, as in your first example. You can alternately echo or print html to avoid exiting php. Exiting php can make code harder to follow, especially when you exit for no reason. There's never any reason to see ?> <?php. Ensure every php command line is terminated with a semi-colon; It's easier to follow logic and find errors if your code is clean and orderly without superfluous constructs.

    Don't confuse html and php code. <?php "</h1>";}}?> is not valid. You're missing an echo or print statement. Turn on debugging and error reporting so you are warned of such mistakes.

    Keep working at it, you'll get it.

  3. RUBYHAUS
    Member
    Posted 1 year ago #

    Ok... I completely understand what your saying. I definitely want to get and understand PHP. So though this is going to likely be incorrect to answer your question as to what I'm trying to do...

    I'll try and mock it as better incorrect PHP LOL!!!!!

    //ADD CATEGORY TITLE AT TOP OF CAT PAGES
    add_action('pagelines_inside_top_postloop','cat_title');
    function cat_title(){
     if (get_post_type('post') ) {
       print <h1 class="cat-title"><?php single_cat_title(); ?></h1>
     }
     else (get_post_type('page') ) {
       print DONT PRINT ANY HTML STRIP IT OUT. Cause it prints a blank
             H1 tag when it's a Page.
     }
    }
  4. bcworkz
    Member
    Posted 1 year ago #

    The general logic of your code is fine. Should work once you iron out php syntax errors.

    As you warned, you just need to learn how to properly code php. This isn't the place to get generic php help, but I'll give you a few more things.

    get_post_type() takes a post ID, not a string argument. You then use a comparison operator to check the returned value, something like this:

    global $post;
    if ( get_post_type( $post->ID ) == 'post') {

    print statement needs a string argument, ie 'text in quotes'. Since html needs quotes too, it has to be done in a way that is unambiguous as to which is a php string delimiter and which is html syntax. PHP handles single and double quotes differently, worth learning about.

    The else statement doesn't take a logic argument, it executes it's following code whenever the previous if argument is false. Since you're not really doing anything after the else, you don't need the whole else structure anyway. You could use elseif () for extending logic branching within an if block, which does require a logic argument.

    You have a <?php ?> block within a php block, wrong. To include the results of a function in a string argument, you use the string concatenation dot (.) operator.

    You can find full php documentation and many examples at php.net .

Topic Closed

This topic has been closed to new replies.

About this Topic