最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get page content by ID (from a plugin)

programmeradmin2浏览0评论

I'm stuck on loading page content by id (through a plugin). What I have is the following:

<?php $my_query = new WP_Query('page_id=39'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_content(); ?>
<?php endwhile; ?>    

Instead of entering the id "39" though, it needs to come from $user_set_value

I'm able to echo the id like this:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'user_set_value', true);
wp_reset_query();
?>    

..but how could I go about echoing $user_set_value into the first snippet so it would get the id on the fly?

Many thanks!

I'm stuck on loading page content by id (through a plugin). What I have is the following:

<?php $my_query = new WP_Query('page_id=39'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_content(); ?>
<?php endwhile; ?>    

Instead of entering the id "39" though, it needs to come from $user_set_value

I'm able to echo the id like this:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'user_set_value', true);
wp_reset_query();
?>    

..but how could I go about echoing $user_set_value into the first snippet so it would get the id on the fly?

Many thanks!

Share Improve this question asked Jan 24, 2015 at 19:26 RayRay 251 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The values assigned to $user_set_value should be stored somehow in some form, usually an option. You will know how the values and where the values are stored. It is easy then from there

$user_set_value = get_some_saved_option_value();
$args = array(
    'page_id' => $user_set_value
);

$q = new WP_Query( $args );

Just change get_some_saved_option_value() with the actual method to get the value.

u tried using:

global $post;    
$postid  = $post->ID;

the $post contains data from the current post or page

EDIT: for more info see codex : http://codex.wordpress/Function_Reference/$post.

also i think i may have misunderstood your question, are u after:

<?php 
global $wp_query;
$postid = $wp_query->post->ID;
//  get user value and assign it here
$user_set_value = get_post_meta($postid, 'user_set_value', true);
wp_reset_query();  ?>
// pass user value into query
<?php $my_query = new WP_Query('page_id='.$user_set_value); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_content(); ?>
<?php endwhile; ?> 
发布评论

评论列表(0)

  1. 暂无评论