Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Problema con funcion y variable

Estas en el tema de Problema con funcion y variable en el foro de PHP en Foros del Web. Hola. Tengo la siguiente duda. He creado una función para Worpress, (functions.php) ese es el código: Código PHP: <?php function  the_category_filter2 ( $thelist , $separator ...
  #1 (permalink)  
Antiguo 03/01/2017, 10:57
 
Fecha de Ingreso: abril-2014
Mensajes: 18
Antigüedad: 12 años, 5 meses
Puntos: 0
Problema con funcion y variable

Hola.

Tengo la siguiente duda.

He creado una función para Worpress, (functions.php) ese es el código:
Código PHP:
<?php
function the_category_filter2($thelist,$separator=' ') {
    if(!
defined('WP_ADMIN')) {
        
//Category IDs to exclude
        
$categorias1 = range (1,5);
        
$categorias2 = range(13,1700);
        
$includee = array_merge($categorias1, $categorias2);
        
        
$includea = array();
        foreach(
$includee as $d) {
            
$includea[] = get_cat_name($d);
        }
        
        
$cats = explode($separator,$thelist);
        
$newlist = range();
        foreach(
$cats as $cat) {
            
$catname = trim(strip_tags($cat));
            if(!
in_array($catname,$includea))
                
$newlist[] = $cat;
        }
        return 
implode($separator,$newlist);
    } else {
        return 
$thelist;
    }
}
add_filter('the_categoryss', 'the_category_filter2', 10, 2);
?>
<?
function get_the_category_list3( $separator = '', $parents='', $post_id = false ) {
    global 
$wp_rewrite;
    
$categories = get_the_category3( $post_id );
    if ( !
is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
        return 
apply_filters( 'the_categoryss', '', $separator, $parents );

    if ( empty( 
$categories ) )
        return 
apply_filters( 'the_categoryss', __( 'Uncategorized' ), $separator, $parents );

    
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';

    
$thelist = '';
    if ( 
'' == $separator ) {
        
$thelist .= '<ul class="post-categories">';
        foreach ( 
$categories as $category ) {
            
$thelist .= "\n\t<li>";
            switch ( 
strtolower( $parents ) ) {
                case 
'multiple':
                    if ( 
$category->parent )
                        
$thelist .= get_category_parents( $category->parent, true, $separator );
                    
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
                    break;
                case 
'single':
                    
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
                    if ( 
$category->parent )
                        
$thelist .= get_category_parents( $category->parent, false, $separator );
                    
$thelist .= $category->name.'</a></li>';
                    break;
                case 
'':
                default:
                    
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
            }
        }
        
$thelist .= '</ul>';
    } else {
        
$i = 0;
        foreach ( 
$categories as $category ) {
            if ( 
0 < $i )
                
$thelist .= $separator;
            switch ( 
strtolower( $parents ) ) {
                case 
'multiple':
                    if ( 
$category->parent )
                        
$thelist .= get_category_parents( $category->parent, true, $separator );
                    
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
                    break;
                case 
'single':
                    
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
                    if ( 
$category->parent )
                        
$thelist .= get_category_parents( $category->parent, false, $separator );
                    
$thelist .= "$category->name</a>";
                    break;
                case 
'':
                default:
                    
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
            }
            ++
$i;
        }
    }
    return 
apply_filters( 'the_categoryss', $thelist, $separator, $parents );
}
?>
<?
function get_the_terms3( $id = 0, $taxonomy ) {
    global 
$post;

     
$id = (int) $id;

    if ( !
$id ) {
        if ( !
$post->ID )
            return 
false;
        else
            
$id = (int) $post->ID;
    }
     
$args = array('orderby' => 'ID');
    
$terms = wp_get_object_terms( $id, $taxonomy, $args);
    if ( 
false === $terms ) {
        
$args = array('orderby' => 'ID');
        
$terms = wp_get_object_terms( $id, $taxonomy, $args);
        
wp_cache_add($id, $terms, $args, $taxonomy . '_relationships');
    }

    
$terms = apply_filters( 'get_the_terms3', $terms, $id, $taxonomy, $args );

    if ( empty( 
$terms ) )
        return 
false;

    return 
$terms;
}
?>
<?php 
function get_the_category3( $id = false ) {
    
$categories = get_the_terms3( $id, 'category' );
    if ( ! 
$categories )
        
$categories = array();

    
$categories = array_values( $categories );

    foreach ( 
array_keys( $categories ) as $key ) {
        
_make_cat_compat( $categories[$key] );
    }

    
// Filter name is plural because we return alot of categories (possibly more than #13237) not just one
    
return apply_filters( 'get_the_categories', $categories );
}
?>
<?
function the_categoryss( $separator = '', $parents='', $post_id = false ) {
    echo 
get_the_category_list3( $separator, $parents, $post_id );
}
?>
La función la invoco con:
Código PHP:
<?php the_categoryss(' - ')?>
y funciona correctamente.

Sin embargo me gustaria capturar el resultado de dicha funcion en una variable.

Lo he realizado de la siguiente manera:
<?php $categorias =the_categoryss(' - ') ?>
Si embargo la variable esta vacia.

¿Como podria solucionarlo.

Un saludo.
  #2 (permalink)  
Antiguo 03/01/2017, 11:42
 
Fecha de Ingreso: octubre-2009
Mensajes: 305
Antigüedad: 17 años
Puntos: 29
Respuesta: Problema con funcion y variable

simple: reemplaza el echo con un return, pero eso evitaría el volcar el resultado a la salida, si es necesario declara una variable interna, asignale el valor, hazle echo y luego return.
  #3 (permalink)  
Antiguo 03/01/2017, 11:57
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 18 años, 1 mes
Puntos: 2237
Respuesta: Problema con funcion y variable

Como bien dice sustentio, solo necesitas devolver el contenido en lugar de enviarlo a la pantalla. En la función the_categoryss reemplaza el echo por return y obtendrás todo en una variable.
__________________
- León, Guanajuato
- GV-Foto
  #4 (permalink)  
Antiguo 03/01/2017, 13:34
 
Fecha de Ingreso: abril-2014
Mensajes: 18
Antigüedad: 12 años, 5 meses
Puntos: 0
Respuesta: Problema con funcion y variable

Muchas gracias. Solucionado

Etiquetas: funcion, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:08.