$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Where paramaters of a custom function are coming from inside the loop?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Where paramaters of a custom function are coming from inside the loop?

matteradmin8PV0评论

I have this piece of code and, although it works, I do not understand how. First, I created a custom post type into my mu-plugins folder:

function actor_init() {

$args = array(
    'label' => 'actors',
    'description' => 'hollywood & stuff',
    'supports' => array(
        'thumbnail',
        'title',
        'editor',
        'comments'
        ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,

);


register_post_type('actor', $args);

}

then, in functions.php I hooked it.

add_action( 'init', 'actor_init' );

Also, inside functions.php I created a function to display, if there is, the title of the post / page / custom-post

<?php
function provahel($arg) {

   if (!$arg['title']) {
      $arg['title'] = get_the_title();
   }
?>

   <h1><?php echo $arg['title'] ?></h1>
<?php } ?>

Finally, In my page.php, single-actor.php and single.php files I call the function inside the loop.

<?php
while(have_posts()) {
    the_post();

    provahel($argf);
?>

On the front end, correctly the title of the post gets rendered, either if it is a page, a post or the custom-post-type (in this case actor). Why? How Wordpress knows what parameter is passed into the provahel() functions? What is this parameter $argf (P.S. could be called in any way and it would still work)?
Thanks in advance for any contribution.

I have this piece of code and, although it works, I do not understand how. First, I created a custom post type into my mu-plugins folder:

function actor_init() {

$args = array(
    'label' => 'actors',
    'description' => 'hollywood & stuff',
    'supports' => array(
        'thumbnail',
        'title',
        'editor',
        'comments'
        ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,

);


register_post_type('actor', $args);

}

then, in functions.php I hooked it.

add_action( 'init', 'actor_init' );

Also, inside functions.php I created a function to display, if there is, the title of the post / page / custom-post

<?php
function provahel($arg) {

   if (!$arg['title']) {
      $arg['title'] = get_the_title();
   }
?>

   <h1><?php echo $arg['title'] ?></h1>
<?php } ?>

Finally, In my page.php, single-actor.php and single.php files I call the function inside the loop.

<?php
while(have_posts()) {
    the_post();

    provahel($argf);
?>

On the front end, correctly the title of the post gets rendered, either if it is a page, a post or the custom-post-type (in this case actor). Why? How Wordpress knows what parameter is passed into the provahel() functions? What is this parameter $argf (P.S. could be called in any way and it would still work)?
Thanks in advance for any contribution.

Share Improve this question edited Feb 15, 2019 at 16:37 Enrico asked Feb 15, 2019 at 16:26 EnricoEnrico 1096 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The answer can be found in the documentation for get_the_title:

$post (int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.

If you don't supply a post object or post ID, it defaults to using the global $post variable.

When you call the_post() within the loop, this populates the global $post variable with the data from the current post within that loop.

EDIT:

get_the_title gets the current post object:

$post = get_post( $post );

get_post then accesses the global $post if nothing was passed to it:

$post = $GLOBALS['post'];

Your function provahel() sets the value of title within the if statement as quoted below:

if (!$arg['title']) {
      $arg['title'] = get_the_title(); // Here you sets the $arg['title'] to current page' title
   }

So if you don't assign a value to $argf in page.php, your function will render the title of current page. To display something else, you need to assign a valid value to $argf['title'] like this, before calling your function, in page.php

 $arg['title'] = "My Custom title...";
Post a comment

comment list (0)

  1. No comments so far