【问题标题】:WordPress job and resume board not workingWordPress 工作和简历板无法正常工作
【发布时间】:2022-12-21 01:36:24
【问题描述】:

我刚刚开发了一份工作并继续为我的网站上传代码,希望它能正常工作。我正在使用代码 sn-ps 插件来添加代码。 添加代码后,我在仪表板菜单上获得了菜单快捷方式:如图所示。

enter image description here

我如何开发短代码以添加到页面。

附上开发代码:

// This file contains the code needed to create a job and resume plugin for your WordPress site.
// First, we need to create a function that will register the custom post type for our job and resume posts.
function register_job_resume_post_type() {
    $labels = array(
        'name'               => 'Jobs & Resumes',
        'singular_name'      => 'Job & Resume',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Job & Resume',
        'edit_item'          => 'Edit Job & Resume',
        'new_item'           => 'New Job & Resume',
        'all_items'          => 'All Jobs & Resumes',
        'view_item'          => 'View Job & Resume',
        'search_items'       => 'Search Jobs & Resumes',
        'not_found'          => 'No jobs & resumes found',
        'not_found_in_trash' => 'No jobs & resumes found in Trash',
        'parent_item_colon'  => '',
        'menu_name'          => 'Jobs & Resumes'
    );
   $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'job-resume' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'job-resume', $args );
}
// We also need to create a function to register the custom taxonomies for our job and resume posts.
function register_job_resume_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => 'Job & Resume Categories',
        'singular_name'     => 'Job & Resume Category',
        'search_items'      => 'Search Job & Resume Categories',
        'all_items'         => 'All Job & Resume Categories',
        'parent_item'       => 'Parent Job & Resume Category',
        'parent_item_colon' => 'Parent Job & Resume Category:',
        'edit_item'         => 'Edit Job & Resume Category',
        'update_item'       => 'Update Job & Resume Category',
        'add_new_item'      => 'Add New Job & Resume Category',
        'new_item_name'     => 'New Job & Resume Category Name',
        'menu_name'         => 'Job & Resume Categories'
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'job-resume-category' ),
    );

    register_taxonomy( 'job-resume-category', array( 'job-resume' ), $args );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        'name'                       => 'Job & Resume Tags',
        'singular_name'              => 'Job & Resume Tag',
        'search_items'               => 'Search Job & Resume Tags',
        'popular_items'              => 'Popular Job & Resume Tags',
        'all_items'                  => 'All Job & Resume Tags',
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => 'Edit Job & Resume Tag',
        'update_item'                => 'Update Job & Resume Tag',
        'add_new_item'               => 'Add New Job & Resume Tag',
        'new_item_name'              => 'New Job & Resume Tag Name',
        'separate_items_with_commas' => 'Separate job & resume tags with commas',
        'add_or_remove_items'        => 'Add or remove job & resume tags',
        'choose_from_most_used'      => 'Choose from the most used job & resume tags',
        'menu_name'                  => 'Job & Resume Tags'
    );

    $args = array(
        'hierarchical'          => false,
        'labels'                => $labels,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'rewrite'               => array( 'slug' => 'job-resume-tag' ),
    );

    register_taxonomy( 'job-resume-tag', 'job-resume', $args );
}

// Finally, we need to register our custom post type and taxonomies with WordPress.
 add_action( 'init', 'register_job_resume_post_type' );
 add_action( 'init', 'register_job_resume_taxonomies', 0 );

帮我看看怎么解决。

了解如何使其工作并开发简码以便于安装

【问题讨论】:

    标签: php wordpress code-snippets


    【解决方案1】:

    您将使用 add_shortcode() 函数。该函数有两个参数:短代码的名称和显示短代码内容的回调函数。请参阅下面的代码。

    function namespace_cpt_shortcode() {
        $args = [
            'post_type' => 'job-resume'
        ];
    
        $the_query = new WP_Query( $args );
    
        // The Loop
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) { 
                $the_query->the_post();
                
                // Save the data you wish to show in the post
                $postTitle = get_the_title();
                $postContent = get_the_content();
                
                //Save the data to the variable that will be returned in the shortcode.
                $result = "
                <div class='post-container'>
                <h2>$postTitle</h2>
                <p>$postContent</p>
                </div>";
    
            } // End while loop
        } else {
            $result = "No posts found.";
        }
        /* Restore original Post Data */
        wp_reset_postdata();
        
        return $result;
    }
    // Use add_shortcode() to create the shortcode. First parameter is the name of the shortcode and the second is the function name.
    add_shortcode( 'job-listing', 'namespace_cpt_shortcode' );
    

    如果您想向布局添加更多帖子信息,您可以将其添加到 $result 变量中,因为这是函数中返回的内容,因为短代码需要返回值。

    【讨论】:

      猜你喜欢
      • 2013-07-09
      • 1970-01-01
      • 2012-07-04
      • 2014-12-07
      • 2016-07-29
      • 2016-09-12
      • 2018-02-17
      • 2015-11-24
      • 2018-10-30
      相关资源
      最近更新 更多