• Hello getting these errors on my site:

    Warning: Invalid argument supplied for foreach() in /path/reviews/wp-includes/template-functions-category.php on line 400

    Warning: in_array(): Wrong datatype for second argument in /path/reviews/wp-includes/template-functions-category.php on line 404

    I’m using the custom posts plugins by Ryan. Heres’ the code for the file:

    <?php

    /*
    Plugin Name: Custom Post Templates
    Plugin URI: https://boren.nu/
    Description: Load custom single post templates.
    Author: Ryan Boren
    Version: 0.9
    Author URI: https://boren.nu/
    */

    function cpt_custom_post_template($template) {
    global $wp_query;

    $post = $wp_query->post;
    $id = $post->ID;

    // If a template exists for this post ID, load it.
    if ( file_exists(TEMPLATEPATH . "/single-{$id}.php") )
    return TEMPLATEPATH . "/single-{$id}.php";

    // Add custom checks here. For example, give posts different templates
    // depending on what categories they are in.
    //if ( in_category('1') && file_exists(TEMPLATEPATH . '/single-cat-1.php') )
    // return TEMPLATEPATH . '/single-cat-1.php';

    if ( in_category('2') && file_exists(TEMPLATEPATH . '/single-cat-2.php') )
    return TEMPLATEPATH . '/single-cat-2.php';

    if ( in_category('3') && file_exists(TEMPLATEPATH . '/single-cat-3.php') )
    return TEMPLATEPATH . '/single-cat-3.php';

    if ( in_category('4') && file_exists(TEMPLATEPATH . '/single-cat-4.php') )
    return TEMPLATEPATH . '/single-cat-4.php';

    if ( in_category('5') && file_exists(TEMPLATEPATH . '/single-cat-5.php') )
    return TEMPLATEPATH . '/single-cat-5.php';
    }

    add_filter('single_template', 'cpt_custom_post_template');
    ?>

    What have I done wrong here? I’ll be honest all I did was copy the category template and rename it single-cat-x.php for each cat as it contained the same style I wanted.

    Thanks in advance

Viewing 15 replies - 1 through 15 (of 19 total)
  • Thread Starter jinsan

    (@jinsan)

    bump

    You’re a barrel of fun, Jinsan. :)

    Although the code you’ve copied in doesn’t look complete (where does it return $template?), the errors are due to in_category() not being able to run outside The Loop. A bug was filed on this, but obviously not resolved for 1.5:

    https://mosquito.www.ads-software.com/view.php?id=546

    Perhaps we should be *bugging* one of the lead developers to find out more about this? You know, the one releasing plugins calling iffy functions from the core?

    Thread Starter jinsan

    (@jinsan)

    hmmm well that’s a bugger! so you’re saying that the plugin that could work is wholly complete yet? Any alternatives for getting the post to display the same style as it’s category?

    What if I created an archives template which calls the category related header, sidebar and category.php file? Hard work indeed

    I’ve come up with a moderately clean solution:

    https://guff.szub.net/source/post-templates-by-cat.php

    Change the $catnums array to list all categories you have post templates set up for. You can also alter $template_name, so that if you’re using the same template for a category page AND posts within that category, you can use the same file (i.e. $template_name = 'category'; => category-1.php, category-2.php, etc.).

    Couldn’t test this thoroughly, so let me know if there’s problems.

    Thread Starter jinsan

    (@jinsan)

    that looks promising but you said my original was incorrectly written, right?

    So let me understand this and perhaps you can clarify:

    $catnums = array (2, 3, 4, 5);
    $template_name = 'single-cat';

    This will call each category single-cat file? So it will cal single-cat-2.php, single-cat-3.php etc?

    foreach($catnums as $catnum) {
    $postincat = $wpdb->get_var("SELECT post_id FROM $wpdb->post2cat WHERE category_id = $catnum AND post_id = '$id'");
    if($postincat) {
    if ( file_exists(TEMPLATEPATH . "/$template_name-$catnum.php") )
    return TEMPLATEPATH . "/$template_name-$catnum.php";

    Here I’m not sure what to do – do I replace template_name with ‘category’ as you state, and then following that enter category-1.php? And do I copy and paste that several times within the file for each category template?

    As I said I copied and pasted the category content and placed it in to the single-cat-x.php files so I don’t know if tht’s correct.

    Thanks in advance

    First, do me a favor and download the slightly revised version (I didn’t like that it performed a db query for every category in the $catnums array).

    Now, it should work like this: When on a single post page, the post ID is passed to the plugin. The plugin goes off and *discovers* which categories the post resides in. If one of those categories matches what is in $catnums, it will check for a template that matches.

    The template can be “single-cat-#.php” or “category-#.php” or whatever name you choose, as long as the template filename is SOMETHING-#.php. You define what SOMETHING is through $template_name.

    Example: Categories 1 & 10 are where you want to use a different template for both the category pages and posts within those categories. In fact, you want to use the exact same template for a category and its posts (but different templates for the two categories). Here’s what you’d set up in the plugin.

    $catnums = array (1, 10);
    $template_name = ‘category’;

    So category-1.php will be used for the category 1 page as well as posts within that category. The same goes for category-10.php. (In other words, this reuses the custom category template for posts within that category).

    But say you want custom templates for posts of the categories, but you’re modifying them slightly so one knows they are on a single post page. Then leave $template_name alone, and name your single post templates single-cat-1.php and single-cat-10.php.

    By the way, if anyone grabbed the plugin while I wasn’t looking, you may have hit a buggy edition…the current one there now should be fine.

    Oh, and I’m not sure this has been made clear: Just upload the plugin to your plugins directory and activate it. From there, create your templates, upload to your theme directory, and the plugin should know about them.

    Thread Starter jinsan

    (@jinsan)

    Sounds good will give it a shot. Will report back any sucess. Cheers Kaf.

    Thread Starter jinsan

    (@jinsan)

    Ok I tried it, uploaded it and got this when I tried to activate the plugin:

    Warning: Cannot modify header information - headers already sent by (output started at /path/reviews/wp-content/plugins/custom-post-templates.php:37) in /path/reviews/wp-admin/admin.php on line 6

    Warning: Cannot modify header information - headers already sent by (output started at /path/reviews/wp-content/plugins/custom-post-templates.php:37) in /path/reviews/wp-admin/admin.php on line 7

    Warning: Cannot modify header information - headers already sent by (output started at /path/reviews/wp-content/plugins/custom-post-templates.php:37) in/path/reviews/wp-admin/admin.php on line 8

    Warning: Cannot modify header information - headers already sent by (output started at /path/reviews/wp-content/plugins/custom-post-templates.php:37) in /path/reviews/wp-admin/admin.php on line 9

    This was the only thing I changed in the file:

    $catnums = array(2, 3, 4, 5);
    $template_name = 'category';

    I also tried using template_name = ‘category-‘; as the files are category-1.php, category-2.php etc, but still received the error. Was there more I wa ssupposed to do?

    https://codex.www.ads-software.com/Answers-Trouble_Shooting#Headers_already_sent

    Check for spaces/extra lines before the <?php tag and after the ?> tag in the plugin.

    Thread Starter jinsan

    (@jinsan)

    yeap it was the very first thing I looked for – found the sneaky bugger. And the plugin works (so far) flawleslly. Many thanks Kaf, your assistance is very much appreciated.

    Now…yeah, there is a “now”. Would this same plugin work for pages which obviously don’t have a category id. How would you assign a set template for pages? Or is this more difficult and not possible with the plugin?

    What’s obvious is not always the case…

    Pages do get assigned a category–the default category of 1. Why? I can’t say, and I’m not sure how useful this is in the long run, but it could be used to advantage for your purposes.

    To bring Pages into the plugin’s realm, insert another add_filter line to the end of the plugin:

    add_filter('single_template', 'post_templates_by_cat');
    add_filter('page_template', 'post_templates_by_cat');

    A couple of issues I can see with this: posts in category 1 will use the template, even if you don’t wish it (unless you create a whole other plugin using only the page_template filter and a different template naming scheme). Also, all your Pages will use the custom template, no matter what. I think a smarter option is to prep any templates for use with Pages, then select them manually through Manage >Pages.

    Thread Starter jinsan

    (@jinsan)

    Cool I’ll read up on it when I get back. Cheers again

    Awesome! I’ve been looking for something like this. I installed it and it worked right off the bat. Mahalo!
    https://www.nesurfari.com/

    Hm, I can’t seem to get this to work. Man, I’m having the worst luck lately. It doesn’t seem to load the alternative template. Might it be something with my htaccess not being writable? I swear it is, but for some reason WP refuses to agree. I’ve even CHMOD’ed it to 777 as a test, still no go.

    Ugh… I can’t tell you how long I’ve been trying to set up a separate main page for just some of my categories to be displayed. On WP 1.2 I was able to do this without too much trouble at all. Now, it’s a freakin’ nightmare. I’ve been working on this problem for days.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Invalid Argument on line 404’ is closed to new replies.