• I created a post category selection in the wordpress customizer and working fine. I create a loop and link the the theme_mod to it but my challenge now is that the two are not contenting together. I want an instance where one for an example select politics category out of the numerous category lists in the customizer. it should only display thus.

    Hep me look at my work and amend as necessary. I am a newbie in wordpres development

    
    $categories = get_categories();
    		$blog_cat = array();
    			$i = 0;
    			$blog_cat[]='Select';	
    		foreach($categories as $category){
    			if($i==0){
    			$default = $category->slug;
    			$i++;
    		}
    		$blog_cat[$category->slug] = $category->name;
    	}
    
    // Featured Blog Posts.
    			$wp_customize->add_setting( 'blogger_buzz_featured_blog', array(
    				'sanitize_callback' => 'sanitize_text_field', 	 //done	
    			));
    
    			$wp_customize->add_control(
    				'blogger_buzz_featured_blog', array(
    					'label'			  => esc_html__( 'Select Category', 'blogger-buzz' ),
    					'section'		  => 'iraje_product_featured_catalog',
    					'choices'		  => $blog_cat,
    					'type'    => 'select',
    			));
    	
    
    function iraje_featured_product_loop(){
    	$featured_blog        = get_theme_mod('blogger_buzz_featured_blog');
    	$featured_cat         = explode(',', $featured_blog);
    	
    		
    	$pr_count = get_theme_mod('iraje_product_column_layout',4);	
    	$pr_count = 12 / $pr_count;
    ?>
    <?php if (get_theme_mod('iraje_frontpage_product_display',true)==1){ ?>
    	<div class="container">
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php
    			$args = array(
    				'post_type'      => 'post',
    				'posts_per_page' => - 1,
    			);
    			$q    = new WP_Query( $args );
    		?>
    
    		<div class="row">
    			<?php while ( $q->have_posts() ) : $q->the_post(); ?>
    				<div class="col-sm-6 col-md-6 col-lg-3 col-xl-3">
    					<h3>
    						<a>">
    								<h2><?php the_title(); ?></h2>
    						</a>
    					</h3>
    					<?php the_excerpt(); ?>
    				</div>
    			<?php endwhile; ?>
    			<?php wp_reset_postdata(); ?>
    		</div>
    
    	<?php endwhile; ?>
    </div>
     <?php } ?>
        <?php
    }

    add_action(‘iraje_featured_product_loop’, ‘iraje_featured_product_loop’, 100);

    • This topic was modified 3 years, 9 months ago by bcworkz. Reason: code fixed
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter shaibustephen

    (@shaibustephen)

    This is what i have done and it shows blank when preview at the front end. What am i missing out in the whole thing?

    //Banner
    $wp_customize->add_setting( 'iraje_banner', array(
    'sanitize_callback' => 'iraje_sanitize_sele-ct', //done
    ));
    $wp_customize->add_control(
    'iraje_banner', array(
    'label' => esc_html__( 'Select New Multiple Category', 'blogger-buzz' ),
    'section' => 'iraje_product_featu-red_catalog',
    'type' => 'select',
    'choices' => $blog_cat,
    ));
    /**
    * Banner
    */
    function iraje_banner(){
    $banner = get_theme_mod('iraje-_banner');
    $cat_id = explode(',', $banner);
    ?>
    <div class="row">
    <?php
    $banner_args = array(
    'post_type' => 'post',
    'posts_per_page' => - 1,
    'tax_query' => array(
    array(
    'taxonomy' => 'category',
    'field' => 'term_id',
    'terms' => $cat_id
    ),
    ),
    );
    $banner_query = new WP_Query ($banner_args);
    if ($banner_query->have_posts()):
    while ($banner_query->have_posts()) : $banner_query->the_post();
    ?>
    <div class="col-md-4">
    <h3 class="banner-title">
    <a>"><?php the_title(); ?></a>
    </h3>
    <div><?php the_category( ', ' ); ?></div>
    </div>
    <?php endwhile; endif; wp_reset_postdata(); ?>
    </div>
    <?php
    }
    add_action('iraje_action_banner','iraje_-banner',20);
    • This reply was modified 3 years, 9 months ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    Your customizer code appears to be out of context. Is it within a callback added to the ‘customize_register’ action? It’s the only way I know of to get the proper $wp_customize object.

    If you’re getting a blank white screen, there has likely been a fatal PHP error, like $wp_customize being undefined. Check your error log for specifics, or define WP_DEBUG as true in wp-config.php so errors are displayed within any browser output. A useful setting for development, but it introduces additional security risk on a production site, so don’t leave it as true indefinitely.

    When you post code in these forums, please demarcate with backticks or use the code button. I fixed you posts for your this time. It looks like you tried to do so, but it didn’t work as expected, so you gave up trying. Thanks for the effort anyway. The first backtick needs to be the first character on the line (don’t indent) and there must be another at the end of your code, similar to quote or tag usage. Or highlight the whole thing and click the code button.

    Thread Starter shaibustephen

    (@shaibustephen)

    I dont know if you just help me out. Presently, it pulls up all the entire posts from all the categories lists at once. My challenge now is how to make it display post from each individual category if only is selected. You also talk about ‘callback, how do i add it to ‘customize_register’ action? You also mentioned that my code is out of context, please put me in the right context.

    I am almost frustrated, kindly help me out by putting all the necessary thing to start from there.

    Thanks.

    Thread Starter shaibustephen

    (@shaibustephen)

    After continuous trial, it finally displays correctly without any error shown at the front end. The challenge as i have pointed out is not displaying only single and individual category posts if only one is selected as it stills displays all together.

    Kindly look at my final work and amend where necessary.

    
    /**
     * Product featured Post
     */
    
    function iraje_featured_product_loop(){
    	$featured_blog        = get_theme_mod('iraje_featured_blog');
    	$featured_cat         = explode(',', $featured_blog);
    	
    		
    	$pr_count = get_theme_mod('iraje_product_column_layout',4);	
    	$pr_count = 12 / $pr_count;
    ?>
    <?php if (get_theme_mod('iraje_frontpage_product_display',true)==1){ ?>
    	<div class="container">
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php
    			$args = array(
    				'post_type'      => 'post',
    				'posts_per_page' => - 1,
    			);
    			$q    = new WP_Query( $args );
    		?>
    
    		<div class="row">
    			<?php while ( $q->have_posts() ) : $q->the_post(); ?>
    				<div class="col-sm-6 col-md-6 col-lg-3 col-xl-3">
    					<h3>
    						<a href="<?php the_permalink(); ?>">
    								<h2><?php the_title(); ?></h2>
    						</a>
    					</h3>
    					<?php the_excerpt(); ?>
    					<?php the_category(); ?>
    				</div>
    			<?php endwhile; ?>
    			<?php wp_reset_postdata(); ?>
    		</div>
    
    	<?php endwhile; ?>
    </div>
     <?php } ?>
        <?php
    }
    
    add_action('iraje_featured_product_loop', 'iraje_featured_product_loop', 100);
    
    // Featured Blog Posts.
    			$wp_customize->add_setting( 'iraje_featured_blog', array(
    				'sanitize_callback' => 'sanitize_text_field', 	 //done	
    			));
    
    			$wp_customize->add_control(
    				'iraje_featured_blog', array(
    					'label'			  => esc_html__( 'Select Category', 'blogger-buzz' ),
    					'section'		  => 'iraje_product_featured_catalog',
    					'choices'		  => $blog_cat,
    					'type'    => 'select',
    			));
    	
    
    
    Moderator bcworkz

    (@bcworkz)

    For proper use of the $wp_customize object, see the examples here:
    https://developer.www.ads-software.com/themes/customize-api/customizer-objects/

    To only get posts in specific categories, use the 'category__in' argument for WP_Query. Assign an array of category term IDs that returned posts should have. You get the selected terms with get_theme_mod('iraje_featured_blog'). I’m unsure what we actually get from a stored theme mod, some processing may be required to arrive at an array of IDs appropriate for ‘category__in’.

    Thread Starter shaibustephen

    (@shaibustephen)

    Thanks for your response. The tax_query will only query category post if it’s id or name is specified. This is not what I want. I want an instance where it will display posts only from the specific category when selected at the customizer setting and control without indicating the I’d or name in the tax_query

    from my finding the example you gave me will read thus

    $query->set( ‘category__in’, 23 );

    Thread Starter shaibustephen

    (@shaibustephen)

    For proper understanding what I want is the format for category list drop-down selection in newspaper theme such that when the site administrator select the option of category, posts associated with the individual category will appear in each block section at the front end.

    Moderator bcworkz

    (@bcworkz)

    Tax query requires ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’. There are no other ways to identify a term. You can use tax_query if you like, but I think ‘catregory__in’ will suffice. You can do something like 'category__in'=> [26,32,43,55,], to get posts with any of the listed terms. You do need the term_id for this, other identifiers will not do.

    The best approach might depend on how the customizer object stores the selected terms. Determine this for yourself by making some selections, then getting the related theme_mod and var_dumping it for further study.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Need help on linking customizer section to loop query’ is closed to new replies.