WordPress.org

Ready to get started?Download WordPress

Forums

PHP Noob - How do I bring these two things together? (4 posts)

  1. sgreve
    Member
    Posted 1 year ago #

    Hi everybody, I'm a bit of a PHP noob and I've hit a snag with my template. Here's my problem:

    I'm building a page template that will list of 17 different categories and give you the option to donate to them or to click their amazon affiliate link. This is what it looks like right now:

    <?php
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'exclude' => '1,8'
      );
    $categories=get_categories($args);
      foreach($categories as $category) {
        echo
    	'<div class="donateamazon"><a href="#paypal"><img src="'
    	. get_bloginfo( 'stylesheet_directory' ) .
    	'/images/donate-showblue.png" width="156" height="31" alt="Donate to this Category" /></a></div><div class="donateamazon"><a href="#amazon"><img src="'
    	. get_bloginfo( 'stylesheet_directory' ) .
    	'/images/amazon-showblue.png" width="156" height="31" alt="Shop Amazon for this Show" /></a></div>' ;
    }
    ?>

    The two relevant links in here are currently placeholders, #paypal and #amazon. These are where I'm having trouble. These links are different for each category, and up until now I've been storing them as a big if else file and loading it as a template part. Like this (only with a bunch more elseifs in the real thing):

    <?php if(is_category(364) ) {
    echo 'Paypal link #1';
    }
    elseif(is_category(4) ) {
    echo 'Paypal link #2';
    }
    else {
    echo 'Generic Paypal link';
    } ?>

    I just can't figure out is how to integrate my giant list of links into the category listing above. Anybody have any ideas?

  2. Bas Schuiling
    Member
    Posted 1 year ago #

    This is very basic PHP, so I advice you to take a course in PHP , either some free one online or somewhere else if you want to seriously do this :)

    That said you 'echo' your links instead of writing it to variables. And if you want something like that, I advice to take a look at the documentation of the statement ' switch ' and to see how arrays function.

    Good luck.

  3. sgreve
    Member
    Posted 1 year ago #

    Thanks for telling me about the switch statement! Any chance you can be a little more specific? I can usually figure out how/why something works if I have a code snippet to look over.

    I did take a look at the PHP.net manual, but it's really unhelpful if you're not already familiar with scripting languages. It might as well be cyrillic for all the sense it makes to me. :/

  4. Bas Schuiling
    Member
    Posted 1 year ago #

    For instance:

    if(is_category(364) ) {
    echo 'Paypal link #1';
    }

    should be:

    if(is_category(364) ) {
    $paypal_link = 'Paypal link #1';
    }

    and then later do

    <a href="<? echo $paypal_link ?>">Paypal</a>

    ( or shorthand for echo is <?= $paypal_link ?>

    Trust me, by now I know cyrillic though, just study it :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.