0

In my header.php I have the typical header code - this is for woocommerce but the coding has nothing to do with it:

<h1 class="page_title"><?php woocommerce_page_title(); ?></h1>

Now I have edited that somewhat to show as:

<table style="background-image: url('url');><tr><td><?php woocommerce_page_title(); ?></td></tr></table>

I have pretty permalinks on my site, and I was wondering if it was possible to change the url of the image depending on the category specified in the permalink? I have two categories, so I imagine an IF function should do it, but I'm not sure how to go about it.

4
  • do you mean a category archive, or a single post with a category slug within the URL?
    – Milo
    Dec 17, 2014 at 0:29
  • Well it would be on the category archive and on the single post. Basically it would be great if the function could check if (for example) aaa is included within the url
    – CalvT
    Dec 17, 2014 at 0:31
  • Are you familiar with Conditional Tags?
    – Milo
    Dec 17, 2014 at 0:38
  • @Milo Interesting! I hadn't 'met' Conditional Tags before. But sorry to be a pain, any chance you could point me in the right direction for the IF function? Thanks
    – CalvT
    Dec 17, 2014 at 0:53

2 Answers 2

0

If you are dealing with a category page, you would simply use conditional tags, as suggested by @milo:

if ( is_category( 'cat-1' ) ) {
  $url = 'my-background-image-url-1.ext';
} else if ( is_category( 'cat-2' ) ) {
  $url = 'my-background-image-url-2.ext';
}

remember, you could pass in a category slug as well as a category name or an array containing any of these values.

if you are dealing with woocommerce categories, you could use the woocommerce category image as background image and then query the woocommerce category. if you want to use the woocommerce category image soewhere else, you could simply hardcode the image corresponding to the category slug like:

if ( is_product_category() ){
  global $wp_query;
  $cat = $wp_query->get_queried_object();
  $my_slugs = array( 'category-slug-1', 'category-slug-2' );
  if ( in_array( $cat->slug, $my_slugs ) {
    $url = 'my-background-image-url-' . $cat->slug . '.ext';
  }
}
1
  • So what about sub-categories of the category? Say category A has sub-category CD, will it still show the category A's img url? Because this is what I want, and the reason I wanted to base it off the url of the page
    – CalvT
    Dec 17, 2014 at 15:24
0

In the end I managed to use this:

// retrieves the page url
function getUrl() {
  $url  = 'http' . ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) ? 's' : '';
  $url .= '://' . $_SERVER["SERVER_NAME"];
  $url .= $_SERVER['REQUEST_URI'];
  return $url;
}

// checks to see if text1 is included in the url. If it is, then the output is url1. If it isn't included, the output is url2.
function imgSrcF() {

$a = getUrl();

if (strpos($a,'text1') !== false) {
    $imgsrc = 'url1';
} else {
    $imgsrc = 'url2';
}

 echo $imgsrc;
}

On the page itself, to display the image I used <?php imgSrcF(); ?> to generate the url:

<table style="background-image: url('<?php imgSrcF(); ?>'); ">

I imagine it also could be used like this:

<img src="<?php imgSrcF(); ?>" />

If you want to display the url on the page:

This goes in functions.php

function writeUrl() {
  echo "The current page url is ".getUrl();
}

Then on the page use something like this

<p><?php writeUrl(); ?></p>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.