【问题标题】:Cannot set the page template when posting using python-wordpress-xmlrpc使用 python-wordpress-xmlrpc 发布时无法设置页面模板
【发布时间】:2015-09-16 00:52:44
【问题描述】:

我创建了一个脚本,当我运行它时会发布到我的网站(见下面的代码),一切正常,除了我似乎无法为我创建的页面设置模板。

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))

我在存储库 (https://github.com/maxcutler/python-wordpress-xmlrpc/issues/44) 中发现了这个问题,并尝试使用替代方法:

page.wp_page_template = 'template_name.php'

我还尝试了模板的名称和其他变体。当我使用 GetPageTemplates 代码时,我还发现我的模板:

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

我的所有模板都显示在这里。

有人使用此设置成功了吗?

【问题讨论】:

    标签: python wordpress


    【解决方案1】:

    似乎是 wordpress_xmlrpc 的错误。

    wordpress_xmlrpc 转换字段 'template' -> 'wp_page_tempalte' 但它是错误的。 应转换为“page_template”(不带 wp_ 前缀)。

    “class-wp-xmlrpc-server.php”然后将帖子数据的字段从'page_template'转换为'wp_page_template'。

    所以 wordpress_xmlrpc 应该发送page_template 而不是wp_page_template。 wordpress_xmlrpc 在发布前将template 转换为wp_page_template,所以:

    1. 使用“模板”

      page.template = 'template_name.php'

    2. 补丁 wordpress.py 喜欢:

    *** 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'),
          })
    

    成功了。

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      相关资源
      最近更新 更多