 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
12-06-2007, 11:50 AM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Passing a variable to a function
I'm trying to pass a variable to a function in my wordpress skin.
Basically the function (which is a plugin) selects the latest articles from a certain category. What I want to do is define the category number when I call the function.
To call the function I would write:
Code:
<?php echo posts_by_category(); ?>
Is there something I could add so I can define the category number when I call it?
|
|
|
12-06-2007, 12:23 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
As in?
PHP Code:
<?php posts_by_category($id); ?>
But then surely it depends what the function looks like and needs?
|
|
|
12-06-2007, 12:30 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
I can post the code for the function if that will help?
|
|
|
12-06-2007, 12:31 PM
|
#4 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
Yeah post the function
|
|
|
12-06-2007, 12:34 PM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Code:
function posts_by_category() {
$number = "4";
global $wpdb, $post;
$tp = $wpdb->prefix;
$pc_header = get_option('pc_header');
$sort_code = 'ORDER BY name ASC, post_date DESC';
$the_output = NULL;
$last_posts = (array)$wpdb->get_results("
SELECT {$tp}terms.name, {$tp}terms.term_id
FROM {$tp}terms, {$tp}term_taxonomy
WHERE {$tp}terms.term_id = {$number}
AND {$tp}term_taxonomy.taxonomy = 'category'
{$hide_check}
");
if (empty($last_posts)) {
return NULL;
}
$the_output .= stripslashes($ddle_header);
$used_cats = array();;
$i = 0;
foreach ($last_posts as $posts) {
if (in_array($posts->name, $used_cats)) {
unset($last_posts[$i]);
} else {
$used_cats[] = $posts->name;
}
$i++;
}
$last_posts = array_values($last_posts);
foreach ($last_posts as $posts) {
$the_output .='<div class="col">';
$the_output .= '<h2>' . apply_filters('list_cats', $posts->name, $posts) . '</h2>';
$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'" , $r );
$arcresults = $wpdb->get_results("SELECT *, COUNT( `ID` ) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id ={$number}) GROUP BY 1 ORDER BY post_date DESC LIMIT 0 , 5;");
//. $posts->term_id .
$the_output .= '<ul class="latestmenu">';
foreach ( $arcresults as $arcresult ) {
$the_output .= '<li><a href="' . get_permalink($arcresult->ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a></li>';
}
$the_output .= '';
}
$the_output .= '</ul></div>';
return $the_output;
}
function pc_generate($content) {
$content = str_replace("<!-- postsbycategory -->", posts_by_category(), $content);
return $content;
}
As you can see, I've already swapped the category number in the sql to a variable called $number but it will only work if I define $number inside the function. What I need is to be able to define it when I call the function somehow.
|
|
|
12-06-2007, 12:37 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
you need to define a variable within your function such as
PHP Code:
function posts_by_category($number)
|
|
|
|
12-06-2007, 12:39 PM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
I dont understand. How would I implement that into my code?
|
|
|
12-06-2007, 12:44 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
You're trying to pass a variable through to your function, but your function is not set up to receive any information. By including the variable within the function declaration like i showed above, you're assigning any input to that variable, which will be a valid variable now within the function with the value you assigned to it from outside the function.
So you would now take out your constant, thus your code would look like this:
PHP Code:
function posts_by_category($number) {
global $wpdb, $post;
$tp = $wpdb->prefix;
$pc_header = get_option('pc_header');
$sort_code = 'ORDER BY name ASC, post_date DESC';
$the_output = NULL;
$last_posts = (array)$wpdb->get_results("
SELECT {$tp}terms.name, {$tp}terms.term_id
FROM {$tp}terms, {$tp}term_taxonomy
WHERE {$tp}terms.term_id = {$number}
AND {$tp}term_taxonomy.taxonomy = 'category'
{$hide_check}
");
if (empty($last_posts)) {
return NULL;
}
$the_output .= stripslashes($ddle_header);
$used_cats = array();;
$i = 0;
foreach ($last_posts as $posts) {
if (in_array($posts->name, $used_cats)) {
unset($last_posts[$i]);
} else {
$used_cats[] = $posts->name;
}
$i++;
}
$last_posts = array_values($last_posts);
foreach ($last_posts as $posts) {
$the_output .='<div class="col">';
$the_output .= '<h2>' . apply_filters('list_cats', $posts->name, $posts) . '</h2>';
$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'" , $r );
$arcresults = $wpdb->get_results("SELECT *, COUNT( `ID` ) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id ={$number}) GROUP BY 1 ORDER BY post_date DESC LIMIT 0 , 5;");
//. $posts->term_id .
$the_output .= '<ul class="latestmenu">';
foreach ( $arcresults as $arcresult ) {
$the_output .= '<li><a href="' . get_permalink($arcresult->ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a></li>';
}
$the_output .= '';
}
$the_output .= '</ul></div>';
return $the_output;
}
function pc_generate($content) {
$content = str_replace("<!-- postsbycategory -->", posts_by_category(), $content);
return $content;
}
and you would call this function as follows:
PHP Code:
posts_by_category(4)
|
|
|
|
12-06-2007, 12:59 PM
|
#9 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Thanks :) I'll try that out now.
|
|
|
12-06-2007, 01:03 PM
|
#10 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
No prob :)
|
|
|
|
12-06-2007, 01:03 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
I just get an error saying:
Code:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND wp_term_taxonomy.taxonomy = 'category'' at line 7]
SELECT wp_terms.name, wp_terms.term_id FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = AND wp_term_taxonomy.taxonomy = 'category'
|
|
|
12-06-2007, 01:05 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
Did it work correctly with the constant within the function?
|
|
|
|
12-06-2007, 01:07 PM
|
#13 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Yea :( lol
|
|
|
12-06-2007, 01:09 PM
|
#14 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
Try calling your function like this:
PHP Code:
posts_by_category("4")
|
|
|
|
12-06-2007, 01:14 PM
|
#15 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Nope lol I tried "4" and '4' but no luck :(
|
|
|
12-06-2007, 01:22 PM
|
#16 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
|
PHP Code:
WHERE wp_terms.term_id =
It looks like the $number variable isn't getting passed. However adding the ''s as david suggested seems like it would have fixed it.
And you sure it worked correctly before?
EDIT: Also, are you supposed to echo functions? I thought you just wrote it out.
PHP Code:
<?php posts_by_category(); ?>
Not
PHP Code:
<?php echo posts_by_category(); ?>
|
|
|
12-06-2007, 01:23 PM
|
#17 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
|
Just out of interest, how are you calling the function? the full line...
|
|
|
|
12-06-2007, 01:25 PM
|
#18 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
|
Quote:
Originally Posted by d4v1d
Just out of interest, how are you calling the function? the full line...
|
Hehe, yeah I just noticed that. Check my edit Jmz. 
|
|
|
12-06-2007, 01:29 PM
|
#19 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Yea, you can see it working at: http://www.filmzone.org/ if you go to the bottom of the page, he list of trailers is what is generated by the function.
I want to display three lists exactly like that but from three different categories. Is there another way to do it?
I'm calling the function using this:
Code:
<?php echo posts_by_category(); ?>
|
|
|
12-06-2007, 01:30 PM
|
#20 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
|
I didn't test this but seems your problem is on the query. Try this out:
PHP Code:
$last_posts = (array)$wpdb->get_results(sprintf("
SELECT {$tp}terms.name, {$tp}terms.term_id
FROM {$tp}terms, {$tp}term_taxonomy
WHERE {$tp}terms.term_id = '%d'
AND {$tp}term_taxonomy.taxonomy = category
{$hide_check}
",$number));
And call you function this way:
PHP Code:
echo posts_by_category(4);
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|