Het is niet zo'n hoogstaande vraag denk ik, maar ik kom er niet helemaal uit.
Ik ben bezig een plugin voor wordpress aan het editten. In deze plugin wil ik meerdere functies aanroepen. Dat lukt me ook maar ik denk dat ik daarvoor veel-te-veel code gebruik. Ik zoek dus naar een manier om het sneller en stabieler te maken.
Dit is het origineel:
Dit 10 keer onder elkaar plakken werkt, het enige wat ik daarbij hoef te doen is de functie een andere naam geven. Maar dan krijg je denk ik teveel overbodige code.
Vandaar dat ik dit geprobeerd heb (twee functies onder elkaar):
Alle hulp is van harte welkom.
Ik ben bezig een plugin voor wordpress aan het editten. In deze plugin wil ik meerdere functies aanroepen. Dat lukt me ook maar ik denk dat ik daarvoor veel-te-veel code gebruik. Ik zoek dus naar een manier om het sneller en stabieler te maken.
Dit is het origineel:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| function c2c_get_recent_posts ($num_posts = 5, $format = "<li>%post_date%: %post_URL%</li>", $categories = '', // space separated list of category IDs -- leave empty to get all $orderby = 'date', $order = 'DESC', // either 'ASC' (ascending) or 'DESC' (descending) $offset = 0, // number of posts to skip $date_format = 'm/d/Y', // Date format, php-style, if different from blog's date-format setting $authors = '', // space separated list of author IDs -- leave empty to get all $include_passworded_posts = false) { global $wpdb, $tablecomments, $tableposts, $tablepost2cat; if ($add_recent_comment_to_sql && !isset($tablecomments)) $tablecomments = $wpdb->comments; if (!isset($tablepost2cat)) $tablepost2cat = $wpdb->post2cat; if (!isset($tableposts)) $tableposts = $wpdb->posts; if ($order != 'ASC') $order = 'DESC'; if ('max_comment_date' == $orderby) { $add_recent_comment_to_sql = 1; } else { if ($orderby != 'rand()') $orderby = "$tableposts.post_$orderby"; $add_recent_comment_to_sql = 0; } $include_sticky_posts = true; $now = current_time('mysql'); if ($add_recent_comment_to_sql) $sql = "SELECT $tableposts.*, MAX(comment_date) AS max_comment_date FROM $tablecomments, $tableposts "; else $sql = "SELECT DISTINCT * FROM $tableposts "; if ($categories) { $sql .= "LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) "; $cats = explode(' ', $categories); } $sql .= "WHERE $tableposts.post_date <= '$now' AND ( $tableposts.post_status = 'publish' "; if ($include_sticky_posts) $sql .= "OR $tableposts.post_status = 'sticky' "; $sql .= ") "; if (!$include_passworded_posts) $sql .= "AND $tableposts.post_password = '' "; if ($add_recent_comment_to_sql) $sql .= "AND $tableposts.ID = $tablecomments.comment_post_ID AND $tablecomments.comment_approved = '1' "; if ($categories) { $first = 1; $sql .= "AND ( "; foreach ($cats as $cat) { if ($first) $first = 0; else $sql .= "OR "; $sql .= "$tablepost2cat.category_id = '$cat' "; } $sql .= ") "; } if ($authors) { $auths = explode(' ', $authors); $first = 1; $sql .= "AND ( "; foreach ($auths as $author) { if ($first) $first = 0; else $sql .= "OR "; $sql .= "$tableposts.post_author = '$author' "; } $sql .= ") "; } if ('modified' == $orderby) $sql .= "AND $tableposts.post_modified_gmt <= '$now' "; $sql .= "GROUP BY $tableposts.ID ORDER BY $orderby $order"; if ($num_posts) $sql .= " LIMIT $offset, $num_posts"; $posts = array(); $posts = $wpdb->get_results($sql); if (empty($posts)) return; return c2c_get_recent_handler($posts, $format, $date_format); } //end function c2c_get_recent_posts() |
Dit 10 keer onder elkaar plakken werkt, het enige wat ik daarbij hoef te doen is de functie een andere naam geven. Maar dan krijg je denk ik teveel overbodige code.
Vandaar dat ik dit geprobeerd heb (twee functies onder elkaar):
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| function c2c_get_recent_posts ($num_posts = 1, $format = "<li>%post_date%: %post_URL%</li>", $categories = '1', // space separated list of category IDs -- leave empty to get all $orderby = 'date', $order = 'DESC', // either 'ASC' (ascending) or 'DESC' (descending) $offset = 0, // number of posts to skip $date_format = 'm/d/Y', // Date format, php-style, if different from blog's date-format setting $authors = '', // space separated list of author IDs -- leave empty to get all $include_passworded_posts = false) function c2c_get_recent_posts2 ($num_posts = 10, $format = "<li>%post_date%: %post_URL% %post_excerpt_short%</li>", $categories = '2', // space separated list of category IDs -- leave empty to get all $orderby = 'date', $order = 'DESC', // either 'ASC' (ascending) or 'DESC' (descending) $offset = 0, // number of posts to skip $date_format = 'm/d/Y', // Date format, php-style, if different from blog's date-format setting $authors = '', // space separated list of author IDs -- leave empty to get all $include_passworded_posts = false) { global $wpdb, $tablecomments, $tableposts, $tablepost2cat; if ($add_recent_comment_to_sql && !isset($tablecomments)) $tablecomments = $wpdb->comments; if (!isset($tablepost2cat)) $tablepost2cat = $wpdb->post2cat; if (!isset($tableposts)) $tableposts = $wpdb->posts; if ($order != 'ASC') $order = 'DESC'; if ('max_comment_date' == $orderby) { $add_recent_comment_to_sql = 1; } else { if ($orderby != 'rand()') $orderby = "$tableposts.post_$orderby"; $add_recent_comment_to_sql = 0; } $include_sticky_posts = true; $now = current_time('mysql'); if ($add_recent_comment_to_sql) $sql = "SELECT $tableposts.*, MAX(comment_date) AS max_comment_date FROM $tablecomments, $tableposts "; else $sql = "SELECT DISTINCT * FROM $tableposts "; if ($categories) { $sql .= "LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) "; $cats = explode(' ', $categories); } $sql .= "WHERE $tableposts.post_date <= '$now' AND ( $tableposts.post_status = 'publish' "; if ($include_sticky_posts) $sql .= "OR $tableposts.post_status = 'sticky' "; $sql .= ") "; if (!$include_passworded_posts) $sql .= "AND $tableposts.post_password = '' "; if ($add_recent_comment_to_sql) $sql .= "AND $tableposts.ID = $tablecomments.comment_post_ID AND $tablecomments.comment_approved = '1' "; if ($categories) { $first = 1; $sql .= "AND ( "; foreach ($cats as $cat) { if ($first) $first = 0; else $sql .= "OR "; $sql .= "$tablepost2cat.category_id = '$cat' "; } $sql .= ") "; } if ($authors) { $auths = explode(' ', $authors); $first = 1; $sql .= "AND ( "; foreach ($auths as $author) { if ($first) $first = 0; else $sql .= "OR "; $sql .= "$tableposts.post_author = '$author' "; } $sql .= ") "; } if ('modified' == $orderby) $sql .= "AND $tableposts.post_modified_gmt <= '$now' "; $sql .= "GROUP BY $tableposts.ID ORDER BY $orderby $order"; if ($num_posts) $sql .= " LIMIT $offset, $num_posts"; $posts = array(); $posts = $wpdb->get_results($sql); if (empty($posts)) return; return c2c_get_recent_handler($posts, $format, $date_format); } //end function c2c_get_recent_posts() |
Alle hulp is van harte welkom.