Skip to main content
added 49 characters in body; edited tags
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

I'd like to modify the code below so it grabs the data based on the category the post is in and 'only' that category, not children categories, not other categories, only the category of the current post.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_categoryget_the_category() and here - http://codex.wordpress.org/Class_Reference/WP_Query#Category_ParametersWP_Query Category Parameters  , but I'm doing something wrong.

Can someone help meAny suggestion to tweak this?

My category structure is as follows.

ParentA
-childA
-childB
--childB1
--childB2
-childC

ParentA acts as a 'container', there are no posts in it. I'd like posts in category -childA to display data from other posts in -childA and 'only' from -childA, nothing else.

The code below was contributed by Andrei Gheorghiu (thanks again)

 /**
     * tag related posts
     */
    function relatedCategoryPosts() {
$cats = get_the_category();
$html = '';
if ( $cats ) {
    $cat_ids = array();
    foreach ( $cats as $cat ) {
        $cat_ids[] = $cat->ID;
    }
    $posts = get_posts('numberposts=5&orderby=rand&fields=all&category__in='.implode(',',$cat_ids));

    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://www.abcmysitexyz.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
}
return do_shortcode($html);     
}

add_shortcode('related', 'relatedCategoryPosts');

}

add_shortcode('related', 'relatedCategoryPosts');

I'd like to modify the code below so it grabs the data based on the category the post is in and 'only' that category, not children categories, not other categories, only the category of the current post.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_category and here - http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters  , but I'm doing something wrong.

Can someone help me tweak this?

My category structure is as follows.

ParentA
-childA
-childB
--childB1
--childB2
-childC

ParentA acts as a 'container', there are no posts in it. I'd like posts in category -childA to display data from other posts in -childA and 'only' from -childA, nothing else.

The code below was contributed by Andrei Gheorghiu (thanks again)

 /**
     * tag related posts
     */
    function relatedCategoryPosts() {
$cats = get_the_category();
$html = '';
if ( $cats ) {
    $cat_ids = array();
    foreach ( $cats as $cat ) {
        $cat_ids[] = $cat->ID;
    }
    $posts = get_posts('numberposts=5&orderby=rand&fields=all&category__in='.implode(',',$cat_ids));

    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://www.abcmysitexyz.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
}
return do_shortcode($html);     

}

add_shortcode('related', 'relatedCategoryPosts');

I'd like to modify the code below so it grabs the data based on the category the post is in and 'only' that category, not children categories, not other categories, only the category of the current post.

I reviewed the instructions get_the_category() and WP_Query Category Parameters, but I'm doing something wrong.

Any suggestion to tweak this?

My category structure is as follows.

ParentA
-childA
-childB
--childB1
--childB2
-childC

ParentA acts as a 'container', there are no posts in it. I'd like posts in category -childA to display data from other posts in -childA and 'only' from -childA, nothing else.

The code below was contributed by Andrei Gheorghiu (thanks again)

 /**
     * tag related posts
     */
    function relatedCategoryPosts() {
$cats = get_the_category();
$html = '';
if ( $cats ) {
    $cat_ids = array();
    foreach ( $cats as $cat ) {
        $cat_ids[] = $cat->ID;
    }
    $posts = get_posts('numberposts=5&orderby=rand&fields=all&category__in='.implode(',',$cat_ids));

    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://www.abcmysitexyz.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
}
return do_shortcode($html);     
}

add_shortcode('related', 'relatedCategoryPosts');
updated code and changed title.
Source Link
Mr. B
  • 307
  • 5
  • 14

Content Above the post title, but not Related content based on tag or category pagesname

I have the following code working perfectly right now. It grabs info from posts based on the tag found in the page. I'd like to modify itthe code below so it grabs the same info, butdata based on the category the post is in and 'only' that category, not children categories, not other categories, only the tagcategory of the current post.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_category and here - http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters , but I'm doing something wrong.

