在woocommerce / my-account /页面中创建/管理自定义帖子类型

时间:2018-09-06 13:19:32

标签: wordpress woocommerce custom-post-type endpoint

希望您能帮忙

我的网站上有一个自定义帖子类型-“广告系列”,我想让用户可以在/ my-account / campaigns页面上看到他的帖子列表,将其删除/编辑,并创建新的

我创建了一个新的端点,将此版本添加到我的帐户菜单中并包含模板

<?php

function owr_custom_endpoints() {
   add_rewrite_endpoint( 'campaigns', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'owr_custom_endpoints' );

function owr_custom_query_vars( $vars ) {
  $vars[] = 'campaigns';
  return $vars;
}

add_filter( 'query_vars', 'owr_custom_query_vars', 0 );

function owr_custom_flush_rewrite_rules() {
   flush_rewrite_rules();
}

add_action( 'wp_loaded', 'owr_custom_flush_rewrite_rules' );

function owr_custom_owr_account_menu_items( $items ) {
  $items = array(
    'dashboard'         => __( 'Dashboard', 'woocommerce' ),
    'campaigns'      => 'Campaigns',
    'orders'            => __( 'Orders', 'woocommerce' ),
    'downloads'       => __( 'Downloads', 'woocommerce' ),
    'edit-address'    => __( 'Addresses', 'woocommerce' ),
    'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
    'edit-account'      => __( 'Edit Account', 'woocommerce' ),
    'customer-logout'   => __( 'Logout', 'woocommerce' ),
  );

  return $items;
}

add_filter( 'woocommerce_account_menu_items', 
'owr_custom_owr_account_menu_items' );

function owr_custom_endpoint_content() {
   include 'templates/campaigns-list.php';
}

add_action( 'woocommerce_account_campaigns_endpoint', 
'owr_custom_endpoint_content' );

function owr_remove_account_links( $menu_links ){
   if( current_user_can('buyer') ) {
     unset( $menu_links['campaigns'] );
   }
   return $menu_links;
}

add_filter ( 'woocommerce_account_menu_items', 'owr_remove_account_links' 
);

谢谢!

更新:

我有显示帖子列表的解决方案

<?php

if (!class_exists('list_user_posts')) {

class list_user_posts
{
    function __construct($args = array())
    {
        add_shortcode('user_posts', array($this,list_user_posts));
    }

    public function list_user_posts($attr = array(), $content = null)
    {
        extract(shortcode_atts(array(
            'post_type' => 'post',
            'number' => 10,
        ), $attr));

        if (!is_user_logged_in()){
            return sprintf(__('You Need to <a href="%s">Login</a> to see your posts'),wp_login_url(get_permalink()));
        }

        $pagenum = isset( $_GET['pagenum'] ) ? intval( $_GET['pagenum'] ) : 1;

        $args = array(
            'author' => get_current_user_id(), //this makes the query pull post form the current user only
            'post_status' => array('draft', 'future', 'pending', 'publish'),
            'post_type' => $post_type,
            'posts_per_page' => $number,
            'paged' => $pagenum
        );
        $user_posts = new WP_Query( $args );

        $retVal = '';
        if ( $user_posts->have_posts() ) {

            $retVal = '
                <div class="row">
                    <div class="col-md-6">'.__( 'Title', 'lup' ).'</div>
                    <div class="col-md-3">'.__( 'Status', 'lup' ).'</div>
                    <div class="col-md-3">'.__( 'Actions', 'lup' ).'</div>
                </div>';
            global $post;
            $temp = $post;
            while ($user_posts->have_posts()) {
                $user_posts->the_post();
                $title = $post->post_title;
                $link = '<a href="' . get_permalink() . '" title="' . sprintf(esc_attr__('Permalink to %s', 'lup'), the_title_attribute('echo=0')) . '" rel="bookmark">' . $title . '</a>';
                $retVal .=
                    '<div class="row">
                        <div class="col-md-6">
                            ' . (in_array($post->post_status, array('draft', 'future', 'pending')) ? $title : $link) . '
                        </div>
                        <div class="col-md-3">
                            ' . $post->post_status . '
                        </div>
                        <div class="col-md-3">
                            <a href="LINK_TO_YOUR_EDIT_PAGE"><span style="color: green;">' . __('Edit', 'lup') . '</span></a>
                            <a href="LINK TO YOUR DELETE PAGE"><span style="color: red;">' . __('Delete', 'lup') . '</span></a>
                        </div>
                    </div>';
            }

            if ($user_posts->found_posts > $number ){
                $pagination = paginate_links( array(
                        'base' => add_query_arg( 'pagenum', '%#%' ),
                        'format' => '',
                        'prev_text' => __( '&laquo;', 'lup' ),
                        'next_text' => __( '&raquo;', 'lup' ),
                        'total' => $user_posts->max_num_pages,
                        'current' => $pagenum
                    )
                );
                if ( $pagination ) {
                    $retVal .= '<div class="pagination">'.$pagination .'</div>';
                }
            }
            return $retVal;
        }else{
            return  __("No Posts Found");
        }
    }

  }
}
new list_user_posts();   

现在,我需要用于“创建信息”和编辑/删除链接的按钮

0 个答案:

没有答案