ajuda com query_posts
-
Ola, pessoal boa tarde.
Eu estou tempo problema em criar um query_posts que referencie a pagina do site e os pots de um determinada categoria, só que não estou conseguindo o site ficou meio torto kkk:
http://aprendiz.url.ph/
essa foi a utima tentativa:<div id="primary" class="site-content" <?php $quey_customizada = new WP_Query('cat=2&posts_per_page=9&showposts=4');?> <?php while (have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> <?php endwhile; ?> </div>
-
Olá Juliana Silva Theodoro,
Penso que deve ser isto que precisas.
Podes encontrar documentação sobre query_posts() (Aqui)<div id="primary" class="site-content" <?php query_posts('category_name=wordpress&showposts=5&posts_per_page=9'); <?php while (have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> <?php endwhile; ?> <?php endwhile; else: ?> <p><?php _e( 'Sem Posts para essa categoria', 'THEMENAME ?></p> <?php endif; ?> </div>
Obrigado Carlos vou tentar 🙂
Juliana Silva Theodoro,
Desculpa, mas tinha uma linha mal, aqui vai o corrigido.
(Coloco também 2 opções. Filtrar a categoria pelo Name ou pelo IDFiltrar a categoria pelo Name:
<div id="primary" class="site-content"> <?php query_posts('category_name=wordpress&showposts=5&posts_per_page=9'); ?> <?php while (have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> <?php endwhile; else: ?> <p><?php _e( 'Sem Posts para essa categoria', 'THEMENAME ?></p> <?php endif; ?> </div>
Filtrar a categoria pelo ID:
<div id="primary" class="site-content"> <?php query_posts('cat=2&showposts=5&posts_per_page=9'); ?> <?php while (have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> <?php endwhile; else: ?> <p><?php _e( 'Sem Posts para essa categoria', 'THEMENAME ?></p> <?php endif; ?> </div>
OLa bom dia Carlor, eu coloquei o código da seguinte forma:
echo'<div id="primary" class="site-content">'; <?php query_posts('cat=2&showposts=4&posts_per_page=9'); ?> <?php while(have_posts()) : the_post(); ?> echo'<h1>'; <?php the_title(); ?> echo'</h1>'; <?php the_excerpt(); ?> <?php endwhile; else: ?> echo'<p>'; <?php the_content( 'Sem Posts para essa categoria', 'ABLE');?> echo'</p>'; <?php endif; ?> echo'</div>';
so que continua da o mesmo erro Parse error: syntax error, unexpected ‘<‘ in /home/u245150648/public_html/wp-content/themes/able/functions.php on line 202, so mudando a linha. Obrigado.
Olá Juliana,
Será que me podes colocar aqui o conteudo do teu ficheiro functions.php?
<?php /** * Able functions and definitions * * @package Able * @since Able 1.0 */ /** * Set up the content width value based on the theme's design and stylesheet. */ if ( ! isset( $content_width ) ) $content_width = 1176; /* Default content width, no widgets active */ /** * Adjust the content width based on the presence of active widgets. * * @since Able 1.0 */ function able_set_content_width() { global $content_width; if ( is_active_sidebar( 'sidebar-1' ) /* Left Sidebar */ || is_active_sidebar( 'sidebar-2' ) /* Right Sidebar */ ) $content_width = 869; if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' ) ) $content_width = 562; } add_action( 'template_redirect', 'able_set_content_width' ); if ( ! function_exists( 'able_setup' ) ): /** * Sets up theme defaults and registers support for various WordPress features. * * Note that this function is hooked into the after_setup_theme hook, which runs * before the init hook. The init hook is too late for some features, such as indicating * support post thumbnails. * * @since Able 1.0 */ function able_setup() { /** * Make theme available for translation * Translations can be filed in the /languages/ directory * If you're building a theme based on Able, use a find and replace * to change 'able' to the name of your theme in all the template files */ load_theme_textdomain( 'able', get_template_directory() . '/languages' ); /** * Add default posts and comments RSS feed links to head */ add_theme_support( 'automatic-feed-links' ); /** * Enable support for Post Thumbnails */ add_theme_support( 'post-thumbnails' ); /** * Editor Style */ add_editor_style(); /** * Enable support for Post Formats */ add_theme_support( 'post-formats', array( 'aside', 'image', 'video', 'quote', 'link' ) ); /** * This theme uses wp_nav_menu() in one location. */ register_nav_menus( array( 'primary' => __( 'Primary Menu', 'able' ), 'footer' => __( 'Footer Menu', 'able' ), ) ); } endif; // able_setup add_action( 'after_setup_theme', 'able_setup' ); /** * Setup the WordPress core custom background feature. * * Use add_theme_support to register support for WordPress 3.4+ * as well as provide backward compatibility for previous versions. * Use feature detection of wp_get_theme() which was introduced * in WordPress 3.4. * * Hooks into the after_setup_theme action. * * @since Able 1.0.0 */ function able_register_custom_background() { $args = array( 'default-color' => 'fafafa', ); $args = apply_filters( 'able_custom_background_args', $args ); if ( function_exists( 'wp_get_theme' ) ) { add_theme_support( 'custom-background', $args ); } else { define( 'BACKGROUND_COLOR', $args['default-color'] ); define( 'BACKGROUND_IMAGE', $args['default-image'] ); add_custom_background(); } } add_action( 'after_setup_theme', 'able_register_custom_background' ); /** * Register widgetized area and update sidebar with default widgets * * @since Able 1.0 */ function able_widgets_init() { register_sidebar( array( 'name' => __( 'Left Sidebar', 'able' ), 'id' => 'sidebar-1', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h1 class="widget-title">', 'after_title' => '</h1>', ) ); register_sidebar( array( 'name' => __( 'Right Sidebar', 'able' ), 'id' => 'sidebar-2', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h1 class="widget-title">', 'after_title' => '</h1>', ) ); } add_action( 'widgets_init', 'able_widgets_init' ); /** * Enqueue scripts and styles * * @since Able 1.0 */ function able_scripts() { wp_enqueue_style( 'able-style', get_stylesheet_uri() ); wp_enqueue_script( 'able-small-menu', get_template_directory_uri() . '/js/small-menu.js', array( 'jquery' ), '20120206', true ); if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); } if ( is_singular() && wp_attachment_is_image( get_the_ID() ) ) { wp_enqueue_script( 'able-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20120202' ); } } add_action( 'wp_enqueue_scripts', 'able_scripts' ); /* * Hide the theme-provided background images if the user manually sets a custom background color * * @since Able 1.0 */ function able_custom_background_check() { if (get_background_color()!=' '): echo '<style type="text/css"> body {background-image: none;} </style>'; endif; } add_action( 'wp_head', 'able_custom_background_check' ); /** * Implement the Custom Header feature. */ require( get_template_directory() . '/inc/custom-header.php' ); /** * Implement Custom Google Fonts. */ require( get_template_directory() . '/inc/fonts.php' ); /** * Custom template tags for this theme. */ require( get_template_directory() . '/inc/template-tags.php' ); /** * Custom functions that act independently of the theme templates. */ require( get_template_directory() . '/inc/tweaks.php' ); /* * Load Jetpack compatibility file. */ require( get_template_directory() . '/inc/jetpack.php' );
Ola Juliana
O ficheiro funtions.php parece-me bem, por isso o problema não deverá de estar nele.
Envio novamente o código que executa o que pretendes (tenta usar da mesma forma):<?php if (have_posts()): ?> <div id="primary" class="site-content"> <?php query_posts('cat=2&showposts=4&posts_per_page=9'); while(have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); endwhile; ?> </div> <?php else: ?> <p><?php _e( 'Sem Posts para essa categoria', 'ABLE'); ?></p> <?php endif; ?>
Espero que agora resolva…
oi boa noite, esse código aqui é o original q que uns > sem fechar pelo menos eu acho rsrs, da uma olhada e me diz o que acha!
<?php
/**
* Able functions and definitions
*
* @package Able
* @since Able 1.0
*//**
* Set up the content width value based on the theme’s design and stylesheet.
*/
if ( ! isset( $content_width ) )
$content_width = 1176; /* Default content width, no widgets active *//**
* Adjust the content width based on the presence of active widgets.
*
* @since Able 1.0
*/
function able_set_content_width() {
global $content_width;if ( is_active_sidebar( ‘sidebar-1’ ) /* Left Sidebar */
|| is_active_sidebar( ‘sidebar-2’ ) /* Right Sidebar */
)
$content_width = 869;if ( is_active_sidebar( ‘sidebar-1’ )
&& is_active_sidebar( ‘sidebar-2’ )
)
$content_width = 562;
}
add_action( ‘template_redirect’, ‘able_set_content_width’ );if ( ! function_exists( ‘able_setup’ ) ):
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* @since Able 1.0
*/
function able_setup() {/**
* Make theme available for translation
* Translations can be filed in the /languages/ directory
* If you’re building a theme based on Able, use a find and replace
* to change ‘able’ to the name of your theme in all the template files
*/
load_theme_textdomain( ‘able’, get_template_directory() . ‘/languages’ );/**
* Add default posts and comments RSS feed links to head
*/
add_theme_support( ‘automatic-feed-links’ );/**
* Enable support for Post Thumbnails
*/
add_theme_support( ‘post-thumbnails’ );/**
* Editor Style
*/
add_editor_style();/**
* Enable support for Post Formats
*/
add_theme_support( ‘post-formats’, array( ‘aside’, ‘image’, ‘video’, ‘quote’, ‘link’ ) );/**
* This theme uses wp_nav_menu() in one location.
*/
register_nav_menus( array(
‘primary’ => __( ‘Primary Menu’, ‘able’ ),
‘footer’ => __( ‘Footer Menu’, ‘able’ ),
) );}
endif; // able_setup
add_action( ‘after_setup_theme’, ‘able_setup’ );/**
* Setup the WordPress core custom background feature.
*
* Use add_theme_support to register support for WordPress 3.4+
* as well as provide backward compatibility for previous versions.
* Use feature detection of wp_get_theme() which was introduced
* in WordPress 3.4.
*
* Hooks into the after_setup_theme action.
*
* @since Able 1.0.0
*/
function able_register_custom_background() {
$args = array(
‘default-color’ => ‘fafafa’,
);$args = apply_filters( ‘able_custom_background_args’, $args );
if ( function_exists( ‘wp_get_theme’ ) ) {
add_theme_support( ‘custom-background’, $args );
} else {
define( ‘BACKGROUND_COLOR’, $args[‘default-color’] );
define( ‘BACKGROUND_IMAGE’, $args[‘default-image’] );
add_custom_background();
}
}
add_action( ‘after_setup_theme’, ‘able_register_custom_background’ );/**
* Register widgetized area and update sidebar with default widgets
*
* @since Able 1.0
*/
function able_widgets_init() {
register_sidebar( array(
‘name’ => __( ‘Left Sidebar’, ‘able’ ),
‘id’ => ‘sidebar-1’,
‘before_widget’ => ‘<aside id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ‘</aside>’,
‘before_title’ => ‘<h1 class=”widget-title”>’,
‘after_title’ => ‘</h1>’,
) );
register_sidebar( array(
‘name’ => __( ‘Right Sidebar’, ‘able’ ),
‘id’ => ‘sidebar-2’,
‘before_widget’ => ‘<aside id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ‘</aside>’,
‘before_title’ => ‘<h1 class=”widget-title”>’,
‘after_title’ => ‘</h1>’,
) );
}
add_action( ‘widgets_init’, ‘able_widgets_init’ );/**
* Enqueue scripts and styles
*
* @since Able 1.0
*/
function able_scripts() {wp_enqueue_style( ‘able-style’, get_stylesheet_uri() );
wp_enqueue_script( ‘able-small-menu’, get_template_directory_uri() . ‘/js/small-menu.js’, array( ‘jquery’ ), ‘20120206’, true );
if ( is_singular() && comments_open() && get_option( ‘thread_comments’ ) ) {
wp_enqueue_script( ‘comment-reply’ );
}if ( is_singular() && wp_attachment_is_image( get_the_ID() ) ) {
wp_enqueue_script( ‘able-keyboard-image-navigation’, get_template_directory_uri() . ‘/js/keyboard-image-navigation.js’, array( ‘jquery’ ), ‘20120202’ );
}
}
add_action( ‘wp_enqueue_scripts’, ‘able_scripts’ );/*
* Hide the theme-provided background images if the user manually sets a custom background color
*
* @since Able 1.0
*/
function able_custom_background_check() {if ( ” != get_background_color() ) : ?>
<style type=”text/css”>
body {
background-image: none;
}
</style>
<?php endif;
}add_action( ‘wp_head’, ‘able_custom_background_check’ );
/**
* Implement the Custom Header feature.
*/
require( get_template_directory() . ‘/inc/custom-header.php’ );/**
* Implement Custom Google Fonts.
*/
require( get_template_directory() . ‘/inc/fonts.php’ );/**
* Custom template tags for this theme.
*/
require( get_template_directory() . ‘/inc/template-tags.php’ );/**
* Custom functions that act independently of the theme templates.
*/
require( get_template_directory() . ‘/inc/tweaks.php’ );/*
* Load Jetpack compatibility file.
*/
require( get_template_directory() . ‘/inc/jetpack.php’ );Quando eu insiro o código nesse ou no outro da erro 🙁
O ficheiro functions.php não me parece ter nenhum problema.
Quanto ao código que enviei, o mesmo deverá estar num ficheiro que NÃO o functions.php. CERTO?
Se substituires o último código que enviei pelo que colocas-te logo no primeiro posts deverá ficar a funcionar sem problema:
Teu código:
<div id="primary" class="site-content" <?php $quey_customizada = new WP_Query('cat=2&posts_per_page=9&showposts=4');?> <?php while (have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> <?php endwhile; ?> </div>
Corrigido (que deve substituir o teu código):
<?php if (have_posts()): ?> <div id="primary" class="site-content"> <?php query_posts('cat=2&showposts=4&posts_per_page=9'); while(have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); endwhile; ?> </div> <?php else: ?> <p><?php _e( 'Sem Posts para essa categoria', 'ABLE'); ?></p> <?php endif; ?>
Então Carlos a onde eu devo coloca-la ? obrigado
Ola Juliana Theodoro,
Existem várias formas de implementar, dependendo sempre do contexto.
Para resolver o teu problema, vais ao ficheiro archive.php do teu tema.Procuras pela linha que contém o seguinte comentário:
<?php /* Start the Loop */ ?>
(deve estar +- na linha 92)logo a seguir a essa linha colocas o seguinte código:
(copia e cola)<?php if (is_category('2')): ?> <div id="primary" class="site-content"> <?php query_posts('cat=2&showposts=4&posts_per_page=9'); while(have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); endwhile; ?> </div> <?php else: ?>
após isso terás de colocar:
<?php endif; ?>
antes de:
<?php able_content_nav( 'nav-below' ); ?>
O que estas a fazer é dizer ao WordPress que caso a categoria tenha o ID 2 o loop dos post será execuado de outra forma.
Espero que agora fique a funcionar.
não deu certo pagina em branco ! 🙁
<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package Able
* @since Able 1.0
*/get_header(); ?>
<section id=”primary” class=”site-content”>
<div id=”content” role=”main”><?php if ( have_posts() ) : ?>
<header class=”page-header”>
<h1 class=”page-title”>
<?php
if ( is_category() ) :
printf( __( ‘Category Archives: %s’, ‘able’ ), ‘<span>’ . single_cat_title( ”, false ) . ‘</span>’ );elseif ( is_tag() ) :
printf( __( ‘Tag Archives: %s’, ‘able’ ), ‘<span>’ . single_tag_title( ”, false ) . ‘</span>’ );elseif ( is_author() ) :
/* Queue the first post, that way we know
* what author we’re dealing with (if that is the case).
*/
the_post();
printf( __( ‘Author Archives: %s’, ‘able’ ), ‘<span class=”vcard”>‘ . get_the_author() . ‘</span>’ );
/* Since we called the_post() above, we need to
* rewind the loop back to the beginning that way
* we can run the loop properly, in full.
*/
rewind_posts();elseif ( is_day() ) :
printf( __( ‘Daily Archives: %s’, ‘able’ ), ‘<span>’ . get_the_date() . ‘</span>’ );elseif ( is_month() ) :
printf( __( ‘Monthly Archives: %s’, ‘able’ ), ‘<span>’ . get_the_date( ‘F Y’ ) . ‘</span>’ );elseif ( is_year() ) :
printf( __( ‘Yearly Archives: %s’, ‘able’ ), ‘<span>’ . get_the_date( ‘Y’ ) . ‘</span>’ );elseif ( is_tax( ‘post_format’, ‘post-format-aside’ ) ) :
_e( ‘Asides’, ‘able’ );elseif ( is_tax( ‘post_format’, ‘post-format-image’ ) ) :
_e( ‘Images’, ‘able’);elseif ( is_tax( ‘post_format’, ‘post-format-video’ ) ) :
_e( ‘Videos’, ‘able’ );elseif ( is_tax( ‘post_format’, ‘post-format-quote’ ) ) :
_e( ‘Quotes’, ‘able’ );elseif ( is_tax( ‘post_format’, ‘post-format-link’ ) ) :
_e( ‘Links’, ‘able’ );else :
_e( ‘Archives’, ‘able’ );endif;
?>
</h1>
<?php
if ( is_category() ) :
// show an optional category description
$category_description = category_description();
if ( ! empty( $category_description ) ) :
echo apply_filters( ‘category_archive_meta’, ‘<div class=”taxonomy-description”>’ . $category_description . ‘</div>’ );
endif;elseif ( is_tag() ) :
// show an optional tag description
$tag_description = tag_description();
if ( ! empty( $tag_description ) ) :
echo apply_filters( ‘tag_archive_meta’, ‘<div class=”taxonomy-description”>’ . $tag_description . ‘</div>’ );
endif;endif;
?>
</header><!– .page-header –><?php rewind_posts(); ?>
<?php able_content_nav( ‘nav-above’ ); ?>
<?php /* Start the Loop */ ?>
<?php
if (is_category(‘2’)): ?>
<div id=”primary” class=”site-content”>
<?php
query_posts(‘cat=2&showposts=4&posts_per_page=7’);
while(have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_excerpt();
endwhile;
?>
</div>
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?><?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( ‘content’, get_post_format() );
?><?php endwhile; ?>
<?php able_content_nav( ‘nav-below’ ); ?>
<?php else : ?>
<?php get_template_part( ‘no-results’, ‘archive’ ); ?>
<?php endif; ?>
</div><!– #content –>
</section><!– #primary .site-content –><?php get_sidebar(); ?>
<?php get_footer(); ?><?php /** * The template for displaying Archive pages. * * Learn more: http://codex.wordpress.org/Template_Hierarchy * * @package Able * @since Able 1.0 */ get_header(); ?> <section id="primary" class="site-content"> <div id="content" role="main"> <?php if ( have_posts() ) : ?> <header class="page-header"> <h1 class="page-title"> <?php if ( is_category() ) : printf( __( 'Category Archives: %s', 'able' ), '<span>' . single_cat_title( '', false ) . '</span>' ); elseif ( is_tag() ) : printf( __( 'Tag Archives: %s', 'able' ), '<span>' . single_tag_title( '', false ) . '</span>' ); elseif ( is_author() ) : /* Queue the first post, that way we know * what author we're dealing with (if that is the case). */ the_post(); printf( __( 'Author Archives: %s', 'able' ), '<span class="vcard"><a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '" title="' . esc_attr( get_the_author() ) . '" rel="me">' . get_the_author() . '</a></span>' ); /* Since we called the_post() above, we need to * rewind the loop back to the beginning that way * we can run the loop properly, in full. */ rewind_posts(); elseif ( is_day() ) : printf( __( 'Daily Archives: %s', 'able' ), '<span>' . get_the_date() . '</span>' ); elseif ( is_month() ) : printf( __( 'Monthly Archives: %s', 'able' ), '<span>' . get_the_date( 'F Y' ) . '</span>' ); elseif ( is_year() ) : printf( __( 'Yearly Archives: %s', 'able' ), '<span>' . get_the_date( 'Y' ) . '</span>' ); elseif ( is_tax( 'post_format', 'post-format-aside' ) ) : _e( 'Asides', 'able' ); elseif ( is_tax( 'post_format', 'post-format-image' ) ) : _e( 'Images', 'able'); elseif ( is_tax( 'post_format', 'post-format-video' ) ) : _e( 'Videos', 'able' ); elseif ( is_tax( 'post_format', 'post-format-quote' ) ) : _e( 'Quotes', 'able' ); elseif ( is_tax( 'post_format', 'post-format-link' ) ) : _e( 'Links', 'able' ); else : _e( 'Archives', 'able' ); endif; ?> </h1> <?php if ( is_category() ) : // show an optional category description $category_description = category_description(); if ( ! empty( $category_description ) ) : echo apply_filters( 'category_archive_meta', '<div class="taxonomy-description">' . $category_description . '</div>' ); endif; elseif ( is_tag() ) : // show an optional tag description $tag_description = tag_description(); if ( ! empty( $tag_description ) ) : echo apply_filters( 'tag_archive_meta', '<div class="taxonomy-description">' . $tag_description . '</div>' ); endif; endif; ?> </header><!-- .page-header --> <?php rewind_posts(); ?> <?php able_content_nav( 'nav-above' ); ?> <?php /* Start the Loop */ ?> <?php if (is_category('2')): ?> <div id="primary" class="site-content"> <?php query_posts('cat=2&showposts=4&posts_per_page=9'); while(have_posts()) : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_excerpt(); endwhile; ?> </div> <?php else: ?> <?php while ( have_posts() ) : the_post(); ?> <?php /* Include the Post-Format-specific template for the content. * If you want to overload this in a child theme then include a file * called content-___.php (where ___ is the Post Format name) and that will be used instead. */ get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php endif; ?> <?php able_content_nav( 'nav-below' ); ?> <?php else : ?> <?php get_template_part( 'no-results', 'archive' ); ?> <?php endif; ?> </div><!-- #content --> </section><!-- #primary .site-content --> <?php get_sidebar(); ?> <?php get_footer(); ?>
Este é o meu ficheiro que testei e está funcional.
Senão funcionar o problema deve ser outro…Ola Carlos, sera que ñ posso implementar esse codigo em outro arquivo como archive.php
- O tópico ‘ajuda com query_posts’ está fechado a novas respostas.