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.
2 Answers
Reset to default 0The 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...";