php - How to pass variable using get_template_partGet the solution ↓↓↓
I have three types of posts on my WordPress site (standard, aside, video).
I need to count a variable like
$row in my standard
content.php file But I am unable to pass it to
index.php file code:
<?php if (have_posts()) : $$row = 2; while (have_posts()) : the_post(); get_template_part('content', get_post_format()); $row++; endwhile; endif; ?>
content.php file code:
<div> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p><?php the_content(); ?>- <?php echo $row; ?></p> </div>
but when I
echo $row; nothing shows
I already use this link from a similar question on stackoverflow but I can't change my post format file, Is there another way to do this?
You can't pass parameters exactly, but what you can do is use the functions
get_query_var which makes the variables globally accessible.
In the main template, e.g. index.php, you use
set_query_var to set the variable. Note that the first parameter is the name of the global variable, and this is what you use to retrieve it - not the original parameter name.
set_query_var( "my_global_var_name", $param_vaule); get_template_part('content', get_post_format());
Then in the template part, you can get the value of the now-global variable using
$myvar = set_query_var( "my_global_var_name" );
(It's worth noting that its best practice to minimise global variables, so you might be netter looking at restructuring your template structure to see if there is another way to do this, using extra template parts for example.)
Share solution ↓
Link To Answer People are also looking for solutions of the problem: the process class relies on proc_open, which is not available on your php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.