Can someone help me tweak this so it grabs info based on 'category' instead of 'tag'?

My category structure is as follows.

ParentA
-childA
-childB
--childB1
--childB2
-childC

ParentA acts as a 'container', there are no posts in it. I'd like posts in category -childA to display data from other posts in -childA and 'only' from -childA, nothing else.

The code below was contributed by Andrei Gheorghiu (thanks again)

 /**
     * tag related posts
     */
    function relatedPostsrelatedCategoryPosts(){
    {
        $tags$cats = get_the_tagsget_the_category();
  $html = '';
if ( $tags$cats ) {
    $cat_ids = array();
    foreach ( $tags$cats as $t$cat ) {
            $tag$cat_ids[] = $t$cat->name;
            break;
        }>ID;
    }
        $posts = get_posts('numberposts=5&orderby=rand&fields=all&tag=''numberposts=5&orderby=rand&fields=all&category__in='.$tagimplode(',',$cat_ids));
    
        $html = '';
    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://XXXXXwww.abcmysitexyz.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
        }
return do_shortcode($html);
    
    }
    
    
    add_shortcode('related', 'relatedPosts');

}

add_shortcode('related', 'relatedCategoryPosts');

Content Above the post title, but not on tag or category pages

I have the following code working perfectly right now. It grabs info from posts based on the tag found in the page. I'd like to modify it so it grabs the same info, but based on the category, not the tag.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_category but I'm doing something wrong.

Can someone help me tweak this so it grabs info based on 'category' instead of 'tag'?

 /**
     * tag related posts
     */
    function relatedPosts(){
    
        $tags = get_the_tags();
    if ( $tags ) {
        foreach ( $tags as $t ) {
            $tag = $t->name;
            break;
        }
    }
        $posts = get_posts('numberposts=5&orderby=rand&fields=all&tag='.$tag);
    
        $html = '';
    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://XXXXX.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
        return do_shortcode($html);
    
    }
    
    
    add_shortcode('related', 'relatedPosts');

Related content based on category name

I'd like to modify the code below so it grabs the data based on the category the post is in and 'only' that category, not children categories, not other categories, only the category of the current post.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_category and here - http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters , but I'm doing something wrong.

Can someone help me tweak this?

My category structure is as follows.

ParentA
-childA
-childB
--childB1
--childB2
-childC

ParentA acts as a 'container', there are no posts in it. I'd like posts in category -childA to display data from other posts in -childA and 'only' from -childA, nothing else.

The code below was contributed by Andrei Gheorghiu (thanks again)

 /**
     * tag related posts
     */
    function relatedCategoryPosts() {
$cats = get_the_category();
$html = '';
if ( $cats ) {
    $cat_ids = array();
    foreach ( $cats as $cat ) {
        $cat_ids[] = $cat->ID;
    }
    $posts = get_posts('numberposts=5&orderby=rand&fields=all&category__in='.implode(',',$cat_ids));

    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://www.abcmysitexyz.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
}
return do_shortcode($html);     

}

add_shortcode('related', 'relatedCategoryPosts');

Source Link
Mr. B
  • 307
  • 5
  • 14

Content Above the post title, but not on tag or category pages

I have the following code working perfectly right now. It grabs info from posts based on the tag found in the page. I'd like to modify it so it grabs the same info, but based on the category, not the tag.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_category but I'm doing something wrong.

Can someone help me tweak this so it grabs info based on 'category' instead of 'tag'?

 /**
     * tag related posts
     */
    function relatedPosts(){
    
        $tags = get_the_tags();
    if ( $tags ) {
        foreach ( $tags as $t ) {
            $tag = $t->name;
            break;
        }
    }
        $posts = get_posts('numberposts=5&orderby=rand&fields=all&tag='.$tag);
    
        $html = '';
    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://XXXXX.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
        return do_shortcode($html);
    
    }
    
    
    add_shortcode('related', 'relatedPosts');