{"id":1604,"date":"2022-08-30T15:17:55","date_gmt":"2022-08-30T15:17:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/how-do-i-make-my-function-add-variables-values-to-the-post-object-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:55","modified_gmt":"2022-08-30T15:17:55","slug":"how-do-i-make-my-function-add-variables-values-to-the-post-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-do-i-make-my-function-add-variables-values-to-the-post-object-collection-of-common-programming-errors\/","title":{"rendered":"How do I make my function add variables\/values to the $post object?-Collection of common programming errors"},"content":{"rendered":"<p>How do I add <code>$tzDesc<\/code> and <code>$tzEmbed<\/code> (and other variables) to the <code>$post<\/code> object in the following function so that I can display the values in my theme files by inserting ?<\/p>\n<pre><code>add_action( 'the_post', 'paginate_slide' );\n\nfunction paginate_slide( $post ) {\n\n    global $pages, $multipage, $numpages;\n\n    if( is_single() &amp;&amp; get_post_type() == 'post' ) {\n\n    $multipage = 1;\n    $id = get_the_ID();\n    $custom = array();\n    $pages = array();\n    $i = 1;\n\n    foreach( get_post_custom_keys() as $key )\n        if ( false !== strpos( $key, 'slide' ) )\n            $custom[$key] = get_post_meta( $id, $key, true);\n\n    while( isset( $custom[\"slide{$i}-title\"] ) ) {\n\n        $page = '';\n        $tzTitle = $custom[\"slide{$i}-title\"];\n        $tzImage = $custom[\"slide{$i}-image\"];\n        $tzDesc = $custom[\"slide{$i}-desc\"];\n        $tzEmbed = $custom[\"slide{$i}-embed\"];\n\n        $page = \"<\/code><\/pre>\n<h2><code>{$tzTitle}<\/code><\/h2>\n<pre><img src='{$tzImage}' \/>\";\n        $pages[] = $page;\n        $i++;\n    }\n\n    $numpages = count( $pages );\n    }\n}\n<\/pre>\n<p><code>If you know and can provide an answer can you please be very detailed in how the code should be structured because my knowledge of php is very minimal and I have tried to do this numerous already without any success. Thanks.<\/code><\/p>\n<ol>\n<li>\n<p><code>Like any other php object, you can add items to the $post object like so:<\/code><\/p>\n<pre><code><code>$post-&gt;my_new_val_name = 'my new value';\n<\/code><\/code><\/pre>\n<p>I don&#8217;t know exactly what you&#8217;re trying to do, but inside a function hooked to the_post, you can assign new values and return the object.<\/p>\n<pre><code>function my_func($post) {\n\n    $post-&gt;my_new_val_name = 'my new value';\n    return $post;\n\n}\nadd_action( 'the_post', 'my_func' );\n<\/code><\/pre>\n<p>However, in your template file, you won&#8217;t be able to just echo $my_new_val_name as you&#8217;re suggesting&#8230; the the_post() function doesn&#8217;t extract values that way. You&#8217;ll have to reference the post object explicitly. Like:<\/p>\n<pre><code>echo $post-&gt;my_new_val_name;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:01:16. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>How do I add $tzDesc and $tzEmbed (and other variables) to the $post object in the following function so that I can display the values in my theme files by inserting ? add_action( &#8216;the_post&#8217;, &#8216;paginate_slide&#8217; ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() &amp;&amp; get_post_type() == &#8216;post&#8217; ) { $multipage = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1604","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1604"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1604\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}