WordPress.org

Ready to get started?Download WordPress

Forums

Featured Image Slider - Want to display one category (14 posts)

  1. wbarsby
    Member
    Posted 1 year ago #

    Hi all,

    I'm quite new to WordPress and would like help with restricting a part of my site to showing only a certain product category.

    I have a 'Featured Post Slider' on my site and would like it to only display one category, I have the ID - just have no idea where to edit the code..

    <?php
    
    				 $querydetails = "
    				   SELECT wposts.*
    				   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    				   WHERE wposts.ID = wpostmeta.post_id
    				   AND wpostmeta.meta_key = 'omc_featured_post'
    				   AND wpostmeta.meta_value = '1'
    				   AND wposts.post_status = 'publish'
    				   AND wposts.post_type = 'post'
    				   ORDER BY wposts.post_date DESC
    				 ";
    
    				 $pageposts = $wpdb->get_results($querydetails, OBJECT)
    
    			?>
    			<?php 
    
    			$i = 0;
    			if ($pageposts):
    			foreach ($pageposts as $post):
    			setup_postdata($post);
    			$category = get_the_category();
    			$omc_is_video = get_post_meta(get_the_ID(), 'omc_is_video', true);
    			$i++;
    			$format = get_post_format();
    			if ($i <9) {
    			?>

    Any help would be greatly appreciated.

    Many thanks,

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try changing this [untested]:

    $querydetails = "
    				   SELECT wposts.*
    				   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    				   WHERE wposts.ID = wpostmeta.post_id
    				   AND wpostmeta.meta_key = 'omc_featured_post'
    				   AND wpostmeta.meta_value = '1'
    				   AND wposts.post_status = 'publish'
    				   AND wposts.post_type = 'post'
    				   ORDER BY wposts.post_date DESC
    				 ";
    
    				 $pageposts = $wpdb->get_results($querydetails, OBJECT)

    to this:

    $category_ID = 25;
    $querydetails = "
    				   SELECT wposts.*
    				   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    				   INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id)
    				   INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
    				   WHERE wposts.ID = wpostmeta.post_id
    				   AND wpostmeta.meta_key = 'omc_featured_post'
    				   AND wpostmeta.meta_value = '1'
    				   AND wposts.post_status = 'publish'
    				   AND wposts.post_type = 'post'
    				   AND tt.taxonomy = 'category'
    				   AND tt.term_id = '$category_ID'
    				   ORDER BY wposts.post_date DESC
    				 ";
    
    				 $pageposts = $wpdb->get_results($querydetails, OBJECT);

    Change the category ID in

    $category_ID = 25;
  3. wbarsby
    Member
    Posted 1 year ago #

    Hi Kessiemeijer,

    Many thanks for your reply - it hasn't seemed to have worked though. Is there a way to changed SELECT wposts.* to select category ID '55'? I just dont know the code :\

    Thanks again,

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    it hasn't seemed to have worked though

    Did it return posts or not, or the wrong posts?

  5. wbarsby
    Member
    Posted 1 year ago #

    Oh sorry, it returned no posts at all.

    Thanks,

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Do you still want to use the custom field "omc_featured_post" or only use posts of category 55 for the featured posts?

  7. wbarsby
    Member
    Posted 1 year ago #

    Correct, I only want category 55 to show in the slider.

    My idea is to be able to then choose which posts are visible on my homepage.

    Many thanks,

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Then try changing this:

    $category_ID = 25;
    $querydetails = "
    				   SELECT wposts.*
    				   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    				   INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id)
    				   INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
    				   WHERE wposts.ID = wpostmeta.post_id
    				   AND wpostmeta.meta_key = 'omc_featured_post'
    				   AND wpostmeta.meta_value = '1'
    				   AND wposts.post_status = 'publish'
    				   AND wposts.post_type = 'post'
    				   AND tt.taxonomy = 'category'
    				   AND tt.term_id = '$category_ID'
    				   ORDER BY wposts.post_date DESC
    				 ";
    
    				 $pageposts = $wpdb->get_results($querydetails, OBJECT);

    to this:

    $pageposts = get_posts('cat=55&posts_per_page=10');

    change posts_per_page=10 to how many posts you want to use.

  9. wbarsby
    Member
    Posted 1 year ago #

    Awesome :D

    It works - thank you!

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

  11. satyaki
    Member
    Posted 1 year ago #

    Hi keesiemeijer,

    Sorry for dropping in like this.
    But my problem is some what similar to what wbarsby has faced.

    I am trying to show the next and previous button in my website for single post navigation.
    For this I have used a plugin "WP Single Post Navigation Within Category".

    However, I want this previous and next buttons to appear in only a single category and not in all categories.

    I found in the style.css file of the plugin that the buttons can be removed for anyone accessing my website through a handheld device.

    The code lines were as follows................
    ----------------------------------------------

    @media screen and (max-width: 1024px) {
    .wpspn-area,
    #wpspn-nextpost,
    #wpspn-prevpost {
    display: none;
    }
    }

    -----------------------------------------------

    I guess the line " @media screen and (max-width: 1024px) " can be replaced with a proper code to hide the buttons for all other categories except one.

    I am a complete novice and doesn't have any idea about coding.

    Can you be kind enough to tell me what will be the code, I wish to use my category ID = 21 for showing the buttons.

  12. satyaki
    Member
    Posted 1 year ago #

    Hi experts.

    Any one?

    any idea?

  13. satyaki
    Member
    Posted 1 year ago #

    Can someone help on this?
    I tried to create the plugin support forum also
    But it seems they have withdrawn support for this plugin --- 'WP Single Post Navigation'.

    WOULD REALLY APPRECIATE HELP ON THE TOPIC FROM ANYONE WHO KNOWS ABOUT THE SUBJECT.

  14. keesiemeijer
    moderator
    Posted 1 year ago #

    @satyaki
    Your question has nothing to do with a featured image slider, please continue here: http://wordpress.org/support/topic/interesting-reversal-of-options

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.