简体   繁体   中英

Cannot set the page template when posting using python-wordpress-xmlrpc

I have created a script that posts to my website when I run it (see code below) and everything works as it should, except I cannot seem to set the template for pages that I create.

from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts, pages
from wordpress_xmlrpc import WordPressPage

client = Client(...)

page = WordPressPage()
page.template = 'template_name.php'
page.parent_id = '206'

page.title = 'Test'
page.slug = 'rrr'
page.content = 'Some text goes here'
page.post_status = 'publish'
page.id = client.call(posts.NewPost(page))

I found this issue posted at the repository ( https://github.com/maxcutler/python-wordpress-xmlrpc/issues/44 ), and I tried using the alternative method:

page.wp_page_template = 'template_name.php'

I've also tried the names of the templates and other variations. I also find that my template(s) when I use the GetPageTemplates code:

templates = client.call(pages.GetPageTemplates())

All of my templates gets shown here.

Anyone had any success using this setting?

Seems a bug of wordpress_xmlrpc.

wordpress_xmlrpc converts field 'template' -> 'wp_page_tempalte' but its wrong. Should be converted to 'page_template' (without wp_ prefix).

"class-wp-xmlrpc-server.php" then converts field of post data from 'page_template' to 'wp_page_template'.

So wordpress_xmlrpc should send page_template not wp_page_template . wordpress_xmlrpc converts template to wp_page_template before post, so:

  1. use 'template'

    page.template = 'template_name.php'

  2. patch wordpress.py like:

*** wordpress.py
--- wordpress.py.orig
***************
*** 126,132 ****
     class WordPressPage(WordPressPost):
      definition = dict(WordPressPost.definition, **{
!         'template': 'page_template',
          'post_type': FieldMap('post_type', default='page'),
      })   
--- 126,132 ----
     class WordPressPage(WordPressPost):
      definition = dict(WordPressPost.definition, **{
!         'template': 'wp_page_template',
          'post_type': FieldMap('post_type', default='page'),
      })

It worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM