mohamad mehdi hajati
mohamad mehdi hajati

Reputation: 21

Is there any chance to solve my post type problem?

I want to show this post type in my index page. this is a carousel of my website and I wanted to add some carousel-caption in it.but it doesn't get my title,excerpt and permalink.what should I have to do to work?

<?php

function my_function() {
    $labels = array(
        'name'               => __( 'slideshow' ),
        'singular_name'      => __( 'slideshow' ),
        'menu_name'          => __( 'slideshow' ),
        'name_admin_bar'     => __( 'slideshow' ),
        'add_new'            => __( 'add picture ' ),
        'add_new_item'       => __( 'slideshow' ),
        'new_item'           => __( 'new post' ),
        'edit_item'          => __( 'edit post' ),
        'view_item'          => __( 'view post' ),
        'all_items'          => __( 'all picturs' ),
        'search_items'       => __( 'search' ),
        'parent_item_colon'  => __( 'parrent' ),
        'not_found'          => __( 'not found' ),
        'not_found_in_trash' => __( 'not found in trash' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'carousel post type' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'carousel' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields','permalink' ));

    register_post_type( 'carousel', $args );
} 

and this is my index code

 <?php $slider = get_posts(array('post_type' => 'carousel', 'posts_per_page' => 3)); ?>
                <?php $count = 0; ?>
                <?php foreach ($slider as $slide) : ?>
                  <div class="carousel-item <?php echo ($count == 0) ? 'active' : ''; ?>">
                <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($slide->ID));?>" class="d-block w-100" alt="...">
                <div class="carousel-caption d-none d-md-block">
                    <h3 class="text-right"><?php echo $post_title = $slide->post_title;?></h3>
                  <div class="text-right"><?php  echo $post_excerpt = $slide->post_excerpt;?></div>
                <div><a href="<?php echo $post_permalink = $slide->post_permalink;?>" class="btn btn-custom float-left" >ادامه مطلب</a></div>
                </div>
               
              </div>
                    <?php $count++; ?>
                <?php endforeach; ?>

Upvotes: 2

Views: 38

Answers (1)

Harit Panchal
Harit Panchal

Reputation: 455

I added your code in my setup and it is getting all the data as mentioned perfectly.

Can you please add more details for the same to understand clearly like if carousel post-type is visible or if you have added posts in carousel post-type?

Also please update you index code with this,

$slider = get_posts(array('post_type' => 'carousel', 'posts_per_page' => 3)); ?>

<?php $count = 0; ?>
<?php foreach ($slider as $slide) : ?>
    <div class="carousel-item <?php echo ($count == 0) ? 'active' : ''; ?>">
        <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($slide->ID));?>" class="d-block w-100" alt="...">
        <div class="carousel-caption d-none d-md-block">
            <h3 class="text-right"><?php echo get_the_title( $slide->ID )?></h3>
            <div class="text-right"><?php  echo $slide->post_excerpt;?></div>
            <div><a href="<?php echo get_the_permalink($slide->ID);?>" class="btn btn-custom float-left" >ادامه مطلب</a></div>
        </div>
    </div>
<?php $count++; ?>
<?php endforeach;

Upvotes: 1

Related Questions