Support

Account

Forum Replies Created

  • Hello,

    i try to add values to woocommerce product permalink.

    my code :

    //add rewrite rules 
    function product_add_rewrite_rules() {
    	global $wp_rewrite;
        
    	$wp_rewrite->add_rewrite_tag('%product%', '([^/]+)', 'product=');
    	$wp_rewrite->add_rewrite_tag('%subtitle%', '([^/]+)', 'subtitle=');
    	$wp_rewrite->add_rewrite_tag('%artiste%', '([^/]+)', 'artiste=');
    	$wp_rewrite->add_rewrite_tag('%numero_de_page%', '([^/]+)', 'numero_de_page=');
    	$wp_rewrite->add_permastruct('product', '/catalogue/%artiste%-%product%-%subtitle%-page-%numero_de_page%/', false);
        
        $wp_rewrite->flush_rules();
    }
    add_action('init', 'product_add_rewrite_rules', 10, 0);
    
    // replace the rwrite tag by the content
    function product_permalinks($permalink, $post, $leavename) {
    	$post_id = $post->ID;
    
    	if ($post->post_type == 'product') {
    		$subtitle = sanitize_title(get_field('subtitle', $post_id));
    		$artiste = get_field('artiste', $post_id);
            $artistesurname = sanitize_title($artiste->name);
    		$pagenumber = sanitize_title(get_field('numero_de_page', $post_id));
    		if ($artiste) { 
    			$permalink = str_replace('%artiste%', $artistesurname, $permalink);
    		} else {
    			$permalink = str_replace('%artiste%', 0, $permalink);
    		}
            if ($subtitle) { 
    			$permalink = str_replace('%subtitle%', $subtitle, $permalink);
    		} else {
    			$permalink = str_replace('%subtitle%', 0, $permalink);
    		}
            if ($pagenumber) { 
    			$permalink = str_replace('%numero_de_page%', $pagenumber, $permalink);
    		} else {
    			$permalink = str_replace('%numero_de_page%', 0, $permalink);
    		}
    	}
    
    	return $permalink;
    }
    add_filter('post_type_link', 'product_permalinks', 10, 3);
    

    In the backend it’s showing the change :
    permalink display in the backend

    But in frontend i get a 404. I tried on an other custom post and i get the same.
    Anyone had already the same problem ?

    Thanks in advance,

    Simon

  • Hi John,

    thanks a lot for you help !

    i share the last version code for the community.

    To order images from a gallery according to a date picker field in the attachment :

    <?php 
    // replace by your gallery field name
    $gallery = get_field('field_name');
    
    // vars
    $order = array();
    
    // populate order
    // the above code gets the date field attached to the image. 
    // The 3rd parameter is false to get the unformatted value of the field to sort by.
    foreach( $gallery as $i => $image ) {
    	$order[ $i ] = get_field('date', $image['id'], false);
    }
    
    // multisort
    array_multisort( $order, SORT_DESC, $gallery );
    
    // loop through gallery
    if( $gallery ): 
    
    // variable
    $current_header = ''; ?>
            
    <?php foreach( $gallery as $i => $image ):
    
    $date = get_field('date', $image['ID'], false);
    $temp_header = date_i18n('Y', strtotime($date));
    
        // generate the year as header
        if ( $temp_header != $current_header ) {
            $current_header = $temp_header;
            echo "<h3>".$current_header.'</h3>';
    
        }
        ?>
        
        // add the image and the date
        <div>
            <img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
            <?php the_field('date', $image['ID']); ?>
        </div>
    
    	<?php endforeach; ?>
    
    <?php endif; ?>
    
  • i found a part of the code.
    The probleme is i store the year in a variable and when the loop see an other year, it’s erase the year to an other one.

    So i get many time the same year and the systeme dont put all the images who have the same year in the same header.

    here is my code

    <?php 
    $post_id = "option"; // options page
    $gallery = get_field('galerie_archive', $post_id);
    
    // vars
    $order = array();
    
    // populate order
    foreach( $gallery as $i => $image ) {
    	$order[ $i ] = $image['id'];
    }
    
    // multisort
    array_multisort( $order, SORT_DESC, $gallery );
    
    // loop through gallery
    if( $gallery ): 
    
    $current_header = ''; ?>
            
    <?php foreach( $gallery as $i => $image ): 
    $date = get_field('date', $image['ID'], false);
    
    $temp_header = date_i18n('Y', strtotime($date));
    
        if ( $temp_header != $current_header ) {
            $current_header = $temp_header;
            echo "<h3>".$current_header.'</h3>';
    
        }
            ?>
    
    		<div>
                <img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
                <?php the_field('date', $image['ID']); ?>
            </div>
    
    	<?php endforeach; ?>
    
    <?php endif; ?>
  • Hello,

    the best option should be to migrate your site by uploading all the files of wp on your ftp, changing the database connexion on wp-config.php and then change urls in you phpmyadmin like this :
    select your db
    click on sql tab
    add this script and change what’s needed :
    UPDATE wp_options SET option_value = replace(option_value, ‘http://www.oldurl&#8217;, ‘http://www.newurl&#8217;) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;

    UPDATE wp_posts SET guid = replace(guid, ‘http://www.oldurl&#8217;,’http://www.newurl&#8217;);

    UPDATE wp_posts SET post_content = replace(post_content, ‘http://www.oldurl&#8217;, ‘http://www.newurl&#8217;);

    UPDATE wp_postmeta SET meta_value = replace(meta_value,’http://www.oldurl&#8217;,’http://www.newurl&#8217;);

Viewing 4 posts - 1 through 4 (of 4 total)