Мой пользовательский тип сообщения в WordPress не отображает контент через короткий код - PullRequest
1 голос
/ 09 июля 2020
function diwp_services_custom_post_type()
{
  $labels = array(
        'name' => 'Services',
        'singular_name' => 'Service',
        'add_new'    => 'Add Service',
        'add_new_item' => 'Enter Service Details',
        'all_items' => 'All Services',
        'featured_image' => 'Add Service Image',
        'set_featured_image' => 'Set Service Image',
        'remove_featured_image' => 'Remove Service Image'

    );


    // Set Options for this custom post type;

    $args = array(
        'public' => true,
        'label'       => 'Services',
        'labels'      => $labels,
        'description' => 'Services is a collection of services and their info',
        'menu_icon'      => 'dashicons-hammer',
        'supports'   => array('title', 'editor', 'thumbnail'),
        'capability_type' => 'page',

    );

    register_post_type('services', $args);
}

add_action('init', 'diwp_services_custom_post_type');
// >> Create Shortcode to Display Services Post Types

function diwp_create_shortcode_services_post_type()
{

  $args = array(
        'post_type'      => 'services',
        'posts_per_page' => '10',
        'publish_status' => 'published',
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) :

        while ($query->have_posts()) :

            $query->the_post();
            $result .= '<div class="card">';
            $result .= '<div class="card-block block-1">';
            $result .= ' <h3 class="card-title">' . get_the_title() . '</div>';
            $result .= ' <p class="card-text">' . get_the_content() . '</div>';
            $result .= '</div>';
            $result .= '</div>';

        endwhile;

        wp_reset_postdata();

    endif;

    return $result;
}

add_shortcode('services-list', 'diwp_create_shortcode_services_post_type'); 
 
// shortcode code ends here

Я посетил ссылку ниже и реализовал то же самое, но не нашел результатов DIVEIN WP

Вот скриншот, когда я работаю на localhost введите описание изображения здесь

Любая помощь по этому поводу будет принята. Спасибо

1 Ответ

0 голосов
/ 09 июля 2020

Сначала вы должны получить постоянную ссылку из Настройка >> постоянная ссылка и нажать Сохранить изменения

секунд. Теперь вы должны добавить do_shortcode в нужную позицию. показать все сообщения из настраиваемого типа сообщения, например:

echo do_shortcode('[services-list]');

Я тестирую ваш код на своей стороне и работаю хорошо https://abukotsh.me/wp/services/test-service/

Прокрутите вниз, и вы увидите тестовые услуги

...