如何知道WordPress中博客页面的父页面ID?

时间:2017-06-12 18:16:44

标签: php wordpress

我正在构建一个主题。我知道如何检索子页面的父页面。这非常直接。

       with tf.Session() as sess:
         sess.run(init)
         summary_writer =tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())
        start_time = time.time()
        for i in range(iteration_number):
          j = (i - epoch) * batch_size % number_of_examples
          k= (i - epoch + 1) * batch_size % number_of_examples
          if (k < j): # THE END OF DATA SET ------------------------
            k = number_of_examples
            batch_x = train_images[j:number_of_examples, :]
            batch_y = train_labels[j:number_of_examples, :]
            print("Iter " + str(i) + ", epoch Loss= " + \
                    "{:.6f}".format(loss) + ", Training Accuracy= " + \
                    "{:.5f}".format(acc))

            data = numpy.concatenate((train_images, train_labels), axis=1)
            numpy.random.shuffle(data)
            train_images = data[:, :3072]
            train_labels = data[:, 3072:3082]
            epoch = i + 1

          else:
            batch_x = train_images[j:k, :]
            batch_y = train_labels[j:k, :]
            loss, acc, summary = sess.run([cost, accuracy, merged_summary_op], feed_dict={x: batch_x,
                                                        y: batch_y,
                                                        keep_prob: 0.3})
            summary_writer.add_summary(summary)

            sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
                                        keep_prob: dropout})

但我的主要问题是.... 我将页面'右侧边栏'设置为显示所有帖子的博客页面。我还将右侧边栏页面设置为'博客'页面的子页面。然后我使用global $post; $parent_page = $post->post_parent // do with the post parent id whatever you need 在右侧边栏页面上搜索我找不到var_dump ( $post )的任何属性值。但是,后父母有25个身份证。

如何知道已设置为博客页面的页面的父页面ID?

3 个答案:

答案 0 :(得分:0)

您可以使用page_for_posts选项:

$posts_page_id = get_option('page_for_posts');
  

显示帖子的页面的ID。当 show_on_front 的值为page时有用。

     

- WordPress Codec

答案 1 :(得分:0)

我通过做一些实验得到了答案。最后答案是

global $wp_query;
$parent_post_id = $wp_query->queried_object->post_parent;

由于

答案 2 :(得分:0)

此函数添加到主题functions.php文件中。此函数将从父页面返回页面ID。

function get_top_parent_page_id() { 
    global $post; 

    if ($post->ancestors) { 
        return end($post->ancestors); 
    } else { 
        return $post->ID; 
    } 
}