• Hello all,

    I would like to add some shortcodes, html, and text to the bottom of all posts in a category and all children of that category(about 15 of 40 categories). The problem is my PHP skills are lacking (to say the least).
    example cat 1 <-added to this one
    – example cat2 <-added to this one
    – example cat3 <-added to this one
    — example cat3a <-added to this one
    example cat4 <-NOT added to this one

    I figure the best way to accomplish this is to add a filter to the functions.php using something along the lines of

    add_filter('the_content', 'custom_category_text');

    with a statement like

    if (in_cat(28) || post_is_in_descendant_category(28)){$content =  $content . $custom_category_text;
    }
    return $content;
    }

    So the whole thing would look something like this:

    add_filter('the_content', 'custom_category_text');
    function custom_category_text($content){
    global $post;
    $custom_category_text = 
    
    if (in_cat(28) || post_is_in_descendant_category(28)){$content =  $content . $custom_category_text;
    }
    return $content;
    }

    So, does anyone think this will work?

    If so, then my question becomes “How do I format the PHP in “$custom_category_text =” so that the shortcodes, html, and text all work?”

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    It won’t work as is of course, but the concept is valid. There are no functions like you suggest, you could use wp_get_post_categories() and decide to append content based on the returned array of IDs. This will not tell you if a particular ID is a child of something, but you could get the associated category object and extract the parent’s ID. This could be done recursively until the top parent is found if need be. Or you could possibly hardcode all the children in an array and check if there are any matches.

    With ‘the_content’ filter, shortcodes have been already expanded in the main content. If your appended content also has shortcodes, you need to pass it through do_shortcode() so that those shortcodes are expanded as well.

    Thread Starter deputy963

    (@deputy963)

    Wow, quite a bit more complicated than I though and definitely above my experience, or lack thereof, level. I’m at a loss, with no idea how to proceed.

    Maybe I should just copy/paste the content, though having it automated (and changeable in one location) would be awesome!

    Moderator bcworkz

    (@bcworkz)

    Depending on the frequency of posts that need this treatment, cut ‘n paste may not be that much of a hardship. Still it’s a shame to manually do something when it can be coded. Depending on the nature of your posts, the code may be simpler than I made it sound.

    For example, if the posts only have one category and you are able to enter all the possible category IDs to check for in an array, all you need to do is something like this:

    $cats = array(1,2,3,4); //cat IDs for which to append content
    $post_cats = wp_get_post_categories($post->ID);
    if(in_array($post_cats[0], $cats)) $content .= do_shortcode('some added content with [shortcode]');
    return $content;

    Thread Starter deputy963

    (@deputy963)

    Thanks for the help so far bcworkz!
    Would this still go in the functions.php file as a function?
    As I said, my PHP skills are severely lacking – I can sort of follow along…
    1. creating an array
    2. setting a post_cats variable
    3. using an if statement to say if posts_cats does not = 0 and does = one of the categories in cats then display the code.

    But, the “content” appears complicated to me, at least in PHP terms. There’s 3 shortcodes and html along with inline styling.

    Heck, how much to write the code out for me or spell it out in “even an idiot could do it” language? ??

    Moderator bcworkz

    (@bcworkz)

    Right, goes into your filter function you first proposed. It can be in functions.php or can be made into a simple plugin. The do_shortcode() function expands all shortcodes in the string passed to it, regardless of how many or how complex the included HTML. This is the same function the_content() uses after grabbing content from the DB, it can handle anything suitable for being output to a browser.

    How much? Let us assume you are joking as soliciting or offering payment on this forum will get the thread closed. There is a different forum for this sort of thing if you are serious. I’m happy to make suggestions and offer examples, but I don’t code for anyone, paid or not, on this forum.

    I appreciate your reticence because of your limited skills, but I would suggest you take a stab at getting this working yourself. Most of what you need has been established, you just need to assemble the pieces. If you really get stuck, post what you have so far and you will get more help. It may appear I’m stringing you along, since I could easily provide the answer you need. I’m really doing you a favor, as you learning something along the way, even though it’s a struggle, will serve you better in the long run than just having the answer handed to you.

    If you just need the answer and can’t be bothered to learn, I understand, but I’m not the right person to help you in that case. Assuming you want to learn, here’s a quick lesson about if(in_array($post_cats[0], $cats))

    wp_get_post_categories() always returns an array, even if there is only one category for the post. In order to get a single value out of the array, we use square brackets [] to specify the array index number of the value we want. A zero index will get us the first value in the array. So the if conditional is saying if the first cat ID value in $post_cats is the same as any ID in the $cats array, the condition is true so do the following line of code, which would be appending expanded shortcode content to the post’s normal content. If there were no match, the condition is false and the next line or block is skipped over.

    Thread Starter deputy963

    (@deputy963)

    BC,
    Sorry for the late reply. I was joking, but with my current time restraints maybe posting in the other forum would be best.

    So…

    add_filter('the_content', 'custom_category_text');
    $cats = array(1,2,3,4); //cat IDs for which to append content
    $post_cats = wp_get_post_categories($post->ID);
    if(in_array($post_cats[0], $cats)) $content .= do_shortcode('some added content with [shortcode]');
    return $content;

    The added content is something like this…

    [hide_from level="subscriber"]
    <span style="color: #ff0000;"><strong>This content is exclusively for xxxxxx Members.</strong></span> <strong>If you are a Member please <a href="https://www.xxxxxx.com/member-login/" target="_self">Log In</a>.</strong>
    
    Not a Member? Please Consider <a href="https://www.xxxxxxx.com" target="_self">becoming one for ONLY $18/yr</a>. Membership supports the site and gives users access to many exclusive resources not available to non-members, as well as substantial discounts on store merchandise.
    <ul>
    	<li>Exclusive in-depth content ONLY for subscribers!</li>
    	<li>Large color screenshots illustrate the instructions!</li>
    	<li>Email notification of each new post to the Member’s Area!</li>
    	<li>Exclusive discounts on books and other products!</li>
    </ul>
    [add_to_cart item="1" ajax="yes" ]

    which is where I’m having a big problem as I don’t understand how to format it correctly in PHP so the shortcodes and html work.

    Moderator bcworkz

    (@bcworkz)

    Our dialog is certainly not timely, if you’re pressed for time and are able to pay, try WP Jobs.

    Here is an example complete code framework, though untested. It will at least need the correct category IDs input, and assumes posts have only one category or the first category returned is adequate for filtering purposes:

    add_filter('the_content', 'custom_category_text');
    function custom_category_text( $content ) {
      global $post;
      $cats = array( 1, 2, 3, 4, ); //cat IDs for which to append content
      $text = '[hide_from level="subscriber"]
        <span style="color: #ff0000;">...
          more HTML and/or shortcode
        ...</ul>
        [add_to_cart item="1" ajax="yes" ]';
      $post_cats = wp_get_post_categories( $post->ID );
      if( in_array( $post_cats[0], $cats )) $content .= do_shortcode( $text );
      return $content;
    }

    Ensure any single quotes in your $text assignment are escaped with \'. This can go in functions.php as long as it is inside a <?php ?> block. There’s more efficient ways to do this, but the example is straight forward and (relatively) easy to follow.

    Thread Starter deputy963

    (@deputy963)

    Thanks for all your help (and patience) BC. I’ve tried, and it errors out on the $text variable and I’m sure it has to do with the formatting of the shortcodes/html. There are no single quotes to escape so it has to be incorrectly formatted for PHP (the shortcodes and html).

    Thanks for the time and advice. I’ll give WP Net a shot.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Add shortcodes,text, and htm to a category and its' children’ is closed to new replies.