Page 1 of 1

Trying to add a php include ...

Posted: Fri Aug 14, 2009 7:38 am
by rdkelsey
I have been trying to do this for several months now. I can't seem to make this work, nor can I find the answer online.
(I am not a programmer, for sure, but I can dabble a bit)

This is from a file (archive.php) within a Wordpress blog I run:

<?php single_cat_title(); ?>

If I retype this into the file it will print to screen the name of the category, let's say ... marketing.

What I want to do is have a "php include" that calls a file name that is this "category" (marketing).

<?php
include_once("templates/marketing.php");
?>

like that, where the word "marketing" would come from <?php single_cat_title(); ?>.

I have tried ending the include and inserting the single_cat call ... but of course that doesn't work.

I've tried many different ideas, but like I said, I'm not a programmer.

Can this even be done?

--Robert

Re: Trying to add a php include ...

Posted: Fri Aug 14, 2009 7:47 am
by robnet
Try this:

Code: Select all

 
include('dir/'.function().'.php');
 

Re: Trying to add a php include ...

Posted: Fri Aug 14, 2009 8:28 am
by rdkelsey
Here is what I put into the file:

<?php include ('../../'.single_cat_title().'.php'); ?>

Here is what printed to screen:

Marketing
Warning: include(../../.php) [function.include]: failed to open stream: No such file or directory in /home/ddeu/public_html...etc

Re: Trying to add a php include ...

Posted: Fri Aug 14, 2009 9:06 am
by robnet
Ahaa: I think your function is echoing the string you want. If you use return instead of echo you should be there.

This will output the string to screen before your include statement is processed:

Code: Select all

function getstring(){
 echo "Marketing";
}
While this will return the string after the function has finished, into include(), just as you need:

Code: Select all

function getstring(){
 return "Marketing";
}

Re: Trying to add a php include ...

Posted: Fri Aug 14, 2009 10:19 am
by rdkelsey
i'm not getting something here

I tried:

<?php return('../../../includes/'.single_cat_title().'.inc'); ?>

which simply prints to screen the cat name (marketing or acne or whatever category i am in)

<?php single_cat_title(); ?>
simply prints the cat name to screen

<?php include('../../../includes/'.single_cat_title().'.php'); ?>

First displays to screen the cat name:
Marketing
Then prints the error to screen:
Warning: include(../../../includes/.php) [function.include]: failed to open stream: No such file or directory in /home/adfss/public_html/blog ...... etc

So, it is printing the cat name to screen but it IS NOT putting the cat name into the include where the error says ../../../includes/.php --- it should be /includes/marketing.php

Re: Trying to add a php include ...

Posted: Fri Aug 14, 2009 10:24 am
by jackpf
Do as robnet suggested and use return() then.

Re: Trying to add a php include ...

Posted: Fri Aug 14, 2009 10:25 am
by robnet
The problem is in your function, single_cat_title(). - replace "echo" with "return" - as the example above...

If I have misdiagnosed the problem post the function and I'll have a look.

Re: Trying to add a php include ...

Posted: Sat Aug 15, 2009 6:56 am
by rdkelsey
I finally solved this:

<?php $current_category = single_cat_title("", false); ?>
<?php include_once("/includes/$current_category.inc"); ?>

That code outputs whatever I have in a directory full of include files. I have one file for each category the blog has.

One problem though is that the "single_cat_title" provides me with a name with a Capital Letter to start it, like Marketing instead of marketing.

Is there a way to force it to be lowercase?

Re: Trying to add a php include ...

Posted: Sat Aug 15, 2009 6:59 am
by jackpf
strtolower()

Re: Trying to add a php include ...

Posted: Sat Aug 15, 2009 7:19 am
by rdkelsey
Yes, this works.

<?php $current_category = single_cat_title("", false); ?>
<?php include_once strtolower("includes/$current_category.php"); ?>

This is very cool ... I know am able to pull out a file to include within any page that is relevant to the topic of the page itself ... well, relatively revelant anyway, since categories are rather broad.

I need to dig deeper into php and learn, learn, learn...

THANK YOU

Re: Trying to add a php include ...

Posted: Sat Aug 15, 2009 7:36 am
by jackpf
Cool, no problem.