• soykje

    (@soykje)


    Hello,

    I’m actually trying to get the attachment url for a custom post type I use, for my homepage (http://www.soykje.com/testeur/).

    My problem is that when I change the image of my CPT (an article), I can’t get it on front… Seems like the code doesn’t get the last version. Nevertheless, if I use the the_content function, there is no problem and I get the last version/image of my CPT.

    Do I miss something? Maybe I don’t use correctly the wp_get_attachment_url function?

    Thanks by advance for your help!

    Here is my CPT declaration:

    <?php
    
    add_action( 'init', 'register_homeimg' );
    
    function register_homeimg() {
    	register_post_type('homeimg',
    		array(
    		'labels' => array(
    			'name' => 'Image d\'accueil',
    			'singular_name' => 'Image d\'accueil',
    			'add_new' => 'Ajouter une image d\'accueil',
    			'add_new_item' => 'Ajouter une nouvelle image d\'accueil',
    			'edit_item' => 'Editer une image d\'accueil',
    			'new_item' => 'Nouvelle image d\'accueil',
    			'view_item' => 'Voir l\'image d\'accueil',
    			'search_items' => 'Rechercher une image d\'accueil',
    			'not_found' => 'Aucune image d\'accueil',
    			'not_found_in_trash' => 'Aucune image d\'accueil dans la corbeille',
    			'parent_item_colon' => '',
    			'menu_name' => 'Image d\'accueil'
    		),
    		'public' => true,
    		'show_ui' => true,
    		'menu_icon' => 'dashicons-format-image',
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => 6,
    		'supports' => array('title','editor')
    	));
    }
    
    ?>

    And here is my code, for my homepage:

    <?php
    /*
    Template Name: Accueil
    */
    ?>
    
    <?php get_header(); ?>
    
        <section id="content" class="home">
            <?php $mainimg = new WP_Query('post_type=homeimg'); ?>
            <?php while ($mainimg -> have_posts()) : $mainimg -> the_post(); ?>
    
    		<?php
    			$args = array(
    				'post_type' => 'attachment',
    				'post_mime_type' => 'image',
    				'post_parent' => $post->ID,
    				'post_status' => 'any',
    				'orderby' => 'modified',
    				'order' => 'DESC',
    				'posts_per_page' => 1
    			);
    
    			$attachments = get_posts($args);
    			if ($attachments) {
    				foreach ($attachments as $attachment) { ?>
    
    				<p style="background-image:url(<?php echo wp_get_attachment_url($attachment->ID); ?>)"></p>
    
    		<?php
    			}
    			}
    		?>
    
            <?php endwhile; ?>
        </section>
    
    <?php get_footer(); ?>

  • The topic ‘wp_get_attachment_url and custom post type, not refreshing?’ is closed to new replies.