Crear subtitulo en WordPress con código PHP

  • Autor Autor eic2010
  • Fecha de inicio Fecha de inicio
E

eic2010

Beta
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Hola estoy buscando la función php que cree subtitulo en wordpress, se que hay plugins pero quisiera realizarlo con el código php ya que todos los que he probado no han funcionado pero se que se puede realizar desde ya muchas gracias
 
Puedes hacerlo con un shortcode , si te sirve acepto donaciones ! 🙂


Metodo 1 común en functions.php

<?php
function add_subtitle($title, $id) {
$subtitle = get_post_meta($id, 'myCustomField', true);
$new_title = $title;
if(!empty($subtitle))
$new_title = $subtitle . ': ' . $new_title;
return $new_title;
}
add_filter('the_title', 'add_subtitle', 10, 2);

Metodo 2

function my_wp_subtitle_page_part_support() {
add_post_type_support( 'my_post_type', 'wps_subtitle' );
}
add_action( 'init', 'my_wp_subtitle_page_part_support' );
`

En donde quieres llamarlo

<?php the_subtitle(); ?>



Metodo 3 con SHORTCODE

Agregas en functions.php


function spoiler( $atts, $content = NULL ) {
extract( shortcode_atts( array(
'title' => 'default title',
'subtitle' => 'default subtitle',
), $atts ) );

return '<div class="moreinfo"><h3 class="click drop subtitle">'

.'Title:'.$title.'Subtitle:'.$subtitle.

'</h3><div class="morecontent"><p>'

.$content.

'</p></div><!--/.morecontent--></div><!--/.moreinfo-->';
}
add_shortcode( 'spoiler', 'spoiler' );


SHORTCODE:

[spoiler title='title' subtitle='subtitle']content[/spoiler]
 
Última edición:
Desde ya gracias por contestar pero no me ha funcionado ninguno, por ahí me exprese mal lo que necesito es el código que va en functions.php para generar en NUEVA ENTRADA esto y así yo al poner <?php the_subtitle(); ?> en single.php y me lo mostraría abajo del titulo.

image2.webp
 
Haz provado con este? Normalmente es el mas comundo y de hecho el primero que se recomienda usar:

<?php
function add_subtitle($title, $id) {
$subtitle = get_post_meta($id, 'myCustomField', true);
$new_title = $title;
if(!empty($subtitle))
$new_title = $subtitle . ': ' . $new_title;
return $new_title;
}
add_filter('the_title', 'add_subtitle', 10, 2);
 
Este es mi código functions.php así debería estar bien?

PHP:
<?php
// This function enqueues the Normalize.css for use. The first parameter is a name for the stylesheet, the second is the URL. Here we
// use an online version of the css file.
function add_normalize_CSS() {
    wp_enqueue_style( 'normalize-styles', "https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css");
}

// Register a new sidebar simply named 'sidebar'
function add_widget_Support() {
                register_sidebar( array(
                                'name'          => 'Sidebar',
                                'id'            => 'sidebar',
                                'before_widget' => '<div>',
                                'after_widget'  => '</div>',
                                'before_title'  => '<h2>',
                                'after_title'   => '</h2>',
                ) );
}
// Hook the widget initiation and run our function
add_action( 'widgets_init', 'add_Widget_Support' );



// Register a new navigation menu
function add_Main_Nav() {
  register_nav_menu('header-menu',__( 'Header Menu' ));
}
// Hook to the init action hook, run our navigation menu function
add_action( 'init', 'add_Main_Nav' );

if ( function_exists( 'add_theme_support' ) )
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size(150, 150);


function add_subtitle($title, $id) {
$subtitle = get_post_meta($id, 'myCustomField', true);
$new_title = $title;
if(!empty($subtitle))
$new_title = $subtitle . ': ' . $new_title;
return $new_title;
}
add_filter('the_title', 'add_subtitle', 10, 2);
 
Primero tienes que crear el metabox.
 
crea el metabox
 
Atrás
Arriba