开发者

MySQL question: Appending text to a wordpress post, but only if it's in a certain category

开发者 https://www.devze.com 2023-01-19 05:01 出处:网络
I need to amend (via CONCAT, presumably) something to every wordpress post if it belongs to a certain category (say, category ID 7), but I\'m struggling to get it to work.

I need to amend (via CONCAT, presumably) something to every wordpress post if it belongs to a certain category (say, category ID 7), but I'm struggling to get it to work.

To test, I'm first tr开发者_C百科ying to select all the relevant posts. So far, I have the following:

SELECT post_title 
    FROM cruise_wp_posts 
        LEFT JOIN cruise_wp_term_relationships 
        ON  cruise_wp_term_relationships.object_id = cruise_wp_posts.ID
        WHERE term_taxonomy_id = 87;

However, it only lists posts that are only in category 87 - I need all posts that are in category 87 (and possibly other categories too)

I'm a MySQL newbie, and this is really breaking my brain.

Any pointers would be passionately welcomed.


The best way to do it is to filter it in as needed. This way the addition is made everywhere the_content is used and not just in the templates you modify.

<?php

function my_content_concat($the_content) {
    if (in_category(7)) {
        $the_content .= '<br /><br />foo!';
    }
    return $the_content;
}
add_filter('the_content', 'my_content_concat', 9);

?>

in_category can take the id, name or slug of your target category.

I put the filter at 9 so that it runs before WordPress texturizes the content. If you don't need that run it at 11.


Why not just use get_the_category( $id ) and ammend the text when you output the post?

$cat = get_the_category( $postID );
if ($cat == 7) {
  //Add text here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